gpt4 book ai didi

html - CSS 使 div 100% 宽度与主体相关,而不是它的父 div

转载 作者:太空狗 更新时间:2023-10-29 15:15:01 26 4
gpt4 key购买 nike

我有

<div style="position:relative; width:500px;">
<div style="position:absolute; width:100%"></div>
</div>

子 div 占其父 div 的 100% 宽度。有没有一种方法可以强制它占用 100% 的主体宽度而不是父 div。相对和绝对定位都是必需的,因为我在上面的代码中有它。

最佳答案

现代浏览器有support对于 viewport units .

header {
width: 50%;
background-color: lightblue;
padding: 20px 0;
}

nav {
width: 100vw;
background-color: orange;
padding: 20px 0;
}
  <html>
<body>

<header>
<p>This inherits the width of the parent like normal.</p>

<nav>
<a href="/">this fills the viewport</a>
</nav>
</header>

</body>
</html>

关于html - CSS 使 div 100% 宽度与主体相关,而不是它的父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7948305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com