在桌面上,如果我将body
或 a设置div
为 以便100vh
占用所有屏幕空间并让溢出正常运行,则标签的实际高度是浏览器内容窗口的真实可见高度。
相反,在 Android 上(使用 Mozilla Focus 117.0 进行测试),它不会处理浏览器标题,因此页面不完整。
html {
height: 100%;
}
body {
min-height: 100%;
}
.content {
height: 100vh;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
<form>
<div class="col-lg-6 mx-auto d-flex flex-column content">
<div class="container-fluid bg-light px-3 pt-1 flex-shrink-1">
<p class="text-center display-4 h1">My project</p>
<hr class="my-1">
</div>
<div class="p-2 flex-grow-1 overflow-scroll">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.
Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh.
Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet.
Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna
non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in
justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue.
Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam.
Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan
porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci.
</p>
</div>
<div class="px-2 flex-shrink-1">
<hr class="my-1">
<div class="input-group">
<input type="text" class="form-control" placeholder="Placeholder..." aria-label="Placeholder..." aria-describedby="submit">
<button class="btn btn-outline-secondary" type="submit" id="submit">Send</button>
</div>
<div class="my-1 alert alert-success" role="alert">
Some messages
</div>
</div>
<div class="px-2 flex-shrink-1">
<div id="carousel" class="carousel slide carousel-fade" data-bs-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img src="banner/test1.jpg" class="d-block w-100" alt="SAMPLE BANNER">
</div>
</div>
</div>
</div>
</div>
</form>
具体来说,如果用户向下滚动页面时浏览器隐藏工具栏,就会发生这种情况。例如,在我的 apache Web 服务器上运行的完全相同的代码会导致我描述的问题,但无论出于何种原因,如果我在 stackoverflow.com内运行代码片段:
- 浏览器不隐藏工具栏
- 溢出工作正常
我想可以告诉浏览器保留工具栏(即this),但这对我来说似乎是一个解决方法。
有没有办法在有或没有浏览器工具栏的情况下将可见高度设置为实际可用空间?
这似乎是这个问题的重复,但我不想改变浏览器行为,我只想使用可用空间。
您可以使用考虑移动地址栏的新视口单元之一。可能较大或动态视口高度(lvh 或 dvh)。
您可以在这里找到简单的解释: https: //web.dev/viewport-units/
与往常一样,您应该在使用较新的网络功能之前检查浏览器支持,但在这种情况下,支持已经相当不错: https: //caniuse.com/viewport-unit-variants