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?
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.
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.
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.
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/
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?
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With