There will be a 4-pixel wide space between these span elements:
span { display: inline-block; width: 100px; background-color: palevioletred; }
Foo Bar
Fiddle Demo
I know I can remove this space by removing the space between the span elements in the HTML:
Foo Bar
I'm looking for a CSS solution that doesn't involve:
For browsers that comply with the CSS3 standard, you can use the
white-space-collapsing:discard
attributeSee:http://www.w3.org/TR/2010/WD-css3-text-20101005/#white-space-collapsing
Alternatively, you can useflexboxto achieve many of the layouts you might have previously achieved using inline-block:https://css-tricks.com/snippets/css/a-guide-to -flexbox/
Since this answer has become quite popular, I'm rewriting it.
Let us not forget the actual question asked:
This problem can be solved using just CSS, but there is no completely stable CSS fix.
The solution I proposed in my original answer was to add
font-size: 0
to the parent element and then declare a reasonablefont-size
in the child element.http://jsfiddle.net/thirtydot/dGHFV/1361/
This works in the latest versions of all modern browsers. It works in IE8. It doesn't work in Safari 5, but works in Safari 6. Safari 5 is almost a dead browser (0.33%, August 2015).
Most issues related to relative font sizes are not complicated.
However, if you are free to change the HTML (most people are), this is a reasonable solution, but not one I would recommend.
As an experienced web developer, this is how I actually solved this problem:
Yes, that's it. I removed the spaces in the HTML between inline block elements.
It's easy. this is very simple. It works everywhere. This is a pragmatic solution.
You sometimes have to think carefully about the source of whitespace. Will spaces be added if I append another element using JavaScript? No, if you do it correctly.
Let's take a magical journey and learn about some different ways to remove spaces, using some new HTML:
You can do what I usually do:
http://jsfiddle.net/thirtydot/dGHFV/1362/
Or, do this:
Or, use comments:
Or if you use PHP or something similar:
Alternatively,you can evenomitcertainclosing tags entirely (supported by all browsers):
Now that I've bored you with "thirtydot's thousand different ways to remove spaces", I hope you've forgotten
font-size: 0
.