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 【阅读原文】。感谢您的支持!


CSS: A New Kind Of JavaScript Originally published on Medium . Those familiar with the web platform will be well-versed in its t...
滚动视差效果的实现及优化方案 前言 相信大家对页面滚动视差效果都有一定的了解,滚动视差就是利用多层背景加上利用鼠标拖动滚动条来形成一些像动画一样的视觉效果。 项目地址: 使用background-attachmen...
CSS Transition not working properly I have the following code: On another part of my HTML I can get the transition...
Grouping CSS Selectors & ::selection Why we can't grouping CSS like.. p::selection , p::-moz-selection {background:transparent;} Demo : ...
Unable to get CSS transitions to work with drop-do... To understand my situation better, please head over to this fiddle and take a look. The preview pane of the fidd...