About this article
This article was created using an automated generation workflow utilizing generative AI. Based on the HTML Standard and MDN picture and currentSrc specifications, it explains image candidates selected by screen width, following the implementation of an existing demo. Screen operations on GitHub Pages could not be verified from this environment.Verification Status: 📘 Official information verified, browser hardware not tested
Select images that clearly show the main subject on narrow screens | picture
When a landscape photo is scaled down to fit a smartphone width, the main subjects such as people or products also become tiny. Simply shrinking an image with CSS cannot crop the intended focal area to be larger. Using the HTML picture element allows you to select different compositions of the same meaning depending on conditions such as screen width.
Live demo switching compositions by screen width
- Change screen width to observe image candidates
- What the picture element selects
- Distinguishing between conditions for selecting images and conditions for selecting resolution
- Inspect the URL selected via currentSrc
- Include alternative text when using in production
- Checklist when images fail to switch
- Constraints and Safe Handling
- References
Change screen width to observe image candidates
Open the demo, press Run, and resize the screen width around 600 CSS px. On the wider side, an SVG image reading Full view is displayed, while on the narrower side, Close-up is displayed. The display width can be adjusted by specifying values in the developer tools responsive mode or by resizing the browser window.
| Display width | Matching candidate | Label visible in demo |
|---|---|---|
| Less than 600 CSS px | Narrow-screen image in img src | Close-up |
| 600 CSS px or greater | Full-view image in source srcset | Full view |
Move the width back and forth across the boundary several times to observe which candidate is selected. Exactly 600 CSS px matches the side meeting the min-width condition. Even when changing the browser zoom level, images may not switch unless the viewport size in CSS pixels crosses the boundary.
In this demo, you first press Run to generate the image, and then change the screen width. Since the image uses an SVG data URL within the code, no external photos are fetched. The text input field on the shared demo screen is hidden for this experiment. The condition changed by the reader is the CSS pixel width of the screen.
What the picture element selects
The picture element is not responsible for displaying the image itself. The source elements listed inside define image candidates and conditions, and the final img element renders the image. The browser evaluates the matching source as a candidate and selects the image URL from its srcset. If no candidates are available, the img src acts as a fallback.
The branching in the diagram indicates that the source-side image is considered as a candidate only when the width condition matches.
flowchart TD
A["画面幅を調べる"] --> B{"600 CSS px以上?"}
B -- "はい" --> C["sourceのsrcsetを選ぶ"]
B -- "いいえ" --> D["imgのsrcを使う"]
Simplifying the implementation yields the following structure:
<picture>
<source
media="(min-width: 600px)"
srcset="wide-scene.jpg"
>
<img
src="close-up.jpg"
alt="中央にある緑の製品"
>
</picture>
Here, the source is evaluated first. If the width is 600 CSS px or greater, wide-scene.jpg becomes a candidate; otherwise, the media condition of the source does not match, and close-up.jpg is used. The HTML Standard specifies a structure where zero or more source elements are placed inside picture, followed by an img element. Image URLs in picture source elements are specified using srcset. Unlike source elements in video or audio, the src attribute of a source within picture does not specify image candidates.
This usage is referred to as art direction. Instead of simply scaling the same photo up or down to fit the display area, the composition itself is changed to effectively convey the intended subject. For example, in portraits, a wide screen can show the surrounding environment, while a narrow screen can display a close-up of the face.
Distinguishing between conditions for selecting images and conditions for selecting resolution
The picture element is not exclusively used for switching compositions based on screen width. By utilizing source media and type attributes, candidates can be switched according to display conditions or image formats. On the other hand, when providing different resolutions for the same composition and letting the browser select the appropriate candidate for the device, it is standard practice to use img srcset and sizes.
| Goal | Primary attributes used | Relationship between images |
|---|---|---|
| Modify the subject composition on narrow screens | source media within picture | Different crop or alternative composition |
| Select image size for the same composition | img srcset, with sizes as needed | Same content with different resolutions |
| Switch formats based on supported formats | source type and srcset | Different formats with the same appearance |
Selecting a different composition and reducing the image file size are distinct optimizations. Export the candidate images at the correct size, and consider compression and lazy loading separately. Do not assume that simply switching to the picture element will automatically improve data transfer volume or rendering speed.
Inspect the URL selected via currentSrc
The currentSrc property of the img element returns the image URL selected by the browser for loading. While useful for checking the currently displayed candidate, the value of currentSrc alone does not confirm that the image has finished loading. Use the img load event to verify completion, and the error event to check for load failures.
The shared demo for this session specifies width conditions in the source media attribute, compares img.currentSrc with the URL of the full-view SVG, and outputs a message indicating whether the view is a 'Full View' or a 'Zoomed Image'. Resizing the screen across the width condition causes the browser to switch the image candidate accordingly. On the actual screen, check the label as well to verify that the message and the image match.
The browser is responsible for selecting the image candidate. Candidate order, display area, screen density, and format support may also influence the selection. Therefore, use currentSrc to verify the 'selected URL', and check pixel rendering and load success separately.
Include alternative text when using in production
Alternative text is a description used to convey the content of an image to users. Even when switching compositions with the picture element, the alt attribute on the img element remains necessary. If the image conveys the same subject regardless of which composition is used, a common alt attribute can be placed on the img element. If the content or meaning changes significantly depending on the composition, reevaluate whether switching based solely on screen width is appropriate. Alternative text should avoid focusing too heavily on decorative visual descriptions and instead communicate the role the image plays within the page.
On product pages, it can be useful to use a composition that displays the product itself larger on narrow screens, while including the usage context on wider screens. For eye-catching images in news articles or blogs, you can provide candidates where key portions of people or charts are not clipped, even on narrow screens. Regardless of which image is selected, verify through actual screen sizes and assistive technologies that the conveyed information and the correspondence with link destinations are maintained.
Checklist when images fail to switch
If the candidate does not switch even when the width is altered, check the following points in order:
Did you cross 600 CSS px?: Verify whether the condition was crossed using CSS pixels, rather than physical pixels or zoom level.
Source position and attributes: Verify that the source element is a child of the picture element, placed before the img element, and that the media attribute is correct, such as '(min-width: 600px)'.
srcset URL: Even if the width condition is met, the image will fail to load if the image URL is broken. Check this using the Network or Console panels in developer tools.
Actually selected candidate: Inspect img.currentSrc. If currentSrc is empty or unexpected, check the HTML nesting, attribute values, and load errors.
Separate display results from selection resultsEven when a URL is selected, loading may occasionally fail. Let us examine the load and error events separately from image rendering.
If the fallback is always triggered, check the media parentheses, units, order of source and img elements, and URL validity. If the image changes but the primary subject is not visible, do not assume it is a browser bug; instead, review the combination of the prepared cropping and display area.
Constraints and Safe Handling
Because the diagram in this sample loads Mermaid 11.12.2 from a CDN, external communication occurs when rendering the diagram. The targets of manipulation are hypothetical SVG images, and there are no functions for selecting, transmitting, or saving user photos or files. When using a public website, standard connection destination information may reach the distributor.
When deploying to an actual site, limit image URLs to publicly distributable assets and ensure that internal-only images or those containing personal information are not placed in a public environment. Verify image rights, model releases, alternative text, and appearance on both smartphones and desktops. Physical browser testing, screen reader testing, and image delivery network checks were not performed this time.
