HTMLImageElement property alt provides fallback (alternate) text to display when the image specified by the <img> element is not loaded. This may be the case because of an error, because the user has disabled the loading of images, or because the image simply hasn't finished loading yet.

Perhaps the most important reason to use the alt tag is to support accessibility , as the alt text may be used by screen readers and other assistive technologies to help differently-abled users make full use of your content. It will be read aloud or sent to the touch device, for example, to support visually impaired users.

Think of it like this: When choosing alt strings for your images, imagine what you would say when reading the page to someone over the phone without mentioning that there's an image on the page.

The alternate text is displayed in the space the image would occupy and should be able to take the place of the image without altering the meaning of the page .

句法

htmlImageElement.alt = altText;
let altText = htmlImageElement.alt;
					

A DOMString which contains the alternate text to display when the image is not loaded or for use by assistive devices.

alt attribute is officially mandatory; it's meant to always be specified. If the image doesn't require a fallback (such as for an image which is simply decorative or an advisory icon of minimal importance), you may specify an empty string ( "" ). For compatibility reasons, browsers generally will accept an image without an alt attribute, but you should try to get into the habit of using it.

用法注意事项

The fundamental guideline for the alt attribute is that every image's alternate text should be able to replace the image without altering the meaning of the page . You should never use alt for text that could be construed as a caption or title. There are separate attributes and elements designed for those purposes.

范例

Beyond that, there are additional guidelines for using alt appropriately which vary depending on what the image is being used for. These are shown in the examples below.

Decorative images

Images with no semantic meaning—such as those which are solely decorative—or of limited informational value, should have their alt attributes set to the empty string ( "" ). This is shown in the example below.

HTML

In the HTML for this example, shown below, the <img> element includes the alt property, which will prevent the image from having any alternate text, since it's simply a decorational detail.

<div class="container">
  <div class="left-margin">
    <img src="/files/16861/margin-flourish.svg" >
  </div>
  <div class="contents">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque quis orci ligula. Lorem ipsum dolor sit
    amet, consectetur adipiscing elit. In ac neque libero.
    Vivamus consectetur rhoncus elit eget porta. Etiam
    pulvinar ex id sapien laoreet, quis aliquet odio
    lobortis. Nam ac mauris at risus laoreet cursus vitae et
    sapien. Etiam molestie auctor eros, ac porta risus
    scelerisque sit amet. Ut nunc neque, porta eu auctor at,
    tempor et arcu.</p>
  </div>
</div>
					

CSS

The CSS for this example simply sets up the styles for the layout as shown in the result below. They're not crucial to the concept shown in the HTML above, but you may expand the view below if you wish to read the CSS used.

View the CSS

结果

Images used as buttons

When using an image as a button (by using it as the only visible child of an <a> element representing a hyperlink), the alt attribute must be used to convey the purpose of the button. In other words, it should be the same text you would use in a textual button to serve the same purpose.

For example, in the snippet of HTML below, a toolbar which uses icon images as link labels provides alt attributes for each giving a textual label to use instead of the icon when the icons cannot be or are intentionally not used.

<li class="toolbar" aria-role="toolbar">
  <a href="songs.html" aria-role="button"><img src="songicon.svg" alt="Songs"</a>
  <a href="albums.html" aria-role="button"><img src="albumicon.svg" alt="Albums"</a>
  <a href="artists.html" aria-role="button"><img src="artisticon.svg" alt="Artists"</a>
  <a href="playlists.html" aria-role="button"><img src="playlisticon.svg" alt="Playlists"</a>
</li>
						

Images containining diagrams or maps

When an image contains information presented as a diagram, chart, graph, or map, the alt text should provide the same information, at least in summary form. This is true whether the /me image is in a bitmapped format such as PNG or JPEG or in a vector format like SVG .

  • For a map, the alt text could be directions to the place indicated by the map, similarly to how you would explain it verbally.
  • For a chart, the text could describe the items in the chart and their values.
  • For a diagram, the text could be an explanation of the concept presented by the diagram.

Keep in mind that any text included in diagrams and charts presented in SVG format may be read by screen readers. This may impact the decisions you make shen writing the alt text for the diagram.

Icons or logos

Logos (such as corporate or brand logos) and informational icons should use the corresponding text in their alt strings. That is, if an image is a corporate logo, the alt text should be the name of the company. If the image is an icon representing a status or other information, the text should be the name of that state.

For example, if an image is a badge that's shown on a page to indicate that the content of the page is new and has been updated recently, the corresponding alt text might be " Updated Recently" or even " Updated on 27 August 2019" .

In this example, a starburst image with the word "New!" is used to indicate that an article is about something new (and probably supposedly also exciting). The alt attribute is set to New Page! to allow that text to be displayed in place of the image if the image isn't available. It is also available to be read by screen readers.

HTML

The HTML below creates a snippet of content from a site using the described icon. Note the use of the alt 属性在 <<img>> , providing a good substitution string to use in case the image doesn't load.

<div class="container">
  <img src="https://www.bitstampede.com/mdn-test/new-page.svg"
       alt="New Page!" class="pageinfo-badge">
  <p class="contents">
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque quis orci ligula. Lorem ipsum dolor sit
    amet, consectetur adipiscing elit. In ac neque libero.
    Vivamus consectetur rhoncus elit eget porta. Etiam
    pulvinar ex id sapien laoreet, quis aliquet odio
    lobortis. Nam ac mauris at risus laoreet cursus vitae et
    sapien. Etiam molestie auctor eros, ac porta risus
    scelerisque sit amet. Ut nunc neque, porta eu auctor at,
    tempor et arcu.
  </p>
</div>
						

CSS

The main feature of the CSS here is the use of clip-path and shape-outside to wrap the text around the icon in an appealing way.

.container {
  max-width: 500px;
}
.pageinfo-badge {
  width: 9em;
  padding-right: 1em;
  float: left;
  clip-path: polygon(100% 0, 100% 50%, 90% 70%, 80% 80%,
             70% 90%, 50% 100%, 0 100%, 0 0);
  shape-outside: polygon(100% 0, 100% 50%, 90% 70%, 80% 80%,
             70% 90%, 50% 100%, 0 100%, 0 0);
}
.contents {
  margin-top: 1em;
  font: 16px Arial,Helvetica,Verdana,sans-serif;
}
						

结果

Other images

Images simply showing objects or scenes should have alt text describing what's seen in the image. A photo of a yellow teapot might literally have its alt attribute set to " A yellow teapot" .

规范

规范 状态 注释
HTML 实时标准
The definition of 'HTMLImageElement.alt' 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
alt Chrome 1 Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android ? Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

元数据

  • 最后修改:
  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