Images & Media

HTML <source> element

Offers an alternative media resource inside audio, video or picture. Use src for media and srcset for picture sources. The example uses illustrative local asset paths; supply matching files before running it. A picture source uses srcset and may use media, type and sizes. A media source uses src and can declare a type to help the browser choose a playable candidate. Source is not displayed as an independent image or control. It accepts global HTML attributes; individual global features may be inapplicable or restricted.

Formal Syntax & Structure

html syntaxhtml
<source src="sample.ogg" type="audio/ogg">

Parameters & Attributes

NameTypeDescription
srcURLUsed for audio/video sources, not picture image selection.
typeelement-dependent keyword or MIME typeSelects an element-specific kind: input control state, button behavior, ordered-list numbering, script interpretation or resource media type. Consult the element's own allowed values.
srcsetimage candidate listUsed for picture source image selection, not audio/video.
sizesresponsive source size list or icon sizesOn responsive images, describes the expected display slot width used with width-descriptor candidates. On icon links, describes icon dimensions. The grammars differ.
mediamedia query listSpecifies conditions under which a resource or metadata applies, such as a stylesheet's target media or a picture source's viewport condition.
widthnon-negative integerDefines element-specific width, commonly in CSS pixels. On images it can establish an intrinsic aspect ratio together with height; on canvas it sets the bitmap width.
heightnon-negative integerSets an element-specific height, commonly in CSS pixels. On images, pair it with width matching the intrinsic aspect ratio to reserve layout space. It is not a CSS length with units.

Return Value

HTMLSourceElement

Content model: Empty; this void element cannot contain children and must not have an end tag. No closing tag.

Working Example

Example Snippethtml
<!doctype html><html lang="en">  <head>    <meta charset="utf-8">    <title>HTML example</title>  </head>  <body>    <audio controls>      <source src="sample.ogg" type="audio/ogg">      <source src="sample.mp3" type="audio/mpeg">    </audio>  </body></html>
HTML Reference