简体   繁体   English

为什么 100% 或 100vh 高度没有占据整个页面高度

[英]why 100% or 100vh height not occupying full page height

I was learning css pseudo-selector class ::before .我正在学习css伪选择器 class ::before I am confused how come 100% value given to height property of background-color not actually occupying full page height.我很困惑为什么background-colorheight属性的 100% 值实际上并没有占据整个页面高度。 I tried with viewport height also.我也尝试了视口高度。 Same issue.同样的问题。 Below is my code:下面是我的代码:

 * { margin: 0; padding: 0; }:root { --navbar-height: 59px; } #navbar { display: flex; align-items: center; border: 2px solid red; } #navbar ul { display: flex; } #navbar::before { content: ""; background-color: black; position: absolute; /* height being 100% or 100vh not occupying full page */ height: 100vh; width: 100%; z-index: -1; opacity: 0.4; } #navbar ul li { list-style: none; font-size: 1.3rem; } #navbar ul li a { display: block; padding: 3px 22px; border-radius: 20px; text-decoration: none; }
 <nav id="navbar"> <ul> <li class="item"><a href="#">home</a></li> <li class="item"><a href="#">services</a></li> <li class="item"><a href="#">contactus</a></li> </ul> </nav>

output output

在此处输入图像描述

what I expected was black background-color because have been given 100% height will occupy whole page.我所期望的是黑色background-color ,因为已给出 100% 的高度将占据整个页面。 But it didn't happen.但它没有发生。 Why is it so?为什么会这样?

The issue is due to display: flex;问题是由于 display: flex; on #navbar.在#navbar 上。 Remove that and you should be fine.删除它,你应该没问题。 Another solution is wrapping the navbar in a parent div and adding:: before on it.另一种解决方案是将导航栏包装在父 div 中并在其之前添加::。

You just need to position your :before correctly.你只需要 position 你的:before正确。 Add below CSS to your #navbar::before and you should be set:将下面的 CSS 添加到您的#navbar::before ,您应该设置:

top: 0;
left: 0;

Because your positioning is absolute you need to check the top and left properties.因为您的定位是绝对的,所以您需要检查 top 和 left 属性。

I replicated your code and then opened the chrome dev tools as shown in the attached screenshot.我复制了您的代码,然后打开了 chrome 开发工具,如随附的屏幕截图所示。 If you open the computed tab you will see that the top property is -516.378px.如果打开计算选项卡,您将看到 top 属性为 -516.378px。

You can do this by clicking Elements>Computed which will show you all the properties after the browser has calculated them.您可以通过单击 Elements>Computed 来执行此操作,这将在浏览器计算出所有属性后显示给您。

铬开发工具

If you add top: 0;如果添加顶部:0; as shown below you can override the calculated css position.如下所示,您可以覆盖计算的 css position。

#navbar::before {
  content: "";
  background-color: black;
  position: absolute;
  /* height being 100% or 100vh not occupying full page */
  height: 100vh;
  width: 100%;
  top: 0;
  z-index: -1;
  opacity: 0.4;
}

Here's a stackblitz example you can play with to try it out yourself:这是一个 stackblitz 示例,您可以自己尝试一下:

https://stackblitz.com/edit/js-82vdk5?file=style.css https://stackblitz.com/edit/js-82vdk5?file=style.css

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么即使高度为100%,高度为100vh,也无法获得完整的页面? - Why I can't get full length page even with height 100% and height 100vh? 在不使用100vh的情况下将div强制设为完整页面高度 - Forcing a div to be full page height without using 100vh 车身高宽为100vh; 并且其中的 div 宽度为 100vh 或 100% 但它没有接触边缘?为什么 - Body height and width is 100vh; and the div in this has width 100vh or 100% but it is not touching the edge ?why 如何在高度为 100Vh 的页面上添加 100vh header 图像? - How to add a 100vh header image on a page with a height of 100Vh? 使用 100vh 并具有居中内容的 HTML 全高英雄 - HTML full height hero using 100vh with centered content 为什么使用100vh作为高度后组件没有拉伸到web页面的高度 - Why is the component not stretching to the height of the web page after using 100vh as the height 将 100vh 高度页面中的元素向上滚动并移出视图 - Scroll elements in a 100vh height page up and out of view 页面上的3个元素,总共100vh,一个高度未知 - 3 elements on page, 100vh altogether, one height unknown ```高度:100vh```和页面底部的白色边距的意外行为 - Unexpected behavior with ```height: 100vh``` and white margin at the bottom of the page 为什么这个高度为100vh的div无法覆盖整个视口? - Why is this div with 100vh height not covering the whole viewport?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM