Strange pixels showing up D:
Hello! I was doing a project by myself and something strange is going on with this code:
HTML
CSS
I changed the background-color just to see that there are (exactly) 7 pixels below my image, and I honestly don't know where are those coming from. Maybe is some default setting of the page which generates the image on my HTML, but I'm not sure. Does someone know what's going on??
10 Replies
add
display: block
to your image and see if that fixes itMaybe is some default setting of the page which generates the image on my HTMLmost likel because neither div nor img have paddings or extra white space by default. check with the style inspector in your browser devtools where that extra 7px are comming from.
I tried with other images and that problem still persisted, but I added that line and it worked
how did you know that was the solution?
I've been around the block a few times 😉
Why does it work? I'm not really sure myself on the technicalities of it 😂, I'm sure someone here could explain
It's a good idea to add this to your resets stylesheet:
that's a great idea actually
i'll be applying it on my new projects from now on
thanks man!!
img is a inline element meaning it has display set to inline by default.
iirc, it's because it's a replaced element with display: inline, which I think puts a phantom text character in there, or at least generates a line where one could be. The extra bit of height is that phantom character. You can see it's an issue with having an inline element by setting
font-size
or line-height
to 0, it makes it go away as well (though display: block
is probably the better solution)if you use
vertical-align: bottom
you will get the same result as display: block
🙂many possible fixes! 😄
The reason setting displat to block is because by default the image element is inline, which means it has a line height. The line height is to give "breathing room" between lines of a paragraph and other inline elements. The default line height is 1.2, so 20% of the current font size is added to the bottom for breathing room.
Once you change the display to block the line height changes from "line space" to "minimum box height" and allows the image to be full-height and no bottom "space" added.