Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

50% inline-blocks not displaying next to each other

Tags:

html

css

Say I have

<span class="ib-half"></span>
<span class="ib-half"></span>

and

.ib-half {
    display: inline-block;
    width: 50%;
}

I expect the two spans to display side-by-side but they won't. There's no margin, padding, or border, so what's the problem?

like image 989
c.. Avatar asked Dec 12 '12 02:12

c..


People also ask

How do you put two blocks next to each other in HTML?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.

Why is display inline block not working?

Because you are using inline-block, any newline character or whitespace you have after the element and before another inline element, will be counted as a space. If you want the blocks to stack side by side like in your picture, your HTML would need to be like this.

How do inline block elements add up to 100 width?

To do this, you simply need to add display: flex to the parent container and flex-grow: 1 to the divs (as opposed to defining a width). With the flexbox styles applied, we can have three inline divs of equal width – just as we can with the inline-block solutions.


3 Answers

Setting the font-size of the parent element to zero may be a fix.

HTML :

<div class = "parent">
    <span class="ib-half">Left</span>
    <span class="ib-half">Right</span>
</div>

CSS:

span{
    background:#bdbdbd;
}

.ib-half {
    display: inline-block;
    width: 50%;
    font-size:10px;
}

.parent {
    font-size: 0;
}

Check this fiddle. http://jsfiddle.net/YpTMh/9/

For more options please refer to http://css-tricks.com/fighting-the-space-between-inline-block-elements/

like image 163
Praveen Puglia Avatar answered Oct 27 '22 22:10

Praveen Puglia


The actual problem is the space (newline) between the two elements. Because it's an inline-block element, it registers the space, so it's 50% + the space.

Some possibilities:

<span class='left'>Left</span><!--
--><span class='right'>Right</span>

or <span class='left'>Left</span><span class='right'>Right</span>

or

<span class='left'>Left</span><span
class='right'>Right</span>

or to me it really probably makes the most sense to float: left; and change it to a display: block element. I believe the nature of inline elements is to operate in the same manner as text with some extra spacial information, so why get hacky when there's no reason to?

like image 32
c.. Avatar answered Oct 27 '22 20:10

c..


good answer in css3 is:

white-space: nowrap;

in parent node, and :

white-space: normal;
vertical-align: top;

in div (or other) at 50%

exemple : http://jsfiddle.net/YpTMh/19/

like image 31
Matrix Avatar answered Oct 27 '22 21:10

Matrix