的宽度不随其父容器或内容的宽度扩展。没有浮动涉及。父div设置了
overflow:scroll
和white-space:pre
;其他都是正常的。在Firefox、IE/Edge、Opera和Chrome中观察到相同的行为。div.wrapper {
padding: 1em;
width: 20em;
background-color: rgb(240, 240, 240);
overflow: scroll;
white-space: pre;
font-family: monospace;
}
div.line {
background-color: rgb(200, 200, 200);
}
div.line span.blue {
color: blue;
}
div.line span.green {
color: green;
}
<div class='wrapper'>
<div class='line'>This text is too <span class="blue">long</span> for the wrapper div to <span class="green">contain</span>, so the scroll bar activates.</div>
<div class='line'>However, the line div's background color fails to <span class="blue">expand</span> with the longer contents, instead ending at the wrapper div's width. Why?</div>
</div>
div.line
的背景颜色在页面上延伸到 div.wrapper
的宽度。但是,当用户向右滚动时,文本会正常显示,而 div.line
的背景颜色则结束了。
我需要的是每个 div.line
都与 div.wrapper
的“内部”宽度一样宽(以填充可滚动区域)。
失败的解决方案:
设置
overflow:hidden
- 这会隐藏溢出的内容,而不是调整 div 的大小设置
overflow:auto
- 没有任何区别添加
<div style="clear:both"> </div>
- 没有任何区别(没有浮动!)设置
after:...clear:both
- 没有任何区别,如上所述在父/子元素上设置高度 - 没有任何区别
设置
width:auto
- 没有任何区别
我已经研究了这个问题,直到我的大脑都疼了。我真的希望我漏看了什么明显的东西。