Whitespace in the HTML source code
In the HTML source code, When you have lines of text or images, or elements that are inline-block
, if there is any whitespace between them (blank spaces, tabs, or new lines), a single blank space character will be added between them when the page is rendered. For example, in the following HTML, a blank space will appear between each of the four pieces of content:
one
two
<img src="three.png"/>
<span style="display: inline-block;">four<span>
This is very helpful for lines of text, and for small images or HTML elements that appear inside a line of text. But it becomes a problem when inline-block
is used for layout purposes, rather than as way to add content inside a paragraph of text.
Removing the extra space
The safest, cross-browser way to avoid the extra 4px or so of space that's added between inline-block
elements, is to remove any whitespace in the HTML source code between the HTML tags.
For instance, if you have a ul
with 3 floated li
tags:
<-- No space, tabs, or line breaks between </li> and <li> -->
<ul>
<li>...</li><li>...</li><li>...</li>
</ul>
Unfortunately, this hurts the maintainability of the website. Besides making the code unreadable, it severely compromises the separation of data and formatting.
If another programmer comes along later and decides to put each li
tag on a separate line in the source code (unaware of why the tags were on the same line, or possibly running it through HTML Tidy and not even having a chance to notice any related HTML comments), suddenly the website has a formatting bug that may be difficult to identify.
Consider floating elements instead
The whitespace behavior strongly suggests that it may be inappropriate to use inline-block
for general-layout purposes, to use it for anything other than adding content inside the flow of a paragraph of text.
Plus, in some cases inline-block
content is very difficult to fully style and align, especially on older browsers.
Quick summary of other solutions
- Put the close tag on the same line as the next open tag, with no white space between them.
- Use HTML comments to fill all of the whitespace between the close tag and the next open tag (as @Arbel suggested).
- Add a negative left margin to each element (usually -3px or -4px, based on the font-size). I don't recommend this particular approach.
- Set the
font-size
for the container element to 0 or 0.01em. This doesn't work in Safari 5 (not sure about later versions), and it may interfere with Responsive Design websites, or any website that uses a font-size
unit other than px
.
- Remove whitespace-only text nodes from the container using JavaScript or jQuery. This doesn't work in IE8 and earlier, as text nodes are not created in those browsers when there's only whitespace between elements, though space is still added between the elements.
- Set
letter-spacing
and word-spacing
for the container (as @PhillipWills suggested). Further info. This requires standardizing em
sizes on the website, which may not be a reasonable option for all websites.
- Add
text-space-collapse: discard;
to the container (previously called white-space-collapse
). Unfortunately, this CSS3 style is not yet supported by any browsers, and the standard hasn't been fully defined.