What if we got aspect-ratio sized images by doing almost nothing?
Solve image loading stutter: use browser native features to achieve seamless image display
Have you ever encountered a situation where web page images load slowly, causing the page to stutter? This is usually because the browser does not know the aspect ratio of the image, so it cannot reserve enough space to display the image. This article will explore an elegant solution that takes advantage of browser native features to solve this problem without much extra code.
Suppose you are using an 800x600 pixel image in a web page and set its width to width: 100%;
. The actual display width of the image will vary according to the container size, but its aspect ratio remains the same. The problem is that the browser cannot determine the height of the image before the image is loaded, which will cause the page to stutter.
Solution: Take advantage of width
and height
properties
Surprisingly, just <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174511723380054.jpg" class="lazy" alt="What if we got aspect-ratio sized images by doing almost nothing?">
Going further: Browser native style support
A more ideal solution is to make the browser itself support this feature. One way is to take advantage of aspect-ratio
attribute of CSS, but this requires developers to add it manually, which is not convenient enough. A better solution is to add the default aspect ratio settings to the browser user agent stylesheet, for example:
img, video { aspect-ratio: attr(width) / attr(height); }
In this way, as long as the image has width
and height
attributes, the browser can automatically calculate and reserve the correct space to avoid load lag.
The advantages of this approach are:
- Easy to understand and implement: No need to learn new attributes or complex CSS tricks.
- Wide applicability: Many websites already use
width
andheight
attributes without modifying existing code. - Developer friendly: New and old developers can easily understand and use it.
Currently, some browsers are testing the feasibility of this solution. This will greatly simplify web development and enhance user experience.
The above is the detailed content of What if we got aspect-ratio sized images by doing almost nothing?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
