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
| Name | Type | Description |
|---|---|---|
| src | URL | Used for audio/video sources, not picture image selection. |
| type | element-dependent keyword or MIME type | Selects 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. |
| srcset | image candidate list | Used for picture source image selection, not audio/video. |
| sizes | responsive source size list or icon sizes | On responsive images, describes the expected display slot width used with width-descriptor candidates. On icon links, describes icon dimensions. The grammars differ. |
| media | media query list | Specifies conditions under which a resource or metadata applies, such as a stylesheet's target media or a picture source's viewport condition. |
| width | non-negative integer | Defines 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. |
| height | non-negative integer | Sets 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>