为了给它的祖先一个宽度,一种解决方案是给祖先position:relative
和元素本身position:absolute
。
/* make the three divs visible, to demonstrate the effect */
div {background-color: rgba(128,128,128,.3)}
/* the main point to the solution */
#wrapper {position:relative}
#wrapper > div > div {position:absolute}
/* some cleaning up of the side effects */
#wrapper > div::after {content:'\200B'}
<div id="wrapper" style="width:100%;">
<div style="width:960px;">
<div style="width:30%;">
<div>Hello World!</div>
</div>
</div>
</div>
正如你可以看到,当你显示在整页的代码段中,最里面的div(与最暗背景颜色)是所述最外面的一个的宽度的30%,即使有是一个固定宽度的div。
除非你的意思是别的,在这种情况下,我很抱歉,但你应该更好地解释自己。
如果你不想要它们,为什么要设置'width's? – panther
如果你希望世界是包装宽度,你需要删除宽度960px和30% –
这取决于你的上下文**很多**。什么定位,100%真的有多宽等等。 – Nit