Remove white space below image
Solution 1
You're seeing the space for descenders (the bits that hang off the bottom of 'y' and 'p') because img
is an inline element by default. This removes the gap:
.youtube-thumb img { display: block; }
Solution 2
You can use code below if you don't want to modify block mode:
img{vertical-align:text-bottom}
Or you can use following as Stuart suggests:
img{vertical-align:bottom}
Solution 3
If you would like to preserve the image as inline you can put vertical-align: top
or vertical-align: bottom
on it. By default it is aligned on the baseline hence the few pixels beneath it.
Solution 4
I've set up a JSFiddle to test several different solutions to this problem. Based on the [vague] criteria of
1) Maximum flexibility
2) No weird behavior
The accepted answer here of
img { display: block; }
which is recommended by a lot of people (such as in this excellent article), actually ranks fourth.
1st, 2nd, and 3rd place are all a toss-up between these three solutions:
1) The solution given by @Dave Kok and @Hasan Gursoy:
img { vertical-align: top; } /* or bottom */
pros:
- All display values work on both the parent and img.
- No very strange behavior; any siblings of the img fall where you'd expect them to.
- Very efficient.
cons:
- In the [perfectly valid] case of both the parent and img having `display: inline`, the value of this property can determine the position of the img's parent (a bit strange).
2) Setting font-size: 0;
on the parent element:
.parent {
font-size: 0;
vertical-align: top;
}
.parent > * {
font-size: 16px;
vertical-align: top;
}
Since this one [kind of] requires vertical-align: top
on the img
, this is basically an extension of the 1st solution.
pros:
- All display values work on both the parent and img.
- No very strange behavior; any siblings of the img fall where you'd expect them to.
- Fixes the inline whitespace problem for any siblings of the img.
- Although this still moves the position of the parent in the case of the parent and img both having `display: inline`, at least you can't see the parent anymore.
cons:
- Less efficient code.
- This assumes "correct" markup; if the img has text node siblings, they won't show up.
3) Setting line-height: 0
on the parent element:
.parent {
line-height: 0;
vertical-align: top;
}
.parent > * {
line-height: 1.15;
vertical-align: top;
}
Similar to the 2nd solution in that, to make it fully flexible, it basically becomes an extension of the 1st.
pros:
- Behaves like the first two solutions on all display combinations except when the parent and img have `display: inline`.
cons:
- Less efficient code.
- In the case of both the parent and img having `display: inline`, we get all sorts of crazy. (Maybe playing with the `line-height` property isn't the best idea...)
So there you have it. I hope this helps some poor soul.
Solution 5
I found this question and none of the solutions here worked for me. I found another solution that got rid of the gaps below images in Chrome. I had to add line-height:0;
to the img selector in my CSS and the gaps below images went away.
Crazy that this problem persists in browsers in 2013.
Related videos on Youtube
Comments
-
Ryan almost 2 years
In Firefox only my video thumbnails are displaying mysterious 2-3 pixels of white space between the bottom of my image and its border (see below).
I've tried everything I can think of in Firebug with no luck.
How can I remove this white space?
-
Michael Benjamin almost 9 yearsHere's a short and complete answer to this question: stackoverflow.com/a/31445364/3597276
-
Davide Andrea over 4 yearsEasiest solution that doesn't not have the drawbacks of "display: block;" is to set the font size of the parent element to 0: <div style=" font-size: 0;"><img ...></div>
-
-
Salman A over 11 yearsWorks, but not good. I have 4 images in a div and I want them
text-align: center
which won't work for blocks. -
Brilliand over 11 yearsUse
vertical-align: middle
if you need the image to play nicely with other things in the same box. In the rare case that the box contains several tiny images and no text, you might need to setfont-size: 0
on the containing box. -
user3479901 about 11 years
text-bottom
still leaves another pixel below the image in Chrome. I would suggestvertical-align:bottom
. -
Brilliand about 11 yearsThe first person to give this solution was Dave Kok, if you don't count my comment on the accepted answer (from a month earlier).
-
DS. almost 11 yearsThis should be the accepted answer, as it doesn't change the default display property of the img tag which might need to be 'fixed' in other places later!
-
Benjamin Intal almost 11 yearsNvm, found why this works: salman-w.blogspot.com/2012/10/…
-
Doug S over 10 yearsThat works, but I used the other answer below from Hasan Gürsoy, since his answer doesn't cause problems with
text-align:center
and other issues that occur when changing the display property. -
Doug S over 10 yearsThank you! Although I went with the
vertical-align:bottom
version instead. -
Zantier almost 10 yearsWhen the image is particularly small (shorter than the line-height of the parent block element), vertical-align:bottom will leave a gap above the image. Hopefully it's clear that this is expected: developer.mozilla.org/en-US/docs/Web/CSS/line-height "On block level elements, the line-height CSS property specifies the minimal height of line boxes within the element."
-
Hooman Askari almost 9 yearsNicely done. You actually need to set the image to be inline-block too to make it work in most browsers.
-
Hooman Askari almost 9 yearsNicely done. You actually need to set the image to be inline-block too to make it work in most browsers.
-
VirtualWolf over 8 yearsYep,
line-height: 0;
is what worked for me too. It's still alive and well in 2015. -
Jamie Carl over 8 yearsThis worked perfectly for me. Way better than display block. Thanks.
-
Nathan Hornby about 6 yearsIt removes the gap but also makes it a block element. It's far from an assumption that changing the display type wouldn't have other drastic effects on a layout.
-
robertc about 6 years@NathanHornby No assumptions are necessary, the layout was in the question (so was the code, originally)
-
benface over 5 yearsThis "problem" persists because it's not a bug. It's just how the inline-formatting context works.
-
Dee over 4 yearsoh no, 'block' will take the whole line
-
Dee over 4 yearsyeah, vertical-align:bottom works better