Table of Contents
Why does the source order affect fallback behavior?
How to correctly arrange the source order?
What are the common problems with fallback behavior?
Let's summarize
Home Web Front-end HTML Tutorial HTML `picture` Source Order and Fallbacks

HTML `picture` Source Order and Fallbacks

Jul 24, 2025 am 01:18 AM

When the browser parses the picture elements, matches in the source order, and finds the first supported resource loading, otherwise it falls back to img. Because the browser checks from top to bottom whether each source meets the criteria, it stops once it matches, and neither the subsequent source nor the img will be loaded. The correct order should be to place the loaded format in the front, such as AVIF, WebP, and alternative formats such as JPG, PNG, and img as the final guarantee. Common problems include img without src, source type error or path error, causing fallback to fail. It is recommended to verify the source order, check the img path, and use developer tools to debug resource loading.

HTML `picture` Source Order and Fallbacks

When the browser parses the HTML picture element, it will determine whether it supports it in the order of the <source></source> tags you wrote in the code until a matching resource is found. If none of them match, the contents that are fallback to <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175329111415127.jpeg" class="lazy" alt="HTML `picture` Source Order and Fallbacks" > tag will eventually fallback.

HTML `picture` Source Order and Fallbacks

Therefore, the order of source is very important , and the order determines which resource the browser prefers to load.


Why does the source order affect fallback behavior?

The way <picture></picture> works is to check "top to bottom" whether each <source></source> meets the support conditions for the current device and browser. Once a <source></source> matches (such as media query is established, or the format is supported), the browser will use it and ignore the subsequent <source></source> and <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175329111661004.jpeg" class="lazy" alt="HTML `picture` Source Order and Fallbacks" > .

HTML `picture` Source Order and Fallbacks

If you put unsupported formats or inappropriate media queries in front of it, the browser may pick it up and keep searching. But if there is no suitable one in the future, then <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175329111891961.jpeg" class="lazy" alt="HTML `picture` Source Order and Fallbacks" > will be used.

For example:

HTML `picture` Source Order and Fallbacks
 <picture>
  <source srcset="image.avif" type="image/avif">
  <source srcset="image.webp" type="image/webp">
  <img src="/static/imghw/default1.png"  data-src="image.jpg"  class="lazy" alt="Fallback image">
</picture>

In this example, if the browser supports AVIF, it will load the AVIF image; if AVIF is not supported but WebP is supported, it will load the WebP; if none is supported, it will display JPG.

But if the writing is reversed:

 <picture>
  <source srcset="image.jpg" type="image/jpeg">
  <source srcset="image.webp" type="image/webp">
  <img src="/static/imghw/default1.png"  data-src="image.avif"  class="lazy" alt="Fallback image">
</picture>

That will be preferred to load JPG even if the browser supports AVIF and WebP, because it is the first matching type. And the AVIF in the <img alt="HTML `picture` Source Order and Fallbacks" > tag will not be used.


How to correctly arrange the source order?

To ensure that the browser can load the format you expect first, you should write it in the order of “modern to traditional” or “efficient to universal”:

  • <source></source> placed in front should be the format you want to load first (such as AVIF, WebP)
  • <source></source> placed behind is an alternative format (such as JPG, PNG)
  • The final <img alt="HTML `picture` Source Order and Fallbacks" > is the bottom-up plan

This ensures:

  • Browsers that support new formats can load smaller images
  • Unsupported browsers can also display pictures normally

What are the common problems with fallback behavior?

Sometimes, even if you write fallback, the picture is not loaded. Common reasons include:

  • <img alt="HTML `picture` Source Order and Fallbacks" > No src attribute is written
  • type of <source></source> is written incorrectly (for example, it is written as image/jpg , but it is actually image/jpeg )
  • All <source></source> does not match, but <img alt="HTML `picture` Source Order and Fallbacks" > path error or resource does not exist

So suggestion:

  • Always provide a reliable src for <img alt="HTML `picture` Source Order and Fallbacks" >
  • Check loaded resources and request status with browser developer tools
  • Test the performance of different browsers, especially mobile and old browsers

Let's summarize

  • The browser matches <source></source> from top to bottom in order
  • The first match <source></source> will be used, and the following will be ignored
  • <img alt="HTML `picture` Source Order and Fallbacks" > will be loaded only if all <source></source> does not match
  • The correct order is: put the loaded format first, and put the compatible format behind.
  • Fallback is often invalid because of path errors, format errors or misorder

