我有
<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/