CSS使图片居中的三种方法

<!--调整背景图位置 :10px center/ center center 10px 和 center就能调整背景图片的位置了-->
<div style="width:30%;height100%;background:url(${ctx}/assets/images/logo.png) no-repeat 10px center"></div>

分开写:

background:url(/../../image/feed.gif)no-repeat;
background-position:10px 5px; /*调整其中的数值就可以达到调整位置的目的。*/
方法一

利用display:table-cell

<div class="img_wrap">
    <img src="wgs.jpg">
</div>
.img_wrap{
    width: 400px;
    height: 300px;
    border: 1px dashed #ccc;
    display: table-cell; /*主要是这个属性*/
    vertical-align: middle;
    text-align: center;
}
方法二

背景法

<div class="img_wrap"></div>
.img_wrap{
    width: 400px;
    height: 300px;
    border: 1px dashed #ccc;
    background: url(wgs.jpg) no-repeat center center;
}
方法三

图片外面用个p标签,通过设置line-height使图片垂直居中

<div class="img_wrap">
    <p><img src="wgs.jpg"></p>
</div>
*{margin: 0px;padding: 0px}
.img_wrap{
    width: 400px;
    height: 300px;
    border: 1px dashed #ccc;
    text-align: center;
}
.img_wrap p{
    width:400px;
    height:300px;
    line-height:300px; /* 行高等于高度 */
}
.img_wrap p img{
    *margin-top:expression((400 - this.height )/2); /* CSS表达式用来兼容IE6/IE7 */
     vertical-align:middle;
    border:1px solid #ccc;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值