Source

Struct Source 

Source
pub struct Source;
Expand description

The <source> element - specifies media resources for <picture>, <audio>, and <video>.

§Purpose

The <source> element specifies multiple media resources for <picture>, <audio>, or <video> elements. The browser selects the most appropriate source based on media queries, format support, and other conditions. It enables responsive media delivery and format fallbacks.

§Content Categories

  • None (used only within specific parent elements)

§Permitted Content Model

  • None (void element)

§Common Use Cases

  • Providing multiple video formats for cross-browser compatibility
  • Offering different audio quality levels or formats
  • Specifying image sources for different screen sizes in <picture>
  • Delivering modern media formats with legacy fallbacks
  • Bandwidth optimization with multiple quality levels

§Key Attributes

  • src: URL of the media resource (for <audio> and <video>)
  • srcset: Image URLs for responsive images (for <picture>)
  • type: MIME type of the resource
  • media: Media query for when this source applies
  • sizes: Image sizes for different viewport widths (for <picture>)
  • width: Intrinsic width for image sources
  • height: Intrinsic height for image sources

§Example

<!-- Video with multiple formats -->
<video controls>
  <source src="/video/movie.webm" type="video/webm">
  <source src="/video/movie.mp4" type="video/mp4">
  Your browser doesn't support the video element.
</video>

<!-- Audio with quality options -->
<audio controls>
  <source src="/audio/music.opus" type="audio/opus">
  <source src="/audio/music.ogg" type="audio/ogg">
  <source src="/audio/music.mp3" type="audio/mpeg">
</audio>

<!-- Responsive images in picture element -->
<picture>
  <source media="(min-width: 1200px)" srcset="/images/xl.jpg">
  <source media="(min-width: 768px)" srcset="/images/lg.jpg">
  <source srcset="/images/sm.jpg">
  <img src="/images/fallback.jpg" alt="Description">
</picture>

<!-- Modern image formats with type hints -->
<picture>
  <source srcset="/images/photo.avif" type="image/avif">
  <source srcset="/images/photo.webp" type="image/webp">
  <img src="/images/photo.jpg" alt="Photo">
</picture>

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Source

Source§

const TAG: &'static str = "source"

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

const VOID: bool = true

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

impl CanContain<Source> for Audio

Source§

impl CanContain<Source> for Picture

Source§

impl CanContain<Source> for Video

Auto Trait Implementations§

§

impl Freeze for Source

§

impl RefUnwindSafe for Source

§

impl Send for Source

§

impl Sync for Source

§

impl Unpin for Source

§

impl UnwindSafe for Source

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.