我試圖弄清楚如何在使用位置固定時保持 div 的寬度,固定 div 有一個額外的寬度
.col-1 {
background-color: blue;
height: 58px;
border-radius: 12px;
}
.fixed {
z-index: 9999;
position: fixed;
background-color: red;
margin-top: 100px;
width: 100%;
}
<div class="col-1">Test</div>
<div class="col-1 fixed">Test</div>
uj5u.com熱心網友回復:
您可以將left: 0;和添加right: 0;到.fixeddiv 以實作 100% 寬度
.fixed {
z-index: 9999;
position: fixed;
background-color: red;
margin-top: 100px;
width: 100%;
left: 0;
right: 0;
}
這看起來不會完全相同,因為第一個 div 已經margin應用到它來自<body>,給它你在它周圍看到的空白區域。我建議使用重置洗掉該邊距以實作相同的寬度。
一個例子:
body {
margin: 0;
}
當然,如果要設定邊距,您可以設定left和right以匹配任何內容,在本例中為8px。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338622.html
標籤:css
