331

I have two inline-block div elements, that are the same, positioned next to eachother. However there seems to be a mysterious space of 4 pixels between the two divs despite the margin being set to 0. There are no parent divs effecting them - What is going on?

CSS

#container
{
    display:inline-block;
    position:relative;
    background:rgb(255,100,0);
    margin:0px;
    width:40%;
    height:100px;
}

Div problem

This is what i want it to look like:

What it SHOULD look like

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Legatro
  • 3,692
  • 4
  • 15
  • 21
  • 1
    There is a much simpler answer, but this is so old I'm making it a comment. Instead of using inline-block to make them side by side, just use `float: left` and center their container. No gap in my tests. – Steampunkery Apr 25 '18 at 03:12

6 Answers6

651

In this instance, your div elements have been changed from block level elements to inline elements. A typical characteristic of inline elements is that they respect the whitespace in the markup. This explains why a gap of space is generated between the elements. (example)

There are a few solutions that can be used to solve this.

Method 1 - Remove the whitespace from the markup

Example 1 - Comment the whitespace out: (example)

<div>text</div><!--
--><div>text</div><!--
--><div>text</div><!--
--><div>text</div><!--
--><div>text</div>

Example 2 - Remove the line breaks: (example)

<div>text</div><div>text</div><div>text</div><div>text</div><div>text</div>

Example 3 - Close part of the tag on the next line (example)

<div>text</div
><div>text</div
><div>text</div
><div>text</div
><div>text</div>

Example 4 - Close the entire tag on the next line: (example)

<div>text
</div><div>text
</div><div>text
</div><div>text
</div><div>text
</div>

Method 2 - Reset the font-size

Since the whitespace between the inline elements is determined by the font-size, you could simply reset the font-size to 0, and thus remove the space between the elements.

Just set font-size: 0 on the parent elements, and then declare a new font-size for the children elements. This works, as demonstrated here (example)

#parent {
    font-size: 0;
}

#child {
    font-size: 16px;
}

This method works pretty well, as it doesn't require a change in the markup; however, it doesn't work if the child element's font-size is declared using em units. I would therefore recommend removing the whitespace from the markup, or alternatively floating the elements and thus avoiding the space generated by inline elements.

Method 3 - Set the parent element to display: flex

In some cases, you can also set the display of the parent element to flex. (example)

This effectively removes the spaces between the elements in supported browsers. Don't forget to add appropriate vendor prefixes for additional support.

.parent {
    display: flex;
}
.parent > div {
    display: inline-block;
    padding: 1em;
    border: 2px solid #f00;
}

.parent {
    display: flex;
}
.parent > div {
    display: inline-block;
    padding: 1em;
    border: 2px solid #f00;
}
<div class="parent">
    <div>text</div>
    <div>text</div>
    <div>text</div>
    <div>text</div>
    <div>text</div>
</div>

Sides notes:

It is incredibly unreliable to use negative margins to remove the space between inline elements. Please don't use negative margins if there are other, more optimal, solutions.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
  • Ok I guess i could patch it up with negative margins - thanks :) however i dont know what or how to get rid of whitespace - agh il research it – Legatro Sep 26 '13 at 21:19
  • Ok negative margin seems to be working fine with no bugs - and thanks, whitespace is really that simple! – Legatro Sep 26 '13 at 21:27
  • 11
    Since no one really bothered to explain **why** this happens... inline (and inline-block) elements respect whitespace in the markup, so what you're seeing is essentially the **space character** between "words" in the markup. Which is why setting a negative `word-spacing` can also "fix" this, although negative word-spacing and negative margin are invalid solutions because the spacing depends on your browser and your font-size (as demonstrated by Josh's last example), so it's going to be the first thing that breaks if you make any changes. – colllin Oct 20 '14 at 02:26
  • 3
    That moment when you spend 20 minutes debugging your CSS, only to discover Chrome is zoomed in to 110% and there was never a gap at all. – Imperative Feb 06 '15 at 22:12
  • I think setting `font-size:0` to parent element is not a good solution. It makes impossible to use the relative unity `em`, witch is a very good choice for font-size, because you will be unable to reset the font-size at the inner element. – jairhumberto May 02 '15 at 19:37
  • @jairhumberto I know that. Did you even read my answer? I said *"however, it doesn't work if the child element's `font-size` is declared using `em` units. I would therefore recommend removing the whitespace from the markup."*. – Josh Crozier May 02 '15 at 19:44
  • Sorry, I read just the first lines, but even using absolute unity I would never choose that as a solution, the rest of what you pointed out was perfect in my opinion. – jairhumberto May 03 '15 at 21:48
  • 25
    I'd say that `display: flex` is the best solution these days. – Alan Shortis Nov 13 '15 at 14:14
  • 3
    `font-size` was the only one that worked for me for the extra vertical space between two `inline-block` elements (which were that way to `text-align: center` them within another `div`). `flex` completely messed up the layout – poshest Sep 29 '16 at 10:58
  • Suggested solutions are fine when you have full control over the styles but not when you do not (i.e. plugins, side-loaded or injected features). I've had reliable result using `0.44em` negative margin. – Don Park Mar 12 '20 at 16:22
  • You my good sir are level 10 web developer.. I've been doing this a very long time and it didn't even dawn on me to use a comment to remove white spaces. Hats off to you! – James Harrington Mar 27 '20 at 21:39
  • honestly, this makes no sense why this occurs, but this answer is 100% true – openwonk Nov 29 '20 at 05:42
