Aperture
Aperture

Reputation: 2427

How do I get rid of the space between <img> elements in a row in a html page?

I'm displaying 3 <img> in a row like this:

<div style="width: 950px">

                <img src='/UploadedImages/86.jpg' alt='' style="width: 300px; margin: 0px; padding: 0px; border: 1px solid Black" />

                <img src='/UploadedImages/85.jpg' alt='' style="width: 300px; margin: 0px; padding: 0px; border: 1px solid Black" />

                <img src='/UploadedImages/84.gif' alt='' style="width: 300px; margin: 0px; padding: 0px; border: 1px solid Black" />

        </div>

As you can see I have thin black borders around the images. My problem is that there are white spaces about 5px wide between the borders of neighbouring images and I have set the margin to be 0px but it does not work. So what is happening here?

Upvotes: 3

Views: 9536

Answers (4)

Bob
Bob

Reputation: 1679

You can get rid of the gaps by setting the font-size on the container to zero ;

CSS

.imgContainer { 
    font-size: 0px ; 
    white-space: nowrap; 
}

HTML

<div class="imgContainer">
    <img src="img1.png" />
    <img src="img2.png" />
    <img src="img3.png" />
</div>

Upvotes: 2

Gemma
Gemma

Reputation: 124

Wrap images in an ul and float the li:

<div style="width: 950px">
 <ul>
  <li><img src='/UploadedImages/86.jpg' alt='' /></li>
  <li><img src='/UploadedImages/85.jpg' alt='' /></li>
  <li><img src='/UploadedImages/84.jpg' alt='' /></li>
 </ul>
</div>

and then we add display:block; to img css:

 ul li {
 float:left;
 display:inline;
 list-style:none;
 }
 ul li img {
 border: 1px solid #000;
 display:block;
 padding: 0;
 width: 300px;
 }

jFiddle demo

Upvotes: 1

Floern
Floern

Reputation: 33914

You have to remove the whitespaces (linebreaks & spaces) between the tags:

<img src='/UploadedImages/86.jpg' alt='' style="..." /><img src='/UploadedImages/85.jpg' alt='' style="..." />

or make comments to keep linebreaks:

<img src='/UploadedImages/86.jpg' alt='' style="..." /><!--

--><img src='/UploadedImages/85.jpg' alt='' style="..." />

Upvotes: 16

jeroen
jeroen

Reputation: 91792

An img is an inline element (sort of...) so spaces and new-lines are displayed in your div. To avoid that you can float the images but then you would have to add an overflow to the container as well:

div {
  overflow: hidden;
}
div img {
  float: left;
}

The overflow on the container is needed so that the container encapsulates the images. Otherwise it will collapse.

Upvotes: 7

Related Questions