HTMLImageElement property sizes allows you to specify the layout width of the image for each of a list of media conditions. This provides the ability to automatically select among different images—even images of different orientations or aspect ratios—as the document state changes to match different media conditions. Each condition is specified using the same conditional format used by media queries .

句法

let sizes = htmlImageElement.sizes;
htmlImageElement.sizes = sizes;
					

A USVString containing a comma-separated list of source size descriptors followed by an optional fallback size. Each source size descriptor is comprised of a media condition, then at least one whitespace character, then the source size value to use for the image when the media condition evaluates to true .

Media conditions

Each source size descriptor consists of a media condition as defined by the media queries standard. Because a source size descriptor is used to specify the width to use for the image during layout of the page, the media condition is typically (but not necessarily) based entirely on width information. See 句法 in Using media queries for details on how to construct a media condition.

Source size values

The source size value is a CSS length . It may be specified using font-relative units (such as em or ex ), absolute units (such as px or cm ), or the vw unit, which lets you specify the width as a percentage of the viewport width ( 1vw being 1% of the viewport width).

注意: The source size value must not be specified as a percentage of the container size; that is, lengths such as 50% or 100% are not allowed, as there would be uncertainty as to what the specified value is a percentage of.

范例

In this example, a blog-like layout is created, displaying some text and an image which for which three size points are specified, depending on the width of the window. Three versions of the image are also available, with their widths specified. The browser takes all of this information and selects an image and width that best meets the specified values.

How exactly the images are used may depend upon the browser and what the pixel density of the user's display is.

Buttons at the bottom of the example let you actually modify the sizes property slightly, switching the largest of the three widths for the image between 40em and 50em.

HTML

<article>
  <h1>An amazing headline</h1>
  <div class="test"></div>
  <p>This is even more amazing content text. It's really spectacular.
     And fascinating. Oh, it's also clever and witty. Award-winning
     stuff, I'm sure.</p>
  <img src="/files/16870/new-york-skyline-wide.jpg"
       srcset="/files/16870/new-york-skyline-wide.jpg 3724w,
               /files/16869/new-york-skyline-4by3.jpg 1961w,
               /files/16871/new-york-skyline-tall.jpg 1060w"
       sizes="((min-width: 50em) and (max-width: 60em)) 50em,
              ((min-width: 30em) and (max-width: 50em)) 30em,
              (max-width: 30em) 20em">
  <p>Then there's even more amazing stuff to say down here. Can you
     believe it? I sure can't.</p>
  <button id="break40">Last Width: 40em</button>
  <button id="break50">Last Width: 50em</button>
</article>
					

CSS

article {
  margin: 1em;
  max-width: 60em;
  min-width: 20em;
  height: 100vh;
  border: 4em solid #880E4F;
  border-radius: 7em;
  padding: 1.5em;
  font: 16px "Open Sans", Verdana, Arial, Helvetica, sans-serif;
}
article img {
  display: block;
  max-width: 100%;
  border: 1px solid #888;
  box-shadow: 0 0.5em 0.3em #888;
  margin-bottom: 1.25em;
}
					

JavaScript

The JavaScript code handles the two buttons that let you toggle the third width option between 40em and 50em; this is done by handling the click event, using the JavaScript string object method replace() to replace the relevant portion of the sizes 字符串。

let image = document.querySelector("article img");
let break40 = document.getElementById("break40");
let break50 = document.getElementById("break50");
break40.addEventListener("click",
    event => image.sizes = image.sizes.replace(/50em,/, "40em,"));
break50.addEventListener("click",
    event => image.sizes = image.sizes.replace(/40em,/, "50em,"));
					

结果

This result may be viewed in its own window here .

规范

规范 状态 注释
HTML 实时标准
The definition of 'HTMLImageElement.sizes' in that specification.
实时标准

浏览器兼容性

The compatibility table on 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
sizes Chrome 45 Edge 13 Firefox 38
38
不支持 33 — 52 Disabled
Disabled ). To change preferences in Firefox, visit
IE No Opera Yes Safari No WebView Android 45 Chrome Android 45 Firefox Android 38
38
不支持 33 — 52 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android No Safari iOS No Samsung Internet Android 5.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

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

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

另请参阅

元数据

  • 最后修改:
  1. HTMLImageElement
  2. 构造函数
    1. Image()
  3. 特性
    1. align
    2. alt
    3. border
    4. complete
    5. crossOrigin
    6. currentSrc
    7. decoding
    8. height
    9. hspace
    10. loading
    11. longDesc
    12. lowSrc
    13. 名称
    14. naturalWidth
    15. referrerPolicy
    16. sizes
    17. srcset
    18. useMap
    19. vspace
    20. width
  4. 方法
    1. decode()
  5. 继承:
    1. HTMLElement
    2. 元素
    3. 节点
    4. EventTarget
  6. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLInputElement
    33. HTMLIsIndexElement
    34. HTMLKeygenElement
    35. HTMLLIElement
    36. HTMLLabelElement
    37. HTMLLegendElement
    38. HTMLLinkElement
    39. HTMLMapElement
    40. HTMLMediaElement
    41. HTMLMetaElement
    42. HTMLMeterElement
    43. HTMLModElement
    44. HTMLOListElement
    45. HTMLObjectElement
    46. HTMLOptGroupElement
    47. HTMLOptionElement
    48. HTMLOptionsCollection
    49. HTMLOutputElement
    50. HTMLParagraphElement
    51. HTMLParamElement
    52. HTMLPictureElement
    53. HTMLPreElement
    54. HTMLProgressElement
    55. HTMLQuoteElement
    56. HTMLScriptElement
    57. HTMLSelectElement
    58. HTMLShadowElement
    59. HTMLSourceElement
    60. HTMLSpanElement
    61. HTMLStyleElement
    62. HTMLTableCaptionElement
    63. HTMLTableCellElement
    64. HTMLTableColElement
    65. HTMLTableDataCellElement
    66. HTMLTableElement
    67. HTMLTableHeaderCellElement
    68. HTMLTableRowElement
    69. HTMLTableSectionElement
    70. HTMLTemplateElement
    71. HTMLTextAreaElement
    72. HTMLTimeElement
    73. HTMLTitleElement
    74. HTMLTrackElement
    75. HTMLUListElement
    76. HTMLUnknownElement
    77. HTMLVideoElement
    78. HashChangeEvent
    79. 历史
    80. ImageData
    81. 定位
    82. MessageChannel
    83. MessageEvent
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers