惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
博客园_首页
MyScale Blog
MyScale Blog
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
F
Full Disclosure
V
V2EX
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
PCI Perspectives
PCI Perspectives
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
Help Net Security
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
Microsoft Azure Blog
Microsoft Azure Blog
K
Kaspersky official blog
G
GRAHAM CLULEY
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
Tor Project blog
Cloudbric
Cloudbric
Hacker News - Newest:
Hacker News - Newest: "LLM"
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

Chip Cullen

The need for importance, and AI: Chip Cullen An updated Colorosetta: Chip Cullen The Return of the Font Combinator!: Chip Cullen Changing the number of an item in an ordered list: Chip Cullen My pizza dough recipe as of May 2025: Chip Cullen Gonna try to be a bit more personal: Chip Cullen How I built dynamic social media images in Eleventy using Cloudinary: Chip Cullen My current approach to AI : Chip Cullen Lessons Learned Surviving a Major Product Launch: Chip Cullen How to Build a Drop Down Menu with Modern CSS: Chip Cullen How to stop page scrolling when you have an open dialog element: Chip Cullen Distraction Driven Development: Chip Cullen How I learned to code: the art of letting go: Chip Cullen In praise of the switch statement: Chip Cullen Project stuck? Think about how you’re breaking it down & question everything: Chip Cullen So how did the onboarding experiment go?: Chip Cullen Ideas for an Onboarding Checklist: Chip Cullen I really like Post Mortems: Chip Cullen Raise Red Flags Early: Chip Cullen How to mock fetch requests in React Testing Librarty tests: Chip Cullen Running a Structured Meeting: Chip Cullen Adding the View Transitions API to my personal site: Chip Cullen A Lightweight Way to Read GraphQL Data: Chip Cullen How to make a color changing favicon: Chip Cullen Using a Pros/Cons list to help navigate technical discussions: Chip Cullen How to use variable fonts from Google Fonts: Chip Cullen A new website: now on Eleventy!: Chip Cullen How to Truncate Type at More Than One Line with Just CSS: Chip Cullen Colorosetta: the VS Code Extension!: Chip Cullen Using CSS Custom Properties and Logical Properties Together: Chip Cullen Browser Dev Tools: Element Inspector Popover: Chip Cullen The Link with rel=preload is a Seperate Thing: Chip Cullen How to have Dark & Light Mode Images that also works with User Choice: Chip Cullen Don’t use Viewport Units for Font Size on their own: Chip Cullen A little known Media Query: Aspect Ratio: Chip Cullen Meta thinking: Managing Decisions: Chip Cullen Give Your To-Do's Context: Chip Cullen Say What the Impact is when Reporting Issues: Chip Cullen Firefighting 101: How to Manage Breakages: Chip Cullen How to Deal With Large Pieces of Technical Debt: Chip Cullen Make Your Request Clear: Chip Cullen Analytics events, HTML classes, and protecting against refactoring: Chip Cullen How We Removed jQuery from a large app: Chip Cullen New tool: ColoRosetta: Chip Cullen Django 3.1 gotcha: Referrer Policy has a new default, and it might break iframes and links: Chip Cullen A Javascript Component Pattern: Chip Cullen CSS min(), max() and clamp() Functions: Chip Cullen Pointer Events and Inline Elements in Chrome: Chip Cullen Resolving a github repo and a new Create React App: Chip Cullen How to POST *Data* with the Fetch API: Chip Cullen The Contrast Triangle: Chip Cullen Advice on interviewing for Junior Developers: Chip Cullen Life Lessons Learned From Running a Marathon: How to do something really hard: Chip Cullen A (Brief) intro to Search Engine Structured Data: Chip Cullen Javascript Fallback Values on Variables and Booleans - a hard lesson: Chip Cullen Alfred Tip: Quickly Access Common URLs: Chip Cullen Responsive Images in Hugo - by Laura Kalbag: Chip Cullen Making a Gatsby Site with Multiple Content Types: Chip Cullen How to Create and Use Fixtures in Cypress Tests: Chip Cullen Fixing the 'Bad Interpreter' Error from AWS and Python 3.7: Chip Cullen Creating a Canonical Tag in a Django Template: Chip Cullen Responsive spacing with viewport and ch units: Chip Cullen Welcome to my New Design - 2019: Chip Cullen Django Templates: Block and If statements don’t work like you might expect: Chip Cullen Books I Read in 2018: Chip Cullen Lifehack: 4 ways to help tame common email noise: Chip Cullen How to make better Pull Requests: Adding Steps to Test: Chip Cullen The unsung develpment tool: Spreadsheets: Chip Cullen Troubleshooting Adding and Removing EventListeners: with Arguments, Debounced, and in a React Class: Chip Cullen How to Fake the Window Object in Jest and Enzyme: Chip Cullen Migrating From Wordpress to Hugo: Chip Cullen Background Repeat and its Possibilities: Chip Cullen Getting Started With Front End Tests: a Mindset: Chip Cullen Migrating a Blog - An Opportunity for a Content Inventory: Chip Cullen Moving to Hugo: Chip Cullen JavaScript events: .target vs .currentTarget: Chip Cullen Things I wish I knew when starting with Python: Chip Cullen Leading Ampersands for modifiers in Sass: An anti-pattern: Chip Cullen How to get rid of the "You have mail" message in your terminal: Chip Cullen Why three typefaces rule the web, and what you can do about it: Chip Cullen You shouldn't worry about Section 508 - it's Section 504: Chip Cullen Looping Video Backgrounds: pointers and pitfalls: Chip Cullen How to “preview” a click event tag in the Google Tag Manager console: Chip Cullen Moving on from a technology, or: life after Drupal: Chip Cullen Don’t be a dumb developer: Chip Cullen Two level breadcrumbs with CSS :only-child: Chip Cullen Simplicity comes with experience: Chip Cullen Do the least amount possible: Chip Cullen SVGs vs. Icon Fonts: Two points in favor of Icon Fonts: Chip Cullen Accessible links without underlines: Chip Cullen The Strategic Job Hunt: Chip Cullen Surviving Getting Laid Off: Chip Cullen How to structure your typography in Sass: Chip Cullen Layer Cake: A Responsive Design Layout Pattern: Chip Cullen Creativity is yet to come in Web Design: Chip Cullen Front End Testing with Wraith: A Step by Step Recipe: Chip Cullen Where to begin? How I start a visual design for the web: Chip Cullen If you could only have five Google Fonts: Chip Cullen Why SVG is so cool (or: what happens when you're late to the party on something): Chip Cullen How to apply classes to elements with CKEditor 4, in Drupal 7: Chip Cullen
What width and height attributes should you use with responsive images?: Chip Cullen
2020-10-31 · via Chip Cullen

Best practice is now to re-include the width and height attributes for images, like we used to back in the day.

Who knew? I recently found out that browsers were encouraging this.

It reduces what Google has labeled Cumulative Layout Shift, which is a fancy way of saying jank. They measure that as an indicator of performance.

If an image is like this:

<!-- how we've done it in the era of responsive design -->
<img src="path/to/image.jpg" alt="alt text" />

A browser doesn't know what to do with that when laying out the page. It's when an image downloads that it knows what space it's supposed to take up. This leads to the page shift around as images download. Not good.

What you can do to help a browser, then, is to add width and height set to two values that are equal to the image's aspect ratio. Aspect ratio is the ratio of an images width compared to it's height. Now, if this image has one size, that's pretty straight forward:

<!-- Let's assume this image is 640 x 360 (i.e. 16:9) - NOTE - no units -->
<img src="path/to/image.jpg" width="640" height="360" alt="alt text" />

You can still make this image behave responsively with the same CSS that we've used for years:

img {
	width: 100%;
	height: auto;
}

Responsive Image attributes

I don't know about you, but the majority of images I deal with on my work projects these days involve multiple resolutions and image formats.

<!-- taken from a real project -->
<picture>
	<!-- for browsers that support webp -->
    <source
      type="image/webp"
      sizes="(max-width: 767px) 100vw,
            (max-width: 1023px) calc(33vw - 42px),
            596px"
      srcset="image_218x122.webp 218w,
              image_280x157.webp 280w,
              image_384x215.webp 384w,
              image_442x247.webp 442w,
              image_480x270.webp 480w,
              image_767x432.webp 767w,
              image_960x540.webp 960w,
              image_1534x864.webp 1534w">
    <img
      sizes="(max-width: 767px) 100vw,
              (max-width: 1023px) calc(33vw - 42px),
              596px"
      srcset="image_218x122.jpg 218w,
              image_280x157.jpg 280w,
              image_384x215.jpg 384w,
              image_442x247.jpg 442w,
              image_480x270.jpg 480w,
              image_767x432.jpg 767w,
              image_960x540.jpg 960w,
              image_1534x864.jpg 1534w"
      src="image_448x250.jpg"
      alt="Alt text"
      />
</picture>

Which attributes should we use? In the web.dev guidance, they say:

To ensure width and height attributes can be set, each image should use the same aspect ratio.

Okay, fair enough - in the above example all the images do share the same aspect ratio - 16:9.

But it's still not clear if any one of the above resolutions is preferable.

At first, I tried this:

<picture>
	...
    <img
      sizes="..."
      srcset="..."
      src="image_448x250.jpg"
      width="16"
      height="9"
      alt="Alt text"
      />
      <!-- this is a 16:9 image, after all -->
      <!-- NOTE: don't do this -->
</picture>

I liked this for these reasons:

  • It made the aspect ratio clear
  • It separated this layout concern from the actual size of the image
  • It indicates that CSS is getting involved in the actual layout

In a more a more in depth article by Craig Buckler, he advises against doing this:

Any appropriate img width and height can be used since the image is resized by CSS (e.g., width="4" height="3". However, it is best to set a reasonable size to ensure the image remains visible in very old browsers, if CSS fails to load, or when a user style sheet is defined.

Which, fine, good point.

Ah, but what is "… a reasonable size" ?

My first instinct is whatever the size is that you would use for the default src.

But is that the best size?

In our case, we typically set the src to be a slightly-less-than-middle-of-the-road size. Smaller resolutions and larger resolutions are available. This feels squirrelly to me.

<picture>
	...
    <img
      sizes="..."
      srcset="..."
      src="image_448x250.jpg"
      width="448"
      height="250"
      alt="Alt text"
      />
      <!-- Using the default src dimensions -->
</picture>

After having given it some thought, though, I think my first instinct is correct.

Set width and height to the resolution that corresponds to your default src.

You have no reason not to, and I don't think there is a better alternative. What matters with these attributes is that the ratio is correct.

Assuming you have responsive image CSS in place, these attributes will have influence on the final rendered size of the image only if your CSS fails or if the user has overridden it with a local stylesheet. In either of those cases, the default size should be a usable experience anyway.