summaryrefslogtreecommitdiff
path: root/devdocs/html/element%2Fpicture.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/html/element%2Fpicture.html
new repository
Diffstat (limited to 'devdocs/html/element%2Fpicture.html')
-rw-r--r--devdocs/html/element%2Fpicture.html108
1 files changed, 108 insertions, 0 deletions
diff --git a/devdocs/html/element%2Fpicture.html b/devdocs/html/element%2Fpicture.html
new file mode 100644
index 00000000..3beeaf09
--- /dev/null
+++ b/devdocs/html/element%2Fpicture.html
@@ -0,0 +1,108 @@
+<header><h1>&lt;picture&gt;: The Picture element</h1></header><div class="section-content">
+<p>The <code>&lt;picture&gt;</code> <a href="../index">HTML</a> element contains zero or more <a href="source"><code>&lt;source&gt;</code></a> elements and one <a href="img"><code>&lt;img&gt;</code></a> element to offer alternative versions of an image for different display/device scenarios.</p> <p>The browser will consider each child <code>&lt;source&gt;</code> element and choose the best match among them. If no matches are found—or the browser doesn't support the <code>&lt;picture&gt;</code> element—the URL of the <code>&lt;img&gt;</code> element's <a href="img#src"><code>src</code></a> attribute is selected. The selected image is then presented in the space occupied by the <code>&lt;img&gt;</code> element.</p>
+</div>
+<h2 id="try_it">Try it</h2>
+<div class="section-content">
+<iframe class="interactive is-tabbed-standard-height" height="200" src="https://interactive-examples.mdn.mozilla.net/pages/tabbed/picture.html" title="MDN Web Docs Interactive Example" loading="lazy"></iframe> <p>To decide which URL to load, the <a href="https://developer.mozilla.org/en-US/docs/Glossary/User_agent">user agent</a> examines each <code>&lt;source&gt;</code>'s <a href="source#srcset"><code>srcset</code></a>, <a href="source#media"><code>media</code></a>, and <a href="source#type"><code>type</code></a> attributes to select a compatible image that best matches the current layout and capabilities of the display device.</p> <p>The <code>&lt;img&gt;</code> element serves two purposes:</p> <ol> <li>It describes the size and other attributes of the image and its presentation.</li> <li>It provides a fallback in case none of the offered <code>&lt;source&gt;</code> elements are able to provide a usable image.</li> </ol> <p>Common use cases for <code>&lt;picture&gt;</code>:</p> <ul> <li>
+<strong>Art direction.</strong> Cropping or modifying images for different <code>media</code> conditions (for example, loading a simpler version of an image which has too many details, on smaller displays).</li> <li>
+<strong>Offering alternative image formats</strong>, for cases where certain formats are not supported. <div class="notecard note" id="sect1"> <p><strong>Note:</strong> For example, newer formats like <a href="https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#avif_image">AVIF</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types#webp_image">WEBP</a> have many advantages, but might not be supported by the browser. A list of supported image formats can be found in: <a href="https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types">Image file type and format guide</a>.</p> </div> </li> <li>
+<strong>Saving bandwidth and speeding page load times</strong> by loading the most appropriate image for the viewer's display.</li> </ul> <p>If providing higher-density versions of an image for high-DPI (Retina) display, use <a href="img#srcset"><code>srcset</code></a> on the <code>&lt;img&gt;</code> element instead. This lets browsers opt for lower-density versions in data-saving modes, and you don't have to write explicit <code>media</code> conditions.</p> <figure class="table-container"><div class="_table"><table class="properties"> <tbody> <tr> <th scope="row"><a href="../content_categories">Content categories</a></th> <td>
+<a href="../content_categories#flow_content">Flow content</a>, phrasing content, embedded content</td> </tr> <tr> <th scope="row">Permitted content</th> <td> Zero or more <a href="source"><code>&lt;source&gt;</code></a> elements, followed by one <a href="img"><code>&lt;img&gt;</code></a> element, optionally intermixed with script-supporting elements. </td> </tr> <tr> <th scope="row">Tag omission</th> <td>None, both the starting and ending tag are mandatory.</td> </tr> <tr> <th scope="row">Permitted parents</th> <td>Any element that allows embedded content.</td> </tr> <tr> <th scope="row">Implicit ARIA role</th> <td><a href="https://www.w3.org/TR/html-aria/#dfn-no-corresponding-role" target="_blank">No corresponding role</a></td> </tr> <tr> <th scope="row">Permitted ARIA roles</th> <td>No <code>role</code> permitted</td> </tr> <tr> <th scope="row">DOM interface</th> <td><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLPictureElement"><code>HTMLPictureElement</code></a></td> </tr> </tbody> </table></div></figure>
+</div>
+<h2 id="attributes">Attributes</h2>
+<div class="section-content"><p>This element includes only <a href="../global_attributes">global attributes</a>.</p></div>
+<h2 id="usage_notes">Usage notes</h2>
+<div class="section-content">
+<p>You can use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/object-position"><code>object-position</code></a> property to adjust the positioning of the image within the element's frame, and the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit"><code>object-fit</code></a> property to control how the image is resized to fit within the frame.</p> <div class="notecard note" id="sect2"> <p><strong>Note:</strong> Use these properties on the child <code>&lt;img&gt;</code> element, <strong>not</strong> the <code>&lt;picture&gt;</code> element.</p> </div>
+</div>
+<h2 id="examples">Examples</h2>
+<div class="section-content"><p>These examples demonstrate how different attributes of the <a href="source"><code>&lt;source&gt;</code></a> element change the selection of the image inside <code>&lt;picture&gt;</code>.</p></div>
+<h3 id="the_media_attribute">The media attribute</h3>
+<div class="section-content">
+<p>The <code>media</code> attribute specifies a media condition (similar to a media query) that the user agent will evaluate for each <a href="source"><code>&lt;source&gt;</code></a> element.</p> <p>If the <a href="source"><code>&lt;source&gt;</code></a>'s media condition evaluates to <code>false</code>, the browser skips it and evaluates the next element inside <code>&lt;picture&gt;</code>.</p> <div class="code-example">
+<p class="example-header"><span class="language-name">html</span></p>
+<pre data-signature="HTS5sj9/JeZxfgCJb0vNnD+K4YSJezBpfY1Kw5ewzc8=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>picture</span><span class="token punctuation">&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>source</span> <span class="token attr-name">srcset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mdn-logo-wide.png<span class="token punctuation">"</span></span> <span class="token attr-name">media</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>(min-width: 600px)<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mdn-logo-narrow.png<span class="token punctuation">"</span></span> <span class="token attr-name">alt</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>MDN<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>picture</span><span class="token punctuation">&gt;</span></span>
+</pre>
+</div>
+</div>
+<h3 id="the_srcset_attribute">The srcset attribute</h3>
+<div class="section-content">
+<p>The <a href="source#srcset">srcset</a> attribute is used to offer list of possible images <em>based on size</em>.</p> <p>It is composed of a comma-separated list of image descriptors. Each image descriptor is composed of a URL of the image, and <em>either</em>:</p> <ul> <li> a <em>width descriptor</em>, followed by a <code>w</code> (such as <code>300w</code>); <em>OR</em> </li> <li>a <em>pixel density descriptor</em>, followed by an <code>x</code> (such as <code>2x</code>) to serve a high-res image for high-DPI screens.</li> </ul> <div class="code-example">
+<p class="example-header"><span class="language-name">html</span></p>
+<pre data-signature="TFQUnfmCqrOpzau1E7THczzY1iLwGn9FPJjRqHl4S0s=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>picture</span><span class="token punctuation">&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>source</span> <span class="token attr-name">srcset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>logo-768.png, logo-768-1.5x.png 1.5x<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>logo-320.png<span class="token punctuation">"</span></span> <span class="token attr-name">alt</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>logo<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>picture</span><span class="token punctuation">&gt;</span></span>
+</pre>
+</div>
+</div>
+<h3 id="the_type_attribute">The type attribute</h3>
+<div class="section-content">
+<p>The <code>type</code> attribute specifies a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types">MIME type</a> for the resource URL(s) in the <a href="source"><code>&lt;source&gt;</code></a> element's <code>srcset</code> attribute. If the user agent does not support the given type, the <a href="source"><code>&lt;source&gt;</code></a> element is skipped.</p> <div class="code-example">
+<p class="example-header"><span class="language-name">html</span></p>
+<pre data-signature="zVNOyNoqC/eoScJZQdVdBQnuXcLhZ8ZB/GfEmIpVD5s=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>picture</span><span class="token punctuation">&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>source</span> <span class="token attr-name">srcset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>photo.avif<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>image/avif<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>source</span> <span class="token attr-name">srcset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>photo.webp<span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>image/webp<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+ <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>photo.jpg<span class="token punctuation">"</span></span> <span class="token attr-name">alt</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>photo<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
+<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>picture</span><span class="token punctuation">&gt;</span></span>
+</pre>
+</div>
+</div>
+<h2 id="specifications">Specifications</h2>
+<div class="_table"><table class="standard-table">
+<thead><tr><th scope="col">Specification</th></tr></thead>
+<tbody><tr><td><a href="https://html.spec.whatwg.org/multipage/embedded-content.html#the-picture-element">HTML Standard <br><small># the-picture-element</small></a></td></tr></tbody>
+</table></div>
+<h2 id="browser_compatibility">Browser compatibility</h2>
+<div class="_table"><table>
+<thead>
+<tr id="bct-browser-type">
+<th></th>
+<th colspan="6">Desktop</th>
+<th colspan="6">Mobile</th>
+</tr>
+<tr id="bct-browsers">
+<th></th>
+<th>Chrome</th>
+<th>Edge</th>
+<th>Firefox</th>
+<th>Internet Explorer</th>
+<th>Opera</th>
+<th>Safari</th>
+<th>WebView Android</th>
+<th>Chrome Android</th>
+<th>Firefox for Android</th>
+<th>Opera Android</th>
+<th>Safari on IOS</th>
+<th>Samsung Internet</th>
+</tr>
+</thead>
+<tbody><tr>
+<th><code>picture</code></th>
+<td class="bc-supports-yes">38</td>
+<td class="bc-supports-yes">13</td>
+<td class="bc-supports-yes">38</td>
+<td class="bc-supports-no">No</td>
+<td class="bc-supports-yes">25</td>
+<td class="bc-supports-yes">9.1</td>
+<td class="bc-supports-yes">38</td>
+<td class="bc-supports-yes">38</td>
+<td class="bc-supports-yes">38</td>
+<td class="bc-supports-yes">25</td>
+<td class="bc-supports-yes">9.3</td>
+<td class="bc-supports-yes">3.0</td>
+</tr></tbody>
+</table></div>
+<h2 id="see_also">See also</h2>
+<div class="section-content"><ul> <li>
+<a href="img"><code>&lt;img&gt;</code></a> element</li> <li>
+<a href="source"><code>&lt;source&gt;</code></a> element</li> <li>Positioning and sizing the picture within its frame: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/object-position"><code>object-position</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit"><code>object-fit</code></a>
+</li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types">Image file type and format guide</a></li> </ul></div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2005&ndash;2023 MDN contributors.<br>Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.<br>
+ <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture" class="_attribution-link">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture</a>
+ </p>
+</div>