40

Inline blocks have this weird space in-between them. I could live with it, up to a point where, if I load more content with an AJAX call, the tiny space goes away. I know I'm missing something here.

div {
    width: 100px;
    height: auto;
    border: 1px solid red;
    outline: 1px solid blue;
    margin: 0;
    padding: 0; 
    display: inline-block;
}

http://jsfiddle.net/AWMMT/

How to make the spacing consistent in Inline blocks?

Andreas
  • 4,832
  • 6
  • 40
  • 49
justnorris
  • 3,466
  • 6
  • 30
  • 48
  • 4
    Elements in the inline formatting context will cause white spaces from carriage returns and white-spaces in your HTML - you can either manually delete the white-space or you can comment it out. Unfortunately the 2.1 spec doesn't go into much detail regarding this behaviour – Jason Yaraghi May 21 '13 at 20:30
  • 4
    http://css-tricks.com/fighting-the-space-between-inline-block-elements/ – cimmanon May 21 '13 at 20:31
  • @cimmanon Best answer is pointing to css-tricks! Thanks. – justnorris May 21 '13 at 20:41
  • http://davidwalsh.name/remove-whitespace-inline-block – Adrien Be Mar 03 '15 at 09:17

5 Answers5

50

The space is in the HTML. There are several possible solutions. From best to worst:

  1. Remove the actual space in the HTML (ideally your server could do this for you when the file is served, or at least your input template could be spaced appropriately) http://jsfiddle.net/AWMMT/2/
  2. Use float: left instead of display: inline-block, but this has undesirable effects on t he height: http://jsfiddle.net/AWMMT/3/
  3. Set the container's font-size to 0 and set an appropriate font-size for the internal elements: http://jsfiddle.net/AWMMT/4/ -- this is pretty simple, but then you can't take advantage of relative font size rules on the internal elements (percentages, em)
Explosion Pills
  • 176,581
  • 46
  • 285
  • 363
  • I've been floating for a long time and was hoping for displaying my boxes as inline-blocks now, but this just makes the markup a little bit unreliable. I mean - a layout might be ruined just because of a simple whitespace. Thanks. – justnorris May 21 '13 at 20:35
  • 2
    Most browsers offer a minimum font-size setting, making `font-size: 0` completely unreliable. – cimmanon May 21 '13 at 20:47
  • I removed whitespaces in server side loop and added an explicit comment about inline blocks and weird tag alignment in PHP. – justnorris May 21 '13 at 20:49
  • @cimmanon define "most browsers," because I've never seen that be the case (although I agree it's not the ideal option; removing the whitespace is probably the best, at least for now) – Explosion Pills May 21 '13 at 20:51
  • 1
    Opera has had it since forever, Chrome, Firefox, Safari. Seems like "most browsers" to me. – cimmanon May 21 '13 at 21:00
  • @cimmanon if that's the case then why does my example work properly in (at least) Chrome and Firefox? – Explosion Pills May 21 '13 at 21:04
  • @cimmanon - Just testing Firefox's minimum font size setting, it doesn't seem to affect `font-size:0`. Haven't tried other browsers. – Alohci May 21 '13 at 23:32
  • You may want to use `line-height: 0`. I prefer this to `font-size` because `font-size` is inherited from the parent, whereas `line-height` is influenced from its own `font-size`. This is to avoid his warning in point 3. – roydukkey Dec 31 '14 at 03:59
  • #3 sounds doable, others - not so much, as they have repercussions. – Neolisk Jul 05 '16 at 18:15
21

http://jsfiddle.net/AWMMT/1/

<div>...</div><div>...</div>
              ^
              |--- no whitespace/new line here.

Your spaces were the new lines the browser converted to "spaces" when displaying it.

Or you could try to hack a bit with CSS:

A flexbox conveniently ignores whitespace between its child elements and will display similarly to consecutive inline-block elements.

http://jsfiddle.net/AWMMT/470/

body { display: flex; flex-wrap: wrap; align-items: end; }

Old answer (still applies to older, pre-flexbox browsers) http://jsfiddle.net/AWMMT/6/

