HTML <picture> tagS2C Home   « HTML <picture> tag

Definition

The <picture> tag is used when rendering image resources to give more flexibility and options.

The <picture> element must contain one <img> element which must be the last child. This ensures that there is a falback image if none of the conditions of any <source> elements are met.

The <picture> element can also contain zero or more <source> elements.

The <picture> tag was introduced in HTML5.

Example


<p>Resize the screen to see the picture change.</p>
<picture>
  <source media="(min-width:1100px)" srcset="../images/chips.webp">
  <source media="(min-width:600px)" srcset="../images/chillifriedseafoodmedium.webp">
  <img src="../images/chickenpiesmall.webp" alt="Chicken Pie" style="width:auto;">
</picture>

Resize the screen to see the picture change.

Chicken Pie

In the example above we are using the <picture> element in conjunction with two <source> elements. The browser will select the first <source> element that matches the width specified by the media attribute. When neither minimum width is reached a default image (chicken pie) is shown.

Attributes

The following attributes can be used with the <picture> tag.


<picture> Specific Attributes
Attribute Value Description Example
There are no required or optional attributes specific to the <picture> tag.
<picture> Global Attributes
Attribute Description Example
Common
classSpecifies a classname for the element allowing you to apply the style of the predefined class to the content.

<picture class="name">Picture content</picture>
idSpecifies a unique id for the element allowing you to apply the style of the predefined id to the content.

<picture id="name">Picture content</picture>
styleSpecifies an inline style for the element allowing you to apply the style to the contentt.

<picture style="color:red;">Picture content</picture>
titleSpecifies an inline style for the element allowing you to apply the style to the content.

<picture title="Content info">Picture content</picture>
Keyboard
accesskeySpecifies a keyboard shortcut to associate with the element.

<picture accesskey="a">Picture content</picture>
tabindexSpecifies a tab order for the element.

<picture tabindex="1">Picture content</picture>
Language
dirSpecifies the directional flow of the content.

<!-- The text will flow from left to right -->
<picture dir="ltr">Picture content</picture>
<!-- The text will flow from right to left -->
<picture dir="rtl">Picture content</picture>
langSpecifies a language code for the content of the element.

<picture lang="en">Picture content</picture>
spellcheckSpecifies an inline style for the element allowing you to apply the style to the contentt.

<!-- Valid values true and false. -->
<!-- Default inherited / browser specific. -->
<picture spellcheck="true">Picture content</picture> 
translateSpecifies an inline style for the element allowing you to apply the style to the content.

<!-- Valid values yes and no. -->
<!-- Default yes. -->
<picture translate="no">Picture content</picture> 
Miscellaneous
contenteditableSpecifies whether the content of the element is editable.

<!-- Valid values true and false. -->
<!-- Default inherited. -->
<picture contenteditable="true">Picture content</picture> 
draggableSpecifies whether the element is draggable.

<!-- Valid values true and false. -->
<!-- Default browser specific. -->
<picture draggable="true">Picture content</picture>
hiddenSpecifies whether the element is not yet, or no longer, relevant.

<!-- Valid values an empty string or hidden -->
<picture hidden>Picture content</picture> 
<picture hidden="hidden">Picture content</picture> 
<picture> Event Attributes
Attribute Description Example
Document Element
oncopyThe script to be run when the user copies the content of an element.

 <!-- Executes go() function -->
<picture oncopy="go()">Picture content</picture> 
oncutThe script to be run when the user cuts the content of an element.

 <!-- Executes go() function -->
<picture oncut="go()">Picture content</picture>
onpasteThe script to be run when the user pastes some content into an element.

 <!-- Executes go() function -->
<picture onpaste="go()">Picture content</picture>
Drag and Drop
ondragThe script to be run when an element is dragged.

 <!-- Executes go() function -->
<picture ondrag="go()">Picture content</picture>
ondragendThe script to be run when an element has stopped being dragged.

 <!-- Executes go() function -->
<picture ondragend="go()">Picture content</picture>
ondragenterThe script to be run when an element has been dragged to a valid drop target.

 <!-- Executes go() function -->
<picture ondragenter="go()">Picture content</picture>
ondragleaveThe script to be run when an element leaves a valid drop target.

 <!-- Executes go() function -->
<picture ondragleave="go()">Picture content</picture>
ondragoverThe script to be run when an element is being dragged over a valid drop target.

 <!-- Executes go() function -->
<picture ondragover="go()">Picture content</picture>
ondragstartThe script to be run at the start of a drag operation.

 <!-- Executes go() function -->
<picture ondragstart="go()">Picture content</picture>
ondropThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<picture ondrop="go()">Picture content</picture>
Form
onblurThe script to be run when the element loses focus.

 <!-- Executes go() function -->
<picture onblur="go()">Picture content</picture>
onchangeThe script to be run when object changed and attempt to leave field.

 <!-- Executes go() function -->
<picture onchange="go()">Picture content</picture>
oncontextmenuThe script to be run when a context menu is triggered.

 <!-- Executes go() function -->
<picture oncontextmenu="go()">Picture content</picture>
onfocusThe script to be run when the element gets focus.

 <!-- Executes go() function -->
<picture onfocus="go()">Picture content</picture>
oninputThe script to be run when an element gets user input.

 <!-- Executes go() function -->
<picture oninput="go()">Picture content</picture>
oninvalidThe script to be run when an element is invalid.

 <!-- Executes go() function -->
<picture oninvalid="go()">Picture content</picture>
onresetThe script to be run when a dragged element is being dropped.

 <!-- Executes go() function -->
<picture onreset="go()">Picture content</picture>
onselectThe script to be run when some or all of the contents of an object are selected.

 <!-- Executes go() function -->
