Displaying Images as Block to Avoid Line-Height Problem
It's easy to forget that images default to inline elements. In most cases, this is not a big deal because the image is larger than the bounds of any line height restrictions. However, when the image is shorter than the applied line height style, the image might not render exactly where you would expect. For example, I was trying to place a 4 pixel tall image within a 4 pixel tall div sized to the exact dimensions of the image. But the image was rendering about 10 pixels below the top of the div. After about an hour of debugging the layout, I finally realized that the default line height for the page was set to 21 pixels. Could that be the issue? Yes! At first I thought maybe I could just lower the line height of the div containing the image down to 1px. That seemed to work, but it felt like a hack and the image was off by about a pixel. This morning in the shower – yes, where all good ideas come to me – I came to the conclusion that the problem wasn't the line height, it was the fact that the image was inline. Changing the display value to block fixed everything. Yeah, I learned something new today. IFS