我有position:fixed
爲什麼它停留在其它元件之上,並隱藏它們固定報頭,所以必須添加padding
到main.
由於header
高度由內容,字體大小和填充組變化通過媒體查詢,不能像片段中那樣爲padding
使用固定值。有沒有一種解決方案可以在不使用JavaScript的情況下改變header
的高度?CSS位置:固定報頭
body {
margin: 0;
padding: 0;
}
header {
background-color: grey;
position: fixed;
width: 100%;
}
main {
padding-top: 80px; /* bad, because it's fixed */
}
<header>
<h1>Example</h1>
</header>
<main>
<p>Content</p>
</main>
使用 '時間' 80px的同等價值將解決字體大小相關的問題。 –
是的,但不是其他問題。想象一下,你永遠不會知道'header'的高度...... – Hativ
由於'position:fixed'的性質,我會猜測沒有純CSS的做法。 –