Given:
<img src="..."/> <img src="..."/>
The result is two images with a single space between them. It seems that the normal behavior is to show any number of spaces, newlines, and tabs as a single white space. I know I can do the following:
<img src="..."/><img src="..."/>
OR
<img src="..."/><!-- --><img src="..."/>
OR
<img src="..."/ ><img src="..."/>
Or any number of other hacks. Is there a way to remove that whitespace with CSS? e.g.
<div class="nospace"> <img src="..."/> <img src="..."/> </div>
By default, images are displayed as inline elements. Setting them to block elements will remove the space between them and put them on their own line. Then, setting their margin to margin: 0 auto; will center them, for example.
To remove the unwanted space between images or similar HTML elements do one of the following: Put all the elements on one line with no spaces between them. Put the closing bracket of the previous element immediately before the next element on the next line. Comment out the end of line marker (carriage return).
Make them display: block
in your CSS.
An easy way that is compatible pretty much everywhere is to set font-size: 0
on the container, provided you don't have any descendent text nodes you need to style (though it is trivial to override this where needed).
.nospace { font-size: 0; }
jsFiddle.
You could also change from the default display: inline
into block
or inline-block
. Be sure to use the workarounds required for <= IE7 (and possibly ancient Firefoxes) for inline-block
to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With