4 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
所以你是說(shuō)你想在 div 頂部保持 30 像素間距,但仍然希望能夠向下滾動(dòng)?在這種情況下,我認(rèn)為你必須使 div 本身可滾動(dòng)。但既然這不是你想要的,那么div用position:fixed相同的背景顏色覆蓋另一個(gè)怎么樣?
<body>
<div class="overlay"></div>
<div class="content">
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
text to enable scrolling.<br>
</div>
</body>
body {
margin: 0;
padding: 0;
position: relative;
width: 100%;
height: 100%;
}
.content {
position: relative;
padding-top: 50px;
z-index: 1000;
background: white;
width: 300px;
}
.overlay {
position: fixed;
height: 50px;
width: 100%;
z-index: 2000;
background: white;
}

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
嘗試這個(gè) css 規(guī)則來(lái)修復(fù)你的<div>.
.yourfixeddiv {
position: fixed;
top: 30px;
width: 100%;
}

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果你是絕對(duì)定位的話,你不需要 margin-top。
位置:粘性頂部:50px;應(yīng)該達(dá)到你想要的
div {
position: sticky;
top: 50px;
}

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
position: fixed;
是保持元素就位的方法。重要的是使用width
. 更多信息請(qǐng)參見(jiàn):https ://css-tricks.com/almanac/properties/p/position/
- 4 回答
- 0 關(guān)注
- 247 瀏覽
添加回答
舉報(bào)