Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't two inline-blocks line up to the top in parent wrapper div?

Tags:

html

css

I have my HTML like this

<div id="wrapper">
    <div id="main">
        <p>test</p>
    </div>
    <div id="sidebar">
        <p>test</p>
    </div>
</div>

And CSS

#wrapper {
    width: 960px;
    margin: 0px auto;
}

#main {
    width: 790px;
    display: inline-block;
    padding: 0px;
    margin: 0px;
}

#sidebar {
    width: 170px;
    display: inline-block;
    vertical-align: top;
    padding: 0px;
    margin: 0px;
}

Example: http://jsfiddle.net/Hpwff/

The problem is that even though the sum of both divs is 960px, which is the same width as the parent container's (#wrapper), they do not float next to each other. I have to shrink either the sidebar or main containers width back by 4px so they fit. Why is this, and is there a way around it?

like image 914
Ayub Avatar asked Jun 18 '12 19:06

Ayub


People also ask

What is the difference between display inline and inline block in CSS?

CSS Layout - display: inline-block. ❮ Previous Next ❯. Compared to display: inline, the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block, the top and bottom margins/paddings are respected, but with display: inline they are not.

How can I have three inline Divs of the same width?

With the flexbox styles applied, we can have three inline divs of equal width – just as we can with the inline-block solutions. One of the great things about CSS is that there is usually more than one way to solve a problem. Hopefully some of the suggestions above will help you with your future inline element spacing issues.

How to prevent inline-block Divs from wrapping in Bootstrap?

We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes. Let us understand this with the help of an example: Attention reader! Don’t stop learning now.

Why float in CSS display inline-block?

CSS display: inline-block: why it rocks, and why it sucks - Robert's talkRobert's talk – Web development and Internet trends Usually when you want a horizontal list, you need to use float in the CSS code to make it work, with all its drawbacks.


1 Answers

You have a newline between the two divs; since they are inline-block, the newline between them is rendered as a space. Without space it works as you expect.

like image 145
lanzz Avatar answered Sep 28 '22 12:09

lanzz