Picture

Struct Picture 

Source
pub struct Picture;
Expand description

The <picture> element - contains multiple image sources for responsive images.

§Purpose

The <picture> element provides a container for zero or more <source> elements and one <img> element to offer alternative versions of an image for different display/device scenarios. It enables art direction and format-based image selection, going beyond simple resolution switching.

§Content Categories

  • Flow Content
  • Phrasing Content
  • Embedded Content
  • Palpable Content

§Permitted Content Model

  • Zero or more <source> elements
  • Followed by one <img> element
  • Optionally intermixed with script-supporting elements

§Common Use Cases

  • Art direction (different crops or compositions for different viewport sizes)
  • Serving modern image formats with fallbacks (WebP, AVIF with JPEG fallback)
  • Resolution switching based on device pixel ratio
  • Bandwidth optimization by serving different image sizes
  • Responsive design with different aspect ratios

§Key Attributes

  • Global attributes only (attributes are primarily on child <source> and <img> elements)

§Example

<!-- Art direction: different images for different viewport sizes -->
<picture>
  <source media="(min-width: 1024px)" srcset="/images/hero-wide.jpg">
  <source media="(min-width: 768px)" srcset="/images/hero-medium.jpg">
  <img src="/images/hero-narrow.jpg" alt="Hero image">
</picture>

<!-- Format selection: modern formats with fallback -->
<picture>
  <source srcset="/images/photo.avif" type="image/avif">
  <source srcset="/images/photo.webp" type="image/webp">
  <img src="/images/photo.jpg" alt="Photograph">
</picture>

<!-- Combining media queries and formats -->
<picture>
  <source media="(min-width: 768px)" srcset="/images/large.webp" type="image/webp">
  <source media="(min-width: 768px)" srcset="/images/large.jpg">
  <source srcset="/images/small.webp" type="image/webp">
  <img src="/images/small.jpg" alt="Responsive image">
</picture>

§Accessibility

  • The alt attribute should be on the <img> element, not <picture>
  • Ensure all image variations convey the same essential information
  • Test that fallback images are appropriate when sources don’t match

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Picture

Source§

const TAG: &'static str = "picture"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl CanContain<Img> for Picture

Source§

impl CanContain<Script> for Picture

Source§

impl CanContain<Source> for Picture

Source§

impl CanContain<Template> for Picture

Source§

impl EmbeddedContent for Picture

Source§

impl FlowContent for Picture

Source§

impl PalpableContent for Picture

Source§

impl PhrasingContent for Picture

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.