导航菜单

HTML Element: picture

The HTML element displays responsive images on web pages. It allows web developers to provide multiple sources of an image, specifying different versions for various screen sizes or resolutions.

This ensures that the most suitable image is loaded for each user’s device, optimizing performance and user experience.

Syntax

The element consists of one or more  elements nested inside it, followed by an element for fallback content. Here’s a basic example of the syntax: 

12 3 4 5

In this example, three different versions of an image are provided. The browser will choose the most appropriate one based on the viewport size calculated at a min-width media size.

ExamplesArt Direction12 3 4 5

Here, images are selected based on the device’s orientation, ensuring the correct image is displayed for portrait and landscape views. A design may take a different form depending on the device’s orientation to view the content.

Check out the full-screen version and resize the browser to see the image selection in action.

Notice how the elements are bringing the images in, but we still use the img {} selector to target each one with CSS.High-DPI Displays12 3 4 5

This example targets high-DPI (Retina) displays by providing a 2x resolution image with the @2x suffix.

It’s important to note that the @2x suffix in the srcset attribute does not trigger the browser’s high-DPI (Retina) display recognition. The @2x convention is commonly used in file naming to indicate higher pixel density or resolution versions of images for the benefit of developers and designers. Consider it a design pattern.

Attributes

The element supports global attributes.

Content

The element accepts multiple elements followed by an element for fallback content. Each element should have a srcset attribute, and the element should have a src and alt attribute.

Did You Know?The element was introduced in HTML5 to address the need for responsive images in web design.Web developers can combine with the and  elements to create adaptive images for modern websites.Learn MoreOfficial W3C specification HTML & CSS for Beginners (MEGA Free Course!)Adi Purdila08 Sep 2022HTML (Hypertext Markup Language)Ian Yates02 Nov 2023How to Use HTML5 “picture”, “srcset”, and “sizes” for Responsive ImagesKezz Bracey27 Jun 2023HTML Elements -

相关推荐: