<source>

An alternative source of an image, audio, or video for `<picture>`, `<audio>`, and `<video>`.

Display
not displayed
Closing tag
None, void element
Permitted content
None, empty element
On this page

Example

#
HTML
<video controls width="300" height="200" preload="none" poster="/collections/covers/jak-nauczyc-sie-programowac.webp">
  <source src="/media/zapowiedz-kursu.webm" type="video/webm">
  <source src="/media/zapowiedz-kursu.mp4" type="video/mp4">
  <a href="/media/zapowiedz-kursu.mp4">Download the course trailer (MP4)</a>
</video>
Preview

The video files are not available in the preview, so the player shows only the poster and the controls.

Definition and usage

#

The <source> element provides one of several possible media sources. It sits inside <picture>, <audio>, or <video>, and the browser goes through the sources in order and picks the first one it can use. It is an empty element and displays nothing by itself.

In <picture> you point to the file with the srcset attribute, and set selection conditions with the media (a media query) and type (the file format) attributes. In <audio> and <video> you use the src and type attributes, so you can offer the same video in several formats, for example WebM and MP4.

It’s worth always providing the type attribute. The browser then immediately skips sources in formats it doesn’t support, instead of downloading a piece of each file to check.

Syntax

#
Syntax
<source src="…" type="…">
<source srcset="…" media="…">

Attributes

#
  • src

    URL

    The address of the audio or video file. Used inside <audio> and <video>.
  • type

    MIME type, e.g. `video/webm`, `audio/mpeg`, `image/avif`

    The file format. The browser skips sources in formats it doesn’t know. You can add a codecs parameter, for example video/webm; codecs="vp9", to specify the exact codec.
  • srcset

    image address or a list of versions with a width or density descriptor

    The image file for this source, or even several versions of it written the same way as in the srcset of an <img> element. Used only in <picture>.
  • media

    media query, e.g. `(min-width: 768px)`

    A condition the screen must meet for the source to be chosen. It works like @media queries in CSS. The first matching source wins, so with min-width conditions start from the largest width.
  • sizes

    list of conditions and widths

    Works together with a width-based srcset (800w) and tells the browser how wide the image will be on the page. Only in <picture>.
  • width / height

    number of pixels

    The dimensions of the image from this source. They are useful with different crops: the browser reserves space with the proportions of the source it actually chose. Only in <picture>.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

src

URL

The address of the audio or video file. Used inside <audio> and <video>.

HTML
<audio controls preload="none">
  <source src="/podcast/odcinek-12.ogg" type="audio/ogg">
  <source src="/podcast/odcinek-12.mp3" type="audio/mpeg">
</audio>
Preview

The audio files are not available in the preview, only the player controls are visible.

media

media query, e.g. `(min-width: 768px)`

A condition the screen must meet for the source to be chosen. It works like @media queries in CSS. The first matching source wins, so with min-width conditions start from the largest width.

HTML
<picture>
  <source media="(min-width: 480px)" srcset="/collections/covers/vibe-coding.webp" width="240" height="160">
  <img src="/collections/cursor.png" alt="A vibe coding course with the Cursor editor" width="72" height="72">
</picture>
Preview

Narrow the window below 480 pixels and the image changes to the logo.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • In <video> and <audio>, list sources from the more modern and lighter format to the most compatible one, which usually goes last (for example MP4).
  • In <picture>, use srcset, not src. The src attribute on <source> works only in audio and video elements.
  • Changing src on an existing <source> with a script is not enough. You also have to call video.load() so the player loads the new file.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.