HTML <picture> element 包含零个或多个 <source> 元素和一个 <img> element to offer alternative versions of an image for different display/device scenarios.

The browser will consider each child <source> element and choose the best match among them. If no matches are found—or the browser doesn't support the <picture> element—the URL of the <img> 元素的 src attribute is selected. The selected image is then presented in the space occupied by the <img> 元素。

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

To decide which URL to load, the 用户代理 examines each <source> 's srcset , media ,和 type attributes to select a compatible image that best matches the current layout and capabilities of the display device.

<img> element serves two purposes:

  1. It describes the size and other attributes of the image and its presentation.
  2. It provides a fallback in case none of the offered <source> elements are able to provide a usable image.

Common use cases for <picture> :

  • Art direction. Cropping or modifying images for different media conditions (for example, loading a simpler version of an image which has too many details, on smaller displays).
  • Offering alternative image formats , for cases where certain formats are not supported.

    注意: For example, newer formats like AVIF or WEBP have many advantages, but  might not be supported by the browser. A list of supported image formats can be found in: Image file type and format guide .

  • Saving bandwidth and speeding page load times by loading the most appropriate image for the viewer's display.

If providing higher-density versions of an image for high-DPI (Retina) display, use srcset <img> element instead. This lets browsers opt for lower-density versions in data-saving modes, and you don't have to write explicit media conditions.

内容类别 流内容 , phrasing content, embedded content
准许内容 Zero or more <source> elements, followed by one <img> element, optionally intermixed with script-supporting elements.
Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that allows embedded content.
Implicit ARIA role 无对应角色
Permitted ARIA roles No role permitted
DOM 接口 HTMLPictureElement

属性

This element includes only 全局属性 .

用法注意事项

可以使用 object-position property to adjust the positioning of the image within the element's frame, and the object-fit property to control how the image is resized to fit within the frame.

注意: Use these properties on the child <img> element, not the <picture> 元素。

范例

These examples demonstrate how different attributes of the <source> element change the selection of the image inside <picture> .

The media attribute

media attribute specifies a media condition (similar to a media query) that the user agent will evaluate for each <source> 元素。

<source> 's media condition evaluates to false , the browser skips it and evaluates the next element inside <picture> .

<picture>
  <source srcset="mdn-logo-wide.png" media="(min-width: 600px)">
  <img src="mdn-logo-narrow.png" alt="MDN">
</picture>

The srcset attribute

srcset attribute is used to offer list of possible images based on size .

It is composed of a comma-separated list of image descriptors. Each image descriptor is composed of a URL of the image, and either...

  • a width descriptor , followed by a w (譬如 300w );
    OR
  • a pixel density descriptor , followed by an x (譬如 2x ) to serve a high-res image for high-DPI screens.
<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>

The type attribute

type attribute specifies a MIME 类型 for the resource URL(s) in the <source> 元素的 srcset attribute. If the user agent does not support the given type, the <source> element is skipped.

<picture>
  <source srcset="logo.webp" type="image/webp">
  <img src="logo.png" alt="logo">
</picture>

规范

规范 状态 注释
HTML 实时标准
The definition of '<picture>' in that specification.
实时标准 初始定义

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
picture Chrome 完整支持 38 Edge 完整支持 13 Firefox 完整支持 38
完整支持 38
不支持 32 — 52 Disabled
Disabled ). To change preferences in Firefox, visit
IE 不支持 No Opera 完整支持 25 Safari 完整支持 9.1 WebView Android 完整支持 38 Chrome Android 完整支持 38 Firefox Android 完整支持 38
完整支持 38
不支持 32 — 52 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 25 Safari iOS 完整支持 9.3 Samsung Internet Android 完整支持 3.0

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. <audio>
  2. <embed>
  3. <iframe>
  4. <img>
  5. <map>
  6. <object>
  7. <param>
  8. <picture>
  9. <source>
  10. <track>
  11. <video>
  12. HTML 元素
    1. A
      1. <a>
      2. <abbr>
      3. <acronym>
      4. <address>
      5. <applet>
      6. <area>
      7. <article>
      8. <aside>
      9. <audio>
    2. B
      1. <b>
      2. <base>
      3. <basefont>
      4. <bdi>
      5. <bdo>
      6. <bgsound>
      7. <big>
      8. <blink>
      9. <blockquote>
      10. <body>
      11. <br>
      12. <button>
    3. C
      1. <canvas>
      2. <caption>
      3. <center>
      4. <cite>
      5. <code>
      6. <col>
      7. <colgroup>
      8. <content>
    4. D
      1. <data>
      2. <datalist>
      3. <dd>
      4. <del>
      5. <details>
      6. <dfn>
      7. <dialog>
      8. <dir>
      9. <div>
      10. <dl>
      11. <dt>
    5. E
      1. <em>
      2. <embed>
    6. F
      1. <fieldset>
      2. <figcaption>
      3. <figure>
      4. <font>
      5. <footer>
      6. <form>
      7. <frame>
      8. <frameset>
    7. G H
      1. <h1>
      2. <h2>
      3. <h3>
      4. <h4>
      5. <h5>
      6. <h6>
      7. <head>
      8. <header>
      9. <hgroup>
      10. <hr>
      11. <html>
    8. I
      1. <i>
      2. <iframe>
      3. <img>
      4. <input>
      5. <ins>
      6. <isindex>
    9. J K
      1. <kbd>
      2. <keygen>
    10. L
      1. <label>
      2. <legend>
      3. <li>
      4. <link>
      5. <listing>
    11. M
      1. <main>
      2. <map>
      3. <mark>
      4. <marquee>
      5. <menu>
      6. <menuitem>
      7. <meta>
      8. <meter>
    12. N
      1. <nav>
      2. <nobr>
      3. <noframes>
      4. <noscript>
    13. O
      1. <object>
      2. <ol>
      3. <optgroup>
      4. <option>
      5. <output>
    14. P
      1. <p>
      2. <param>
      3. <picture>
      4. <plaintext>
      5. <pre>
      6. <progress>
    15. Q
      1. <q>
    16. R
      1. <rp>
      2. <rt>
      3. <rtc>
      4. <ruby>
    17. S
      1. <s>
      2. <samp>
      3. <script>
      4. <section>
      5. <select>
      6. <shadow>
      7. <slot>
      8. <small>
      9. <source>
      10. <spacer>
      11. <span>
      12. <strike>
      13. <strong>
      14. <style>
      15. <sub>
      16. <summary>
      17. <sup>
    18. T
      1. <table>
      2. <tbody>
      3. <td>
      4. <template>
      5. <textarea>
      6. <tfoot>
      7. <th>
      8. <thead>
      9. <time>
      10. <title>
      11. <tr>
      12. <track>
      13. <tt>
    19. U
      1. <u>
      2. <ul>
    20. V
      1. <var>
      2. <video>
    21. W
      1. <wbr>
    22. X Y Z
      1. <xmp>

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1