Basically that's it. The order looks simple, but if you make a mistake, it can easily cause the image to fail to load or the format that should not be loaded.

The above is the detailed content of HTML `picture` Source Order and Fallbacks. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1583
276
Essential HTML Tags for Beginners Essential HTML Tags for Beginners Jul 27, 2025 am 03:45 AM

To get started with HTML quickly, you only need to master a few basic tags to build a web skeleton. 1. The page structure is essential, and, which is the root element, contains meta information, and is the content display area. 2. Use the title. The higher the level, the smaller the number. Use tags to segment the text to avoid skipping the level. 3. The link uses tags and matches the href attributes, and the image uses tags and contains src and alt attributes. 4. The list is divided into unordered lists and ordered lists. Each entry is represented and must be nested in the list. 5. Beginners don’t have to force memorize all tags. It is more efficient to write and check them while you are writing. Master the structure, text, links, pictures and lists to create basic web pages.

What is the name attribute in an input tag for? What is the name attribute in an input tag for? Jul 27, 2025 am 04:14 AM

Thenameattributeinaninputtagisusedtoidentifytheinputwhentheformissubmitted;itservesasthekeyinthekey-valuepairsenttotheserver,wheretheuser'sinputisthevalue.1.Whenaformissubmitted,thenameattributebecomesthekeyandtheinputvaluebecomesthevalueinthedatasen

Can you put a  tag inside another  tag? Can you put a tag inside another tag? Jul 27, 2025 am 04:15 AM

❌Youcannotnesttagsinsideanothertagbecauseit’sinvalidHTML;browsersautomaticallyclosethefirstbeforeopeningthenext,resultinginseparateparagraphs.✅Instead,useinlineelementslike,,orforstylingwithinaparagraph,orblockcontainerslikeortogroupmultipleparagraph

Shadow DOM Concepts and HTML Integration Shadow DOM Concepts and HTML Integration Jul 24, 2025 am 01:39 AM

ShadowDOM is a technology used in web component technology to create isolated DOM subtrees. 1. It allows the mount of an independent DOM structure on ordinary HTML elements, with its own styles and behaviors, and does not affect the main document; 2. Created through JavaScript, such as using the attachShadow method and setting the mode to open; 3. When used in combination with HTML, it has three major features: clear structure, style isolation and content projection (slot); 4. Notes include complex debugging, style scope control, performance overhead and framework compatibility issues. In short, ShadowDOM provides native encapsulation capabilities for building reusable and non-polluting UI components.

HTML `style` Tag: Inline vs. Internal CSS HTML `style` Tag: Inline vs. Internal CSS Jul 26, 2025 am 07:23 AM

The style placement method needs to be selected according to the scene. 1. Inline is suitable for temporary modification of single elements or dynamic JS control, such as the button color changes with operation; 2. Internal CSS is suitable for projects with few pages and simple structure, which is convenient for centralized management of styles, such as basic style settings of login pages; 3. Priority is given to reuse, maintenance and performance, and it is better to split external link CSS files for large projects.

How to embed a PDF document in HTML? How to embed a PDF document in HTML? Aug 01, 2025 am 06:52 AM

Using tags is the easiest and recommended method. The syntax is suitable for modern browsers to embed PDF directly; 2. Using tags can provide better control and backup content support, syntax is, and provides download links in tags as backup solutions when they are not supported; 3. It can be embedded through Google DocsViewer, but it is not recommended to use widely due to privacy and performance issues; 4. In order to improve the user experience, appropriate heights should be set, responsive sizes (such as height: 80vh) and PDF download links should be provided so that users can download and view them themselves.

How to create an unordered list in HTML? How to create an unordered list in HTML? Jul 30, 2025 am 04:50 AM

To create an HTML unordered list, you need to use a tag to define a list container. Each list item is wrapped with a tag, and the browser will automatically add bullets; 1. Create a list with a tag; 2. Each list item is defined with a tag; 3. The browser automatically generates default dot symbols; 4. Sublists can be implemented through nesting; 5. Use the list-style-type attribute of CSS to modify the symbol style, such as disc, circle, square, or none; use these tags correctly to generate a standard unordered list.

How to use the contenteditable attribute? How to use the contenteditable attribute? Jul 28, 2025 am 02:24 AM

ThecontenteditableattributemakesanyHTMLelementeditablebyaddingcontenteditable="true",allowinguserstodirectlymodifycontentinthebrowser.2.Itiscommonlyusedinrichtexteditors,note-takingapps,andin-placeeditinginterfaces,supportingelementslikediv

See all articles