css设置宽比屏幕大的img图片在页面中左右居中

1、html结构

<div class="div1">
    <div class="div2">
        <img src="1.jpg" alt="">  
    </div> 
</div>

2、css样式

.div1{width: 100%;overflow: hidden;}
.div2{width: 1920px;height: 100px;background-color: #000;margin-left: 50%;}
img{margin-left: -50%;}

总结:margin-left:50%;基于父对象宽度的50%左外边距,left:50%;偏移父元素宽度的50%

百分点单位基本上就是相对于当前元素的父元素的,除了transform的translate是相对自身

       

猜你喜欢

转载自my.oschina.net/u/879103/blog/730689