Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with inline-block elements with unknown width

Tags:

html

css

I'd like to line up two elements in a header:

<div class="col-md-3">
    <div class="header">
        <div class="icon"></div>
        <div class="title"><h1>This is the title row</h1></div>
    </div>
    <p>This is some content text. This is some content text. This is some content text. This is some content text. This is some content text. </p>
</div>

To the left is a fixed width icon, and I want to the title to fill up the remaining width. (The title is broken in two lines because of the narrow columns):

.col-md-3 {
    width: 200px; /*this width is just for illustrating the problem, in reality this is Bootstrap's 25% width column*/
}
.icon {
    display: inline-block;
    height:50px;
    width: 50px;
    background: red;
}
.title {
    display: inline-block;
}

I can't get them to line up using inline-block. Any ideas why?

PLUNKER

UPDATE

I added a new plunker (see above) to better demonstrate the problem.

I'm looking for an explanation why inline-block doesn't work in this case, and a possible solution how to make it work. Any workarounds posted are really appreciated, but I'd really like to find out what's the deal with inline-blocks in this case.

like image 713
orszaczky Avatar asked Mar 15 '15 16:03

orszaczky


People also ask

Does width work on inline elements?

Note: An inline element does not start on a new line and only takes up as much width as necessary.

Is inline elements in HTML are not affected by Width and height properties?

An inline element is an element that's width and height are determined by the content it contains. Inline elements, such as a <span> , will completely ignore the width and height as well the the top and bottom margin properties because, well, the content is what determines the dimensions.

Does margin affect inline elements?

Top and bottom margins do not affect inline elements because inline elements flow with content on the page. You can set left and right margins/padding on an inline element but not top or bottom because it would disrupt the flow of content.


1 Answers

If the content of the inline-block does not fit on a single row, it will try to fit as a whole on the next line. This is different from regular inline elements, that most of the time is allowed to wrap to the next line.

You might want to read up on this behaviour at the W3C specification about the 'normal flow'.

Not sure why everyone makes it so complex, why not use a float?

.icon {
    float: left;
    width: 50px;
    height: 50px;
    background-color: red;
}
.title {
    padding: 0 10px;
    overflow: hidden;
}
.title h1 {
    line-height: 50px;
    margin: 0;
}
<div class="col-md-3">
    <div class="header">
        <div class="icon"></div>
        <div class="title"><h1>This is the title row erg erg erg erg erg erg er</h1></div>
    </div>
    <p>This is some content text. This is some content text. This is some content text. This is some content text. This is some content text. </p>
</div>
like image 113
Justus Romijn Avatar answered Oct 02 '22 05:10

Justus Romijn