body { white-space: -0.125em; }
body > * { white-space: 0; /* reset to default */ }
bwoebi
  • 22,955
  • 4
  • 54
  • 75
  • 1
    Oh god. Is that thing still here? I forgot about that since I haven't used inline-blocks for a while now. Is there a name for that so I can keep tabs on whether someone is going to "fix" it someday in browsers ? Now I remember why I usually float my boat. – justnorris May 21 '13 at 20:32
  • 5
    it's called 'whitespace' – Kevin Lynch May 21 '13 at 20:33
  • @Norris just added a CSS method to prevent this spacing here – bwoebi May 21 '13 at 20:34
  • It works for me in gecko as in webkit with no problems and won't affect any additional non-whitespace characters you may want to add between the divs (like a `-`). (the advantage to a font-size) – bwoebi May 21 '13 at 20:40
  • 4
    Beware of magic numbers like -2, etc.: http://css-tricks.com/magic-numbers-in-css/ – cimmanon May 21 '13 at 20:46
  • @cimmanon changed to em. (as it should be relative to font-size) – bwoebi May 21 '13 at 20:49
  • @cimmanon not if you use standard fonts, they all have the same space; if you use exotic fonts... okay... but then you have to use another hack (like the font-size) – bwoebi May 21 '13 at 20:52
9

There’s actually a really simple way to remove whitespace from inline-block that’s both easy and semantic. It’s called a custom font with zero-width spaces, which allows you to collapse the whitespace (added by the browser for inline elements when they're on separate lines) at the font level using a very tiny font. Once you declare the font, you just change the font-family on the container and back again on the children, and voila. Like this:

@font-face{ 
    font-family: 'NoSpace';
    src: url('../Fonts/zerowidthspaces.eot');
    src: url('../Fonts/zerowidthspaces.eot?#iefix') format('embedded-opentype'),
         url('../Fonts/zerowidthspaces.woff') format('woff'),
         url('../Fonts/zerowidthspaces.ttf') format('truetype'),
         url('../Fonts/zerowidthspaces.svg#NoSpace') format('svg');
}

body {
    font-face: 'OpenSans', sans-serif;
}

.inline-container {
    font-face: 'NoSpace';
}

.inline-container > * {
    display: inline-block;
    font-face: 'OpenSans', sans-serif;
}

Suit to taste. Here’s a download to the font I just cooked up in font-forge and converted with FontSquirrel webfont generator. Took me all of 5 minutes. The css @font-face declaration is included: zipped zero-width space font. It's in Google Drive so you'll need to click File > Download to save it to your computer. You'll probably need to change the font paths as well if you copy the declaration to your main css file.

JPC
  • 593
  • 6
  • 4
8

You can comment the whitespace out.

Original answer from 2013

Like:

<span>Text</span><!--

--><span>Text 2</span>

Edit 2016:

I also like the following method, where you just put the closing bracket right before the following element.

<span>Text</span

><span>Text 2</span>
jakub_jo
  • 1,087
  • 11
  • 20
1

Also you can do it like this (which IMHO,I believe is sintatically correct)

<div class="div1">...</div>
<div class="div1">...</div>
.
.

.div1{
    display:inline-block;
}
.div1::before, div1::after { white-space-collapse:collapse; }
  • 2
    Can you provide documentation for `white-space-collapse`? I don't believe this is a valid property. – roydukkey Oct 22 '14 at 08:57
  • @roydukkey Hi! By the time I answered, I had found it here [link](http://www.w3.org/TR/2007/WD-css3-text-20070306/#white-space-collapse). But it slipped my mind it was still a draft. The property has been added as a draft to CSS TextLevel 4: [link](http://dev.w3.org/csswg/css-text-4/#white-space-collapsing). –  Dec 30 '14 at 19:20
  • I see. This is interesting, but I wish the solution is just to have browsers not behave as they currently do, to not have this goofy extra space. – roydukkey Dec 31 '14 at 03:50
  • Based on the latest from caniuse, this is not a valid property: `This CSS property (formerly known as white-space-collapse or white-space-collapsing) is not supported in any modern browser, nor are there any known plans to support it.` https://www.caniuse.com/#search=white-space-collapse – Webreality Apr 23 '19 at 18:02