38

Using inline-block allows for white-space in your HTML, This usually equates to .25em (or 4px).

You can either comment out the white-space or, a more commons solution, is to set the parent's font-size to 0 and the reset it back to the required size on the inline-block elements.

daniel aagentah
  • 1,672
  • 5
  • 29
  • 56
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
  • This is a cool solution that I never thought of, but unfortunately breaks the normal CSS inheritance of font-sizes :/ – colllin Oct 20 '14 at 02:27
  • @colllin Yes, it does...but you just have to be aware of it. Inline-block and float both have their 'gotchas'...how you resolve them is a matter of choice. The font solution is one option but not the only one. – Paulie_D Oct 20 '14 at 09:24
  • sure but reducing the things I "have to be aware of it" is always a good thing, and so is protecting yourself from accidentally breaking something in 6 months. – colllin Oct 20 '14 at 16:23
  • @Pauli_D You could also put comments between you elements as they remove the whitespace not only in the document but also on the site. It's a bit ugly and a pain in the butt when you have ton's of elements. – Niklas Vest Dec 25 '15 at 12:42
  • Thank you, I'm well aware of the various methods of dealing with `inline-block`...this is just one **from two years ago** – Paulie_D Dec 25 '15 at 12:51
18

Any easy fix although it's a bit outdated at this point in time is to just float the container. (eg. float: left;) On another note, each id should be unique, meaning you can't use the same id twice in the same HTML document. You should use a class instead, where you can use that same class for multiple elements.

.container {
    position: relative;
    background: rgb(255, 100, 0);
    margin: 0;
    width: 40%;
    height: 100px;
    float: left;
}
bradcush
  • 528
  • 3
  • 8
  • Although this does work (thakyou) it causes other problems in the layout of my page that im probably at the moment too inexperienced to solve – Legatro Sep 26 '13 at 21:34
  • By adding the `float` property you forces the element to act as a block element, so the `display: inline-block` doesn't apply. – sunpietro Jan 23 '19 at 13:10
3

Found a solution not involving Flex, because Flex doesn't work in older Browsers. Example:

.container {
    display:block;
    position:relative;
    height:150px;
    width:1024px;
    margin:0 auto;
    padding:0px;
    border:0px;
    background:#ececec;
    margin-bottom:10px;
    text-align:justify;
    box-sizing:border-box;
    white-space:nowrap;
    font-size:0pt;
    letter-spacing:-1em;
}

.cols {
    display:inline-block;
    position:relative;
    width:32%;
    height:100%;
    margin:0 auto;
    margin-right:2%;
    border:0px;
    background:lightgreen;  
    box-sizing:border-box;
    padding:10px;
    font-size:10pt;
    letter-spacing:normal;
}

.cols:last-child {
    margin-right:0;
}
Larry
  • 31
  • 1
1

simply add a border: 2px solid #e60000; to your 2nd div tag CSS.

Definitely it works

#Div2Id {
    border: 2px solid #e60000; --> color is your preference
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
-2

You need to add

#container
{
    display:inline-block;
    position:relative;
    background:rgb(255,100,0);
    margin:0px;
    width:40%;
    height:100px;
    margin-right:-4px;
}

because whenever you write display:inline-block it takes an additional margin-right:4px. So, you need to remove it.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
BigFan
  • 1
  • 1