HTML image dimensions with a CSS border

综合技术 2018-05-18 阅读原文

When I use images here, I tend to give ones without any transparency a border, which is done using CSS and applied to img tags unless they have a no-border class.

Like a good web citizen, I also specify image dimensions in HTML :

“The image’s rendered size is given in the width and height attributes, which allows the user agent to allocate space for the image before it is downloaded.”

In fact my BBEdit image snippet makes it a doddle:

<p >

But this causes a problem, which I’ve spotted in a couple of my recent posts.

If you specify the image dimensions, and use a CSS border, and have your CSS box-sizing set to border-box , then the CSS border shrinks the amount of space available to the image to its specified dimensions − 2 × the border width.

So if you specify your img dimensions to match the dimensions of the file, then the image itself will be shrunk within the element.

This animation shows this situation, and what happens when you toggle the CSS border. Watch what happens to the image itself.

(It’s got a slight offset from the text because it’s a screenshot of this blog and includes some of the background on each side.)

In contrast, this animation shows what happens when the dimensions are not specified, and so the image is free to grow when the border is applied:

Really the culprit here is box-sizing: border-box , forcing the border to remain within the size of the img element itself. This is a behaviour you actually want, as it solves the old CSS problem of juggling widths, borders and padding within a parent element. Check out MDN’s box-sizing page to see what I mean.

What are my options, then?

  • Change box-sizing.

    I’m not touching this because the potential sizing headaches are not worth it, even just for img elements.

  • Apply a border to the image files themselves.

    No, because if I change my mind about the CSS, previously posted images are stuck with the old style forever. CSS borders should also work correctly across high-density displays, whereas a 1px border in the file may not.

  • Don’t specify dimensions in the HTML.

    I don’t like the idea of making pages of this site slower to render, but I think this is the least bad option, particularly given that this site is already pretty fast .

It’s not ideal, but that BBEdit snippet is now just:

<p >

Hey, at least it makes images quicker to include in posts!

责编内容by:Primary Unit 【阅读原文】。感谢您的支持!


Issues related to the Datepicker navigation icons Before I posted here, I searched through other answers and made sure about the images folders being in the cc folder, etc. I STILL have a problem t...
FullCalendar dividing lines between days insert a vertical dividing line between two nested divs, not full height I have float left and float right nested within a ligh...
Smashing Book 6 Released The hotly anticipated Smashing Book 6: New Frontiers in Web Design , is released today. Dedicated to some of the thorniest problems we face in our pr...
Unable to center images (css) .photospace .slideshow-container { position: relative; clear: both; height: 450px; i have a slideshow with various image sizes but non b...
Why does this CSS hover transition fail in FireFox... I've posted a simple example here: I know FF needs to have initial values set for the transition properties....