我有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的做法。 –