Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use CSS to remove the space between images [duplicate]

Tags:

html

css

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> 
like image 685
steveo225 Avatar asked Oct 03 '11 23:10

steveo225


People also ask

How do I remove space between images in CSS?

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.

How can I remove space between two pictures?

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).


2 Answers

Make them display: block in your CSS.

like image 116
Daniel A. White Avatar answered Sep 20 '22 11:09

Daniel A. White


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.

like image 31
alex Avatar answered Sep 20 '22 11:09

alex