css設置寬比屏幕大的img圖片在頁面中左右居中

一、html結構css

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

二、css樣式spa

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

總結:margin-left:50%;基於父對象寬度的50%左外邊距,left:50%;偏移父元素寬度的50%htm

百分點單位基本上就是相對於當前元素的父元素的,除了transform的translate是相對自身對象