<picture onselect="go()">Picture content</picture>
onsubmitThe script to be run when a form is submitted.

 <!-- Executes go() function -->
<picture onsubmit="go()">Picture content</picture>
Keyboard
onkeydownThe script to be run when an element is in focus and keyboard key is pressed down.

 <!-- Executes go() function -->
<picture onkeydown="go()">Picture content</picture>
onkeypressThe script to be run when an element is in focus and keyboard key is pressed down and released.

 <!-- Executes go() function -->
<picture onkeypress="go()">Picture content</picture>
onkeyupThe script to be run when an element is in focus and keyboard key is released.

 <!-- Executes go() function -->
<picture onkeyup="go()">Picture content</picture>
Media
onabortThe script code to be run on abort.

 <!-- Executes go() function -->
<picture onabort="go()">Picture content</picture>
oncanplayThe script to be run when a file has buffered enough so it is ready to start playing.

 <!-- Executes go() function -->
<picture oncanplay="go()">Picture content</picture>
oncanplaythroughThe script to be run when a file can be played all the way to the end without further need of buffering.

 <!-- Executes go() function -->
<picture oncanplaythrough="go()">Picture content</picture>
oncuechangeThe script to be run when the cue changes when using the track element.

 <!-- Executes go() function -->
<picture oncuechange="go()">Picture content</picture>
ondurationchangeThe script to be run when the length of the media is changed.

 <!-- Executes go() function -->
<picture ondurationchange="go()">Picture content</picture>
onemptiedThe script to be run when a media resource element suddenly becomes empty, usually due to an error.

 <!-- Executes go() function -->
<picture onemptied="go()">Picture content</picture>
onendedThe script to be run when the media has reach the end.

 <!-- Executes go() function -->
<picture onended="go()">Picture content</picture>
onloadeddataThe script to be run when media data is loaded and playback can start.

 <!-- Executes go() function -->
<picture onloadeddata="go()">Picture content</picture>
onloadedmetadataThe script to be run when metadata has been loaded.

 <!-- Executes go() function -->
<picture onloadedmetadata="go()">Picture content</picture>
onloadstartThe script to be run whenthe media resource has started loading.

 <!-- Executes go() function -->
<picture onloadstart="go()">Picture content</picture>
onpauseThe script to be run when the media resource has been paused.

 <!-- Executes go() function -->
<picture onpause="go()">Picture content</picture>
onplayThe script to be run when the media resource starts playback.

 <!-- Executes go() function -->
<picture onplay="go()">Picture content</picture>
onplayingThe script to be run when when playback has already begun.

 <!-- Executes go() function -->
<picture onplaying="go()">Picture content</picture>
onprogressThe script to be run when the browser is fetching the media data.

 <!-- Executes go() function -->
<picture onprogress="go()">Picture content</picture>
onratechangeThe script to be run when the playback rate changes.

 <!-- Executes go() function -->
<picture onratechange="go()">Picture content</picture>
onseekedThe script to be run when the seeking attribute is set to false indicating that seeking has finished.

 <!-- Executes go() function -->
<picture onseeked="go()">Picture content</picture>
onseekingThe script to be run when the seeking attribute is set to true indicating that seeking is currently active.

 <!-- Executes go() function -->
<picture onseeking="go()">Picture content</picture>
onstalledThe script to be run when the browser is unable to continue fetching media data.

 <!-- Executes go() function -->
<picture onstalled="go()">Picture content</picture>
onsuspendThe script to be run when media data has stopped before being completely loaded.

 <!-- Executes go() function -->
<picture onsuspend="go()">Picture content</picture>
ontimeupdateThe script to be run when the media resources current playback position has changed.

 <!-- Executes go() function -->
<picture ontimeupdate="go()">Picture content</picture>
onvolumechangeThe script to be run when the volume has changed or been muted.

 <!-- Executes go() function -->
<picture onvolumechange="go()">Picture content</picture>
onwaitingThe script to be run when the media resource has paused but is expected to resume.

 <!-- Executes go() function -->
<picture onwaiting="go()">Picture content</picture>
Mouse
onclickThe script to be run when when a mouse is clicked on an element.

 <!-- Executes go() function -->
<picture onclick="go()">Picture content</picture>
ondblclickThe script to be run when a mouse is double clicked on an element.

 <!-- Executes go() function -->
<picture ondblclick="go()">Picture content</picture>
onmousedownThe script to be run when he mouse button is pressed down while the cursor is over an element.

 <!-- Executes go() function -->
<picture onmousedown="go()">Picture content</picture>
onmousemoveThe script to be run when the mouse button is moved.

 <!-- Executes go() function -->
<picture onmousemove="go()">Picture content</picture>
onmouseoutThe script to be run when the mouse cursor moves off an element.

 <!-- Executes go() function -->
<picture onmouseout="go()">Picture content</picture>
onmouseoverThe script to be run when the mouse cursor moves over an element.

 <!-- Executes go() function -->
<picture onmouseover="go()">Picture content</picture>
onmouseupThe script to be run when the mouse button is released while the cursor is over the element.

 <!-- Executes go() function -->
<picture onmouseup="go()">Picture content</picture>
onwheelThe script to be run when the mouse wheel rolls up or down over an element.

 <!-- Executes go() function -->
<picture onwheel="go()">Picture content</picture>
Scroll
onscrollThe script code to be run when the scrollbar of an element is being scrolled.

 <!-- Executes go() function -->
<picture onscroll="go()">Picture content</picture> 
Window - NONE

Relevant HTML Tutorials

HTML Intermediate - Image Flexibility