Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of space between divs when display inline-block and stacked horizontally? [duplicate]

Tags:

html

css

I have three divs horizontally stacked side by side to one another and these divs are set to display: inline-block. However, I have noticed that even with using some sort of CSS reset they produce a small 4px distance between each other? Why does this occur? Is there a way to globally get rid of this from happening or do I just have to shift the divs to the left -4px?

like image 820
Matt Avatar asked Jan 09 '13 16:01

Matt


People also ask

How do I get rid of spaces between divs?

This is caused by the fact your browser will render the DIV's inline and as with words, they are separated by spaces. The width of the space is determined by the font-size, hence an easy trick is to set the font-size of your containing element to 0 and then reset the font-size in your inline divs.

Why is there a space between two divs?

This is because you set #DivMain to position: relative , so you created a new stacking context that is painted over the previously rendered elements.


1 Answers

This is caused by the fact your browser will render the DIV's inline and as with words, they are separated by spaces.

The width of the space is determined by the font-size, hence an easy trick is to set the font-size of your containing element to 0 and then reset the font-size in your inline divs.

#container { font-size: 0; }  #container > div {font-size: 1rem; display: inline-block; } 

I have demonstrated this in this Fiddle.

Take a look at this article for more info.

like image 70
thomaux Avatar answered Sep 28 '22 00:09

thomaux