--- title: '<picture>: 画像要素' slug: Web/HTML/Element/picture tags: - Element - Graphics - HTML - HTML embedded content - Images - Reference - Web - WebP - picture translation_of: Web/HTML/Element/picture --- <div>{{HTMLRef}}</div> <p><span class="seoSummary"><strong>HTML の <code><picture></code> 要素</strong>は、0個以上の {{HTMLElement("source")}} 要素と一つの {{HTMLElement("img")}} 要素を含み、様々な画面や端末の条件に応じた画像を提供します。</span></p> <p>ブラウザーは複数の <code><source></code> 子要素を検討し、その中から最も適切なものを選択します。適切なものがない場合や、ブラウザーが <code><picture></code> 要素に対応してない場合、 <code><img></code> 要素の {{htmlattrxref("src", "img")}} 属性で指定された URL が選択されます。選択された画像は <code><img></code> 要素が占有する領域に表示されます。</p> <div>{{EmbedInteractiveExample("pages/tabbed/picture.html", "tabbed-standard")}}</div> <p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p> <p>どの URL を読み込むかを選択するには、{{Glossary("user agent","ユーザーエージェント")}}はそれぞれの <code><source></code> 要素の {{htmlattrxref("srcset", "source")}}, {{htmlattrxref("media", "source")}}, {{htmlattrxref("type", "source")}} 属性を調べて、現在のページのレイアウトや表示機器の能力に最も合う画像を検討します。</p> <p><code><img></code> 要素は2つの役割を担います。</p> <ol> <li>画像の寸法やその他の属性を記述します。</li> <li><code><source></code> 要素で利用可能な画像を提供できなかった場合の代替策を提供します。</li> </ol> <p><code><picture></code> をよく使う場面は以下の通りです。</p> <ul> <li><strong>アートディレクション</strong> 様々な <code>media</code> の条件に合わせて画像を切り抜いたり変更したりする (例えば、小さな画面では、詳細すぎない、より簡単な版の画像を読み込むなど)。</li> <li>特定の形式に対応していないブラウザーに対して、<strong>代替画像形式を提供する</strong>。</li> <li>見る人の画面に最も適合する画像を読み込むことで、<strong>通信帯域を節約しページの読み込みをより速くする</strong>。</li> </ul> <p>DPI の高い (高解像度の) ディスプレイのために高解像度版の画像を提供する場合は、代わりに {{htmlattrxref("srcset", "img")}} 属性を <code><img></code> に使用してください。これによってブラウザーはデータ節約モードでは低解像度版を選択することができ、 <code>media</code> 条件を明示的に書かなくてもよくなります。</p> <table class="properties"> <tbody> <tr> <th scope="row"><a href="/ja/docs/Web/HTML/Content_categories">コンテンツカテゴリ</a></th> <td><a href="/ja/docs/Web/HTML/Content_categories#Flow_content">フローコンテンツ</a>, 記述コンテンツ, 埋め込みコンテンツ</td> </tr> <tr> <th scope="row">許可されている内容</th> <td>0個以上の {{HTMLElement("source")}} 要素、その後に1個の {{HTMLElement("img")}} 要素、任意でスクリプト対応要素と混在。</td> </tr> <tr> <th scope="row">タグの省略</th> <td>{{no_tag_omission}}</td> </tr> <tr> <th scope="row">許可されている親要素</th> <td>埋め込みコンテンツを含むことができるすべての要素。</td> </tr> <tr> <th scope="row">暗黙の ARIA ロール</th> <td><a href="https://www.w3.org/TR/html-aria/#dfn-no-corresponding-role">対応するロールなし</a></td> </tr> <tr> <th scope="row">許可されている ARIA ロール</th> <td>許可されている <code>role</code> なし</td> </tr> <tr> <th scope="row">DOM インターフェイス</th> <td>{{domxref("HTMLPictureElement")}}</td> </tr> </tbody> </table> <h2 id="Attributes" name="Attributes">属性</h2> <p>この要素は<a href="/ja/docs/Web/HTML/Global_attributes">グローバル属性</a>のみを持ちます。</p> <h2 id="Usage_notes" name="Usage_notes">使用上のメモ</h2> <p>{{cssxref("object-position")}} プロパティを使用して、要素の枠内で画像の位置を調整したり、 {{cssxref("object-fit")}} プロパティを使用して、枠内に合わせるための画像の寸法を変更する方法を制御したりすることができます。</p> <div class="note"> <p><strong>注:</strong> これらのプロパティは子の <code><img></code> 要素に用い、 <code><picture></code> 要素には<strong>用いない</strong>でください。</p> </div> <h2 id="Examples" name="Examples">例</h2> <p>これらの例は、 {{HTMLElement("source")}} 要素の様々な属性がどのように <code><picture></code> 内の画像の選択を変更するかを示しています。</p> <h3 id="The_media_attribute" name="The_media_attribute">media 属性</h3> <p><code>media</code> 属性はユーザーエージェントがそれぞれの {{HTMLElement("source")}} 要素を評価するメディア条件を (メディアクエリと同様に) 指定します。</p> <p>メディア条件が <code>false</code> と評価された場合、 {{HTMLElement("source")}} 要素はスキップされ、 <code><picture></code> 内の次の要素が評価されます。</p> <pre class="brush: html notranslate"><picture> <source srcset="mdn-logo-wide.png" media="(min-width: 600px)"> <img src="mdn-logo-narrow.png" alt="MDN"> </picture> </pre> <h3 id="The_srcset_attribute" name="The_srcset_attribute">srcset 属性</h3> <p><a href="/ja/docs/Web/HTML/Element/source#attr-srcset">{{htmlattrdef("srcset")}}</a> 属性は、<em>寸法に基づいた</em>利用可能な画像のリストを提供するために使用します。</p> <p>これは画像記述子のカンマ区切りのリストで構成されます。それぞれの画像記述子は画像の URL と、次の<em>いずれか</em>で構成されます。</p> <ul> <li><em>幅記述子</em> は <code>w</code> に続けて書きます (<code>300w</code> など)<br> <em>または</em></li> <li><em>ピクセル密度記述子</em> は <code>x</code> に続けて書き (<code>2x</code> など)、高 DPI 画面の高解像度画像を提供します。</li> </ul> <pre class="brush: html notranslate"><picture> <source srcset="logo-768.png 768w, logo-768-1.5x.png 1.5x"> <source srcset="logo-480.png, logo-480-2x.png 2x"> <img src="logo-320.png" alt="logo"> </picture></pre> <h3 id="The_type_attribute" name="The_type_attribute">type 属性</h3> <p><code>type</code> 属性は、 {{HTMLElement("source")}} 要素の <code>srcset</code> 属性で与えられるリソース URL の <a href="/ja/docs/Web/HTTP/Basics_of_HTTP/MIME_types">MIME タイプ</a>を指定します。ユーザーエージェントが指定されたタイプに対応していない場合、その {{HTMLElement("source")}} 要素はスキップされます。</p> <pre class="brush: html notranslate"><picture> <source srcset="logo.webp" type="image/webp"> <img src="logo.png" alt="logo"> </picture> </pre> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> <th scope="col">状態</th> <th scope="col">備考</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('HTML WHATWG', 'embedded-content.html#the-picture-element', '<picture>')}}</td> <td>{{Spec2('HTML WHATWG')}}</td> <td>初回定義</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <p>{{Compat("html.elements.picture")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{HTMLElement("img")}} 要素</li> <li>{{HTMLElement("source")}} 要素</li> <li>フレーム内の画像の位置や寸法の設定: {{cssxref("object-position")}} 及び {{cssxref("object-fit")}}</li> <li><a href="/ja/docs/Web/Media/Formats/Image_types">画像ファイルのImage file type and format guide</a></li> </ul>