[英]Full width child elements inside scrollable container
I have a div-container with a fix width and some child-elements witch could be bigger than the parent. 我有一个具有固定宽度的div容器,并且一些子元素可能比父元素大。
Is it possible to let all the child-elements take the full width of the scrollable area from the parent-element ( overflow: auto
)? 是否可以让所有子元素从父元素(
overflow: auto
)获取可滚动区域的整个宽度?
#container { width: 200px; background-color: grey; overflow:auto; margin:10px; } #container p{ display:block; background-color: green; white-space: nowrap; width: 100%; }
<div id="container"> <p>Sample Text 1</p> <p>Sample Text 2</p> <p>A very very very very very long Sample Text</p> </div>
Here is the fiddle . 这是小提琴 。 When you scroll to the right, you can see that the child-elements background-color is smaller than the content.
向右滚动时,您可以看到子元素background-color小于内容。
Wrap the content in a div, and set it to display: inline-block
: 将内容包装在div中,并将其设置为
display: inline-block
:
#container { width: 200px; background-color: grey; overflow: auto; margin: 10px; } #container>div { display: inline-block; } #container p { display: block; background-color: green; white-space: nowrap; }
<div id="container"> <div> <p>Sample Text 1</p> <p>Sample Text 2</p> <p>A very very very very very long Sample Text</p> </div> </div>
You could set the child elements to display:table-row;
您可以设置要
display:table-row;
的子元素display:table-row;
#container { width: 200px; background-color: grey; overflow: auto; } #container p { display: table-row; background-color: green; white-space: nowrap; }
<div id="container"> <p>Sample Text 1</p> <p>Sample Text 2</p> <p>A very very very very very long Sample Text</p> </div>
Add a extra <div>
if you need extra controls for styling. 如果您需要额外的样式控件,请添加额外的
<div>
。
#container { width: 200px; background-color: grey; overflow: auto; } #container div { display: table; border-spacing: 0 10px; } #container p { display: table-row; background-color: green; white-space: nowrap; }
<div id="container"> <div> <p>Sample Text 1</p> <p>Sample Text 2</p> <p>A very very very very very long Sample Text</p> </div> </div>
You can use from absolute position property to do that . 你可以使用绝对位置属性来做到这一点。
#container { width: 200px; background-color: grey; overflow:auto; margin:10px; } #container p{ display:block; background-color: green; white-space: nowrap; position:absolute; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.