Jez D
Jez D

Reputation: 1489

How do I get rid of the white space under and above image

I have an image which has a border around it and the code looks like this:

img{
 display: block;
 border: 2px solid #000;
 height: 140px;
 width: 200px;
 padding: 0;
}

<img src="pathtoimage.png" alt="product name" />

I get a small 1px high white gap between the upper and lower borders and the image. I have tried putting the image in a container and setting the border to the container, but the gap still appears. I have tried setting position to absolute and relative, but they didn't work. I have tried using css3's box-size, but that didn't work either.

Any suggestions please.

Upvotes: 1

Views: 1722

Answers (2)

Anders Arpi
Anders Arpi

Reputation: 8397

Your code should work as is. Here's a demo.

I think the problem is your actual picture. Are you 100% sure it doesn't have some small white border in the image itself? Try to replace pathtoimage.png with http://placehold.it/350x150 and see if the white edges are gone.

Lastly, try setting padding: 0 !important; to make sure it isn't inheriting padding from somewhere else.

Upvotes: 2

Ren&#233; Wolferink
Ren&#233; Wolferink

Reputation: 3548

There are three types of edges around elements: padding, margin and border.

Add:

margin: 0;

Here is an image explaining which is which:

http://www.avajava.com/tutorials/cascading-style-sheets/how-are-margins-borders-padding-and-content-related/how-are-margins-borders-padding-and-content-related-01.gif

Upvotes: 0

Related Questions