html - 在未知大小的非固定标题下方填充剩余屏幕高度
问题描述
我想实现下面代码片段的布局:
- 顶部的非固定导航栏,高度未知
- 用
#title div
- 文章在标题之后继续(最初在屏幕上不可见,仅在向下滚动后)
这个片段的问题在于,它完全破坏了 HTML 的语义层次结构。将#uglyWrapper
文章分为两部分。
我可以使用 #title { height: calc(100vh - $nav-height) }
,但顶栏本身就是一个 flexbox。所以$nav-height
不为人知。
是否有一个纯 CSS 的解决方案,它不会与这个 HTML 结构混淆:
body
nav
article
#title
text
片段演示,它应该是什么样子:
#uglyWrapper {
height: 100%;
display: flex;
flex-direction: column;
}
#title {
background-color: lightblue;
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
nav {
background-color: grey;
}
<body>
<div id="uglyWrapper">
<nav>top nav with unknown height, not fixed</nav>
<!-- article -->
<div id="title">Full height title image</div>
</div>
Article continues <br>
much text <br>
much text <br>
much text
<!-- /article -->
</body>
解决mfluehr 的答案:
这种方法不会填满剩余的空间。相反,它会填充整个屏幕高度并被标题栏剪裁。我故意离开了关于图像的细节,而不是要求一个 div。因为该 div 中还有其他元素,所以需要精确的布局。所以覆盖一个未知大小的区域是有问题的。请不要停留在图像的细节上并使用最小的示例。我可以从那里开始工作。
解决方案
将绝对定位应用于<nav>
. 由于标题图像将使用object-fit: cover
,因此顶部的块是否被导航覆盖并不重要。
#title {
background-color: lightblue;
height: 100vh;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
nav {
background-color: grey;
position: absolute;
top: 0;
left: 0;
width: 100%;
}
<body>
<nav>top nav with unknown height, not fixed</nav>
<article>
<div id="title">Full height title image</div>
much text <br>
much text <br>
much text
</article>
</body>
推荐阅读
- json - 将动态 Json 对象解析为 VB 类
- mysql - 如何减少sql语句的查询时间,mysql
- java - 我应该如何将这些对象存储在 SQLite 数据库中?
- javascript - WebApplications,如何在设备处于睡眠状态时通知用户
- android - 无法解决依赖关系:com.squareup.retrofit2:retrofit:2.3.0
- internet-explorer - IE 未在页面刷新时为 HTTPS 加载字体
- ios - MKMapCamera 无法缩放到正确的高度
- c - 如何复制堆对象以防止其被破坏?
- javascript - 是否可以在 Safari 中使用 javascript 在文件标签中添加 base64 数据?
- swift - 如何使 UITextField 填充