Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make parent with nowrap inherit width of inline-blocks

Tags:

html

css

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;
}
like image 567
elju Avatar asked Jul 28 '13 19:07

elju


People also ask

Can I set width of inline element?

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/> )

How do I increase the width of an inline-block?

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.

How do inline-block elements add up to 100 width?

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.

How do you prevent inline-block elements from wrapping?

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.


1 Answers

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;
}
like image 61
omma2289 Avatar answered Nov 14 '22 23:11

omma2289