Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do "inline-block" elements in a "nowrap" div overflow?

The following code causes #headline to overflow #wrapper and I do not understand why this is happening.

HTML:

<div id="wrapper">
    <div id="logo">
        <img src="/test.png">
    </div>
    <div id="headline">
        <h1>This headline overflows its wrapping div. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #</h1>
    </div>
</div>

CSS:

#wrapper {
    background: #fea;
    white-space: nowrap;
    width: 50%;
}

#logo {
    display: inline-block;
    vertical-align: middle;
}

#logo img {
       width: 6em; 
}

#headline {
     display: inline-block;
     vertical-align: middle;
     white-space: normal;
}

Example code: http://jsfiddle.net/XjstZ/21/ or http://tinkerbin.com/XvSAEfrK

like image 549
Markus Avatar asked Jul 03 '12 10:07

Markus


People also ask

How do you make inline blocks not wrap?

We apply the white-space: nowrap; property to class “a” which keeps the line of text in the same line even if the screen size is small. Syntax: white-space: nowrap; Next, we apply white-space: normal; which is the default for “white-space”.

How do I stop my DIV from wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

Does padding work for inline elements?

You can add space to the left and right on an inline element, but you cannot add height to the top or bottom padding or margin of an inline element. Inline elements can actually appear within block elements, as shown below. I've added white padding on the left and right side of each inline element.

How do I force HTML elements to stay on the same line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.


1 Answers

As the name suggests, inline-blocks participate in inline layout, which means they act just like inline elements, and text. white-space: nowrap causes text to overflow in an element by preventing it from wrapping; the same thing happens with inline-blocks. It's that simple.

The fact that #headline has white-space: normal has no impact on its own layout. An element's white-space property affects the layout of its contents, not itself, even if the element's own box is inline-level.

like image 116
BoltClock Avatar answered Sep 18 '22 18:09

BoltClock