I'm having trouble getting a parent to inherit the width of it's children. The jsfiddle is here http://jsfiddle.net/4mk3S/
right now I just have an outer div with
white-space: nowrap;
and children divs with
display: inline-block;
I would like the outer div to inherit the width of it's entire group of children. Any help would be really appreciated, thanks!
Code :
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
</div>
.outer {
white-space: nowrap;
padding: 10px;
background: green;
}
.inner {
display: inline-block;
width: 1000px;
height: 100px;
background: red;
}
The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS. Inline elements flow left to right, meaning inline elements appear on the same line unless the line wraps or there's an explicit line break ( <br/> )
Thus, you can't set the width of an inline element. Block elements are made to fill all the available width by default and are thus on their own line rather than flowing inline.
If you have a border or padding set for your divs, then you've created additional pixels that will prevent your divs from adding up to a 100% width. To fix this, make sure you've added box-sizing: border-box to the div's styles.
As a result, the elements can sit next to each other. The major difference between display: inline; and display: inline-block; is that, display: inline-block; also allows us to set the width and height of the element. We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes.
add display: inline-block;
to the parent also
inline-block
forces the container to wrap all of its children while block
(the default for div) sets the width to that of the parent
Updated fiddle
Relevant code to be changed
.outer {
white-space: nowrap;
padding: 10px;
background: green;
display:inline-block;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With