Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there white space after my inline-block? [duplicate]

I'm using the float style to remove white spaces and I want to center it, but another white space appears after my div#bar

enter image description here

This is my html:

<div id="foo">
    <div id="bar">
        <div class="divo divo1">test1</div>
        <div class="divo divo2">test2</div>
        <div class="divo divo3">test3</div>
        <div class="divo divo4">test4</div>
    </div>
</div>

and css:

#foo {
    width: 100%;
    background: #999;
    text-align: center;
}
#bar {
    display: inline-block;
}
.divo {
    display: block;
    float: left;
}

http://jsfiddle.net/Kodam/ay3ywtqa/

Note: I don't want to use negative margin or font-size 0 styles.

like image 684
Doc Kodam Avatar asked Sep 25 '14 14:09

Doc Kodam


People also ask

Why is there a space between inline block elements?

Here's the deal: a series of inline-block elements formatted like you normally format HTML will have spaces in between them. We often want the elements to butt up against each other. In the case of navigation, that means it avoids the awkward little unclickable gaps.

How do I get rid of whitespace in HTML?

The trim() function removes whitespace and other predefined characters from both sides of a string.

How do I make no space in HTML?

A commonly used entity in HTML is the non-breaking space: &nbsp; A non-breaking space is a space that will not break into a new line. Two words separated by a non-breaking space will stick together (not break into a new line). This is handy when breaking the words might be disruptive.


2 Answers

Since #bar is an inline element, it has space reserved for descender text elements (e.g. j, y, g). You could float that left, but that would collapse it, so I'd recommend setting the vertical alignment to top:

#bar {
    display: inline-block;
    vertical-align:top;
}

jsFiddle example

like image 193
j08691 Avatar answered Nov 15 '22 05:11

j08691


try this :

   #foo {
        width: 100%;
        background: #999;
        font-size:0;
        text-align: center;
    }
    #bar {
        display: inline-block;
        font-size:13px;
    }

set parent font-size to 0, then set the needed font-size to the children.

jsFiddle : http://jsfiddle.net/ay3ywtqa/3/

like image 24
nicolast Avatar answered Nov 15 '22 05:11

nicolast