Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 811129 of <img>

  • 版本网址缩略名: Web/HTML/Element/img
  • 版本标题: <img>
  • 版本 id: 811129
  • 创建于:
  • 创建者: KikiAn
  • 是否是当前版本?
  • 评论
标签: 

修订内容

总结

HTML <img> 标签元素(或者 HTML Dom 中的 Image 对象)代表文档中的一个图像。

使用说明:
浏览器并不总是显示该元素中的图像。对于非图形浏览器(包括那些有视力障碍的人所使用的)来说就是这种情况,如果用户选择不显示图像,或者如果浏览器无法显示图像,因为它是无效的或 不支持的类型。在这些情况下,浏览器会用该元素的 alt 属性定义的文本来替换图像。

  • 内容类别流式内容段落内容,嵌入式内容,可触摸内容。如果元素有 {{htmlattrxref("usemap", "img")}} 属性,它也是交互内容的一部分。
  • 允许的内容None,它是一个 {{Glossary("空元素")}}.
  • 标签省略必须有一个开始标签,不允许有结束标签.
  • 允许的父元素接受嵌入式内容的任意元素.
  • DOM 接口{{domxref("HTMLImageElement")}}

 属性

这个元素包括 global 属性.

{{htmlattrdef("align")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}} 使用 {{cssxref('vertical-align')}} CSS 属性
图像相对于它周围上下文的对齐。
{{htmlattrdef("alt")}}
这个属性定义了描述图像的替换文本。用户将看到这个显示,如果图像的URL是错误的,该图像不在 支持的格式 列表中,或者如果还没有下载图像。

使用说明: 省略这个属性表明该图像是内容的关键部分,但没有文本等效可用。将这个属性设置为空字符串表明该图像不是内容的关键部分,非可视化浏览器在渲染的时候可能会忽略该图像。

{{htmlattrdef("border")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
图像周围的边框宽度。
{{htmlattrdef("crossorigin")}} {{HTMLVersionInline(5)}}
这个枚举属性表明是否必须使用 CORS 完成相关图像的抓取。启用CORS的图像 在 {{HTMLElement("canvas")}} 元素中可以重复使用而不会被污染。允许的值有:
anonymous
A cross-origin request (i.e., with Origin: HTTP header) is performed. But no credential is sent (i.e., no cookie, no X.509 certificate, and no HTTP Basic authentication is sent). If the server does not give credentials to the origin site (by not setting the Access-Control-Allow-Origin: HTTP header), the image will be tainted and its usage restricted.
use-credentials
A cross-origin request (i.e., with Origin: HTTP header) performed with credential is sent (i.e., a cookie, a certificate, and HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through Access-Control-Allow-Credentials: HTTP header), the image will be tainted and its usage restricted.
When not present, the resource is fetched without a CORS request (i.e., without sending the Origin: HTTP header), preventing its non-tainted usage in {{HTMLElement('canvas')}} elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information.
{{htmlattrdef("height")}}
图像的高度,在 {{HTMLVersionInline(5)}} 中的单位是 CSS 像素,在 {{HTMLVersionInline(4)}} 中可以是像素也可以是百分比。
{{htmlattrdef("hspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
插入到图像的左侧和右侧的空白像素的值。
{{htmlattrdef("ismap")}}
这个布尔属性表示图像是否是服务器端map的一部分。如果是, 那么点击的精准坐标将会被发送到服务器。

Usage note: This attribute is allowed only if the <img> element is a descendant of an {{htmlelement("a")}} element with a valid {{htmlattrxref("href","a")}} attribute.

{{htmlattrdef("longdesc")}}{{HTMLVersionInline(4)}} only
URL 描述要显示图像的URL描述,是对 alt 文本的补充。
{{htmlattrdef("name")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
元素的名字。在 {{HTMLVersionInline(4)}} 只向后兼容。 使用 id 属性替代。
{{htmlattrdef("sizes")}}{{HTMLVersionInline(5)}}
A list of one or more strings separated by commas indicating a set of source sizes. Each source size consists of:
  1. a media condition. This must be omitted for the last item.
  2. a source size value.

Source size values are used to specify the intended size of the image, for the purpose of selecting a source from the list supplied by the srcset attribute when it uses 'w' descriptors. The selected size affects the intrinsic size of the image (images inherent size if no explicit CSS styling is applied). If the srcset attribute is absent, or contains no values with a width descriptor, then the sizes attribute has no effect.

{{htmlattrdef("src")}}
图像的 URL,这个属性对 <img> 元素来说是必需的。在支持 srcset 的浏览器中,src 被当做拥有一个像素密度的描述符 1x 的候选图像处理,除非一个图像拥有这个像素密度描述符已经被在 srcset 或者 srcset 包含 'w' 描述符中定义了.
 
{{htmlattrdef("srcset")}}{{HTMLVersionInline(5)}}
A list of one or more strings separated by commas indicating a set of possible images for the user agent to use. 每一个字符串由以下组成:
  1. 一个图像的 URL
  2. 可选的,whitespace followed 以下任选其一:
    • 一个宽度描述符,那是一个相对的整数 直接跟在 'w' 后面。宽度描述符被隔开 by the source size given in the sizes attribute to calculate the effective pixel density.
    • 一个像素密度描述符,那是一个相对的浮点数,直接跟在 'x' 后面。

如果没有指定源描述符,那它会被指定为默认的 1x。

在相同的 srcset 属性中混合使用宽度描述符和像素密度描述符时,会导致该值无效。重复的描述符(比如,两个源 在相同的srcset两个源都是 '2x')也是无效的。

浏览器选择在给出的时间点显示大部分 adequate 图片。

{{htmlattrdef("width")}}
图像的宽度,在 {{HTMLVersionInline(5)}} 中单位是 CSS 像素, 在 {{HTMLVersionInline(4)}} 中可以是像素也可以是百分比。
{{htmlattrdef("usemap")}}
与元素相关联的的 image map 的部分 URL(以 '#' 开始的部分)。

使用说明: 你不能使用这个属性,如果 <img> 元素是 {{htmlelement("a")}} 或 {{HTMLElement("button")}} 元素的后代元素。

{{htmlattrdef("vspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
插入到图像的上方和下方的空白像素的数组。

支持的图像格式

HTML 标准并没有给出必须支持的图像格式的列表,因此每个用户代理支持一组不同的格式。 Gecko 支持:

  • JPEG
  • GIF,包括动态的GIFs
  • PNG
  • APNG {{gecko_minversion_inline("1.9.2")}}
  • SVG {{gecko_minversion_inline("2.0")}}
  • BMP
  • BMP ICO
  • PNG ICO {{gecko_minversion_inline("9.0")}}

说明:
对 XBM 格式的支持已经在 Gecko 的 1.9.2 版本中被移除了。

与 CSS 的交互

关于 CSS,<img> 是一个 替换元素。它没有基线,这意味着当在一个行内格式的上下文中使用 {{cssxref("vertical-align")}}: baseline 时,图像的底部将会与容器的基线对齐。

根据它的类型,图像可能会有一个本征维数(intrinsic dimension),但这不是一个必要条件:SVG图像就没有,而光栅图像有。

例 1

<img src="mdn-logo-sm.png" alt="MDN">

MDN

例 2:图像链接 

<a href="https://developer.mozilla.org/"><img src="mdn-logo-sm.png" alt="MDN"></a>

MDN

例 3:使用 srcset 属性

在支持 srcset 的用户代理中,src 属性是 1x 候选项。

 <img src="mdn-logo-sm.png" 
      alt="MDN" 
      srcset="mdn-logo-HD.png 2x">

例 4:使用 srcset 和 sizes 属性

在支持 srcset 的用户代理中,当使用 'w' 描述符时,src 属性会被忽略。当匹配了媒体条件 (min-width: 600px) 时,图像将宽 200px,否则宽 50vw(视图宽度的50%)。

 <img src="clock-demo-thumb-200.png" 
      alt="Clock" 
      srcset="clock-demo-thumb-200.png 200w, clock-demo-thumb-400.png 400w"
      sizes="(min-width: 600px) 200px, 50vw">

规范

规范 状态 说明
{{SpecName('HTML WHATWG', 'embedded-content.html#the-img-element', '<img>')}} {{Spec2('HTML WHATWG')}}  
{{SpecName('HTML5 W3C', 'embedded-content-0.html#the-img-element', '<img>')}} {{Spec2('HTML5 W3C')}}  
{{SpecName('HTML4.01', 'struct/objects.html#h-13.2', '<img>')}} {{Spec2('HTML4.01')}}  

浏览器的兼容性

{{CompatibilityTable}}

特性 Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
基本支持 {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
srcset 属性 {{CompatChrome(34.0)}} {{CompatGeckoDesktop("32.0")}} (behind a pref) {{CompatNo}} {{CompatOpera(21)}} {{CompatSafari(7.1)}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
基本支持 {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
srcset 属性 {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} iOS 8

另见

  • {{HTMLElement("object")}} 和 {{HTMLElement("embed")}} 元素
{{HTMLRef}}

修订版来源

<h2 id=".E6.80.BB.E7.BB.93">总结</h2>

<p><strong>HTML <code>&lt;img&gt;</code>&nbsp;标签元素</strong>(或者 HTML Dom 中的<em>&nbsp;Image 对象</em>)代表文档中的一个图像。</p>

<div class="note">
<p><strong>使用说明:</strong><br />
 浏览器并不总是显示该元素中的图像。对于非图形浏览器(包括那些有视力障碍的人所使用的)来说就是这种情况,如果用户选择不显示图像,或者如果浏览器无法显示图像,因为它是无效的或&nbsp;<a href="#Supported_image_formats" title="HTML/Element/Img#Supported_Image_Formats">不支持的类型</a>。在这些情况下,浏览器会用该元素的&nbsp;<strong>alt</strong>&nbsp;属性定义的文本来替换图像。</p>
</div>

<ul class="htmlelt">
 <li><dfn><a href="/en-US/docs/HTML/Content_categories" title="HTML/Content_categories">内容类别</a></dfn><a href="/en-US/docs/HTML/Content_categories#Flow_content" title="HTML/Content categories#Flow content">流式内容</a>,<a href="/en-US/docs/HTML/Content_categories#Phrasing_content" title="HTML/Content categories#Phrasing content">段落内容</a>,嵌入式内容,可触摸内容。如果元素有&nbsp;{{htmlattrxref("usemap", "img")}} 属性,它也是交互内容的一部分。</li>
 <li><dfn>允许的内容</dfn>None,它是一个&nbsp;{{Glossary("空元素")}}.</li>
 <li><dfn>标签省略</dfn>必须有一个开始标签,不允许有结束标签.</li>
 <li><dfn>允许的父元素</dfn>接受嵌入式内容的任意元素.</li>
 <li><dfn>DOM 接口</dfn>{{domxref("HTMLImageElement")}}</li>
</ul>

<p>&nbsp;<strong style="font-size:2.14285714285714rem; font-weight:700; letter-spacing:-1px; line-height:30px">属性</strong></p>

<p><span style="line-height:21px">这个元素包括&nbsp;</span><a href="https://developer.mozilla.org/en-US/docs/HTML/Global_attributes" style="line-height: 21px;" title="HTML/Global attributes">global 属性</a><span style="line-height:21px">.</span></p>

<dl>
 <dt>{{htmlattrdef("align")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}} 使用&nbsp;{{cssxref('vertical-align')}} CSS 属性</dt>
 <dd>图像相对于它周围上下文的对齐。</dd>
 <dt>{{htmlattrdef("alt")}}</dt>
 <dd>这个属性定义了描述图像的替换文本。用户将看到这个显示,如果图像的URL是错误的,该图像不在&nbsp;<a href="#Supported_image_formats" title="HTML/Element/Img#Supported image formats">支持的格式</a>&nbsp;列表中,或者如果还没有下载图像。
 <div class="note">
 <p><strong>使用说明:&nbsp;</strong>省略这个属性表明该图像是内容的关键部分,但没有文本等效可用。将这个属性设置为空字符串表明该图像不是内容的关键部分,非可视化浏览器在渲染的时候可能会忽略该图像。</p>
 </div>
 </dd>
 <dt>{{htmlattrdef("border")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>图像周围的边框宽度。</dd>
 <dt>{{htmlattrdef("crossorigin")}} {{HTMLVersionInline(5)}}</dt>
 <dd>这个枚举属性表明是否必须使用&nbsp;CORS 完成相关图像的抓取。<a href="/en-US/docs/CORS_Enabled_Image" title="CORS_Enabled_Image">启用CORS的图像</a>&nbsp;在&nbsp;{{HTMLElement("canvas")}} 元素中可以重复使用而不会被污染。<em>允许的值有:</em>
 <dl>
  <dt>anonymous</dt>
  <dd>A cross-origin request (i.e., with <code>Origin:</code> HTTP header) is performed. But no credential is sent (i.e., no cookie, no X.509 certificate, and no HTTP Basic authentication is sent). If the server does not give credentials to the origin site (by not setting the <code>Access-Control-Allow-Origin:</code> HTTP header), the image will be <em>tainted</em> and its usage restricted.</dd>
  <dt>use-credentials</dt>
  <dd>A cross-origin request (i.e., with <code>Origin:</code> HTTP header) performed with credential is sent (i.e., a cookie, a certificate, and HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through <code>Access-Control-Allow-Credentials:</code> HTTP header), the image will be <em>tainted</em> and its usage restricted.</dd>
 </dl>
 When not present, the resource is fetched without a CORS request (i.e., without sending the <code>Origin:</code> HTTP header), preventing its non-tainted usage in {{HTMLElement('canvas')}} elements. If invalid, it is handled as if the enumerated keyword <strong>anonymous</strong> was used. See <a href="mailto:[email protected]" title="CORS settings attributes">CORS settings attributes</a> for additional information.</dd>
 <dt>{{htmlattrdef("height")}}</dt>
 <dd>图像的高度,在 {{HTMLVersionInline(5)}} 中的单位是 CSS 像素,在&nbsp;{{HTMLVersionInline(4)}} 中可以是像素也可以是百分比。</dd>
 <dt>{{htmlattrdef("hspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>插入到图像的左侧和右侧的空白像素的值。</dd>
 <dt>{{htmlattrdef("ismap")}}</dt>
 <dd>这个布尔属性表示图像是否是服务器端map的一部分。如果是, 那么点击的精准坐标将会被发送到服务器。
 <div class="note">
 <p><strong>Usage note: </strong>This attribute is allowed only if the <code>&lt;img&gt;</code> element is a descendant of an {{htmlelement("a")}} element with a valid {{htmlattrxref("href","a")}} attribute.</p>
 </div>
 </dd>
 <dt>{{htmlattrdef("longdesc")}}{{HTMLVersionInline(4)}} only</dt>
 <dd>URL 描述要显示图像的URL描述,是对&nbsp;<strong>alt</strong>&nbsp;文本的补充。</dd>
 <dt>{{htmlattrdef("name")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>元素的名字。在&nbsp;{{HTMLVersionInline(4)}} 只向后兼容。&nbsp;使用&nbsp;<strong>id</strong>&nbsp;属性替代。</dd>
 <dt>{{htmlattrdef("sizes")}}{{HTMLVersionInline(5)}}</dt>
 <dd>A&nbsp;list of one or more strings separated by commas indicating a set of source sizes. Each source size consists of:
 <ol>
  <li>a media condition. This must be omitted for the last item.</li>
  <li>a source size value.</li>
 </ol>

 <p>Source size&nbsp;values are used to specify the intended size of the image, for the purpose of selecting a source from the list supplied by the <code>srcset</code> attribute when it uses '<code>w</code>' descriptors. The selected size affects&nbsp;the intrinsic size of the image (images inherent size if no explicit CSS styling is applied). If the <code>srcset</code> attribute is absent, or contains no values with a width descriptor, then the <code>sizes</code> attribute has no effect.</p>
 </dd>
 <dt>{{htmlattrdef("src")}}</dt>
 <dd>图像的 URL,这个属性对&nbsp;<code>&lt;img&gt;</code>&nbsp;元素来说是必需的。在支持&nbsp;<code>srcset 的浏览器中,</code><code>src</code>&nbsp;被当做拥有一个像素密度的描述符&nbsp;<code>1x</code>&nbsp;的候选图像处理,除非一个图像拥有这个像素密度描述符已经被在&nbsp;<code>srcset</code>&nbsp;或者&nbsp;<code>srcset</code>&nbsp;包含 '<code>w</code>'&nbsp;描述符中定义了.</dd>
 <dt>&nbsp;</dt>
 <dt>{{htmlattrdef("srcset")}}{{HTMLVersionInline(5)}}</dt>
 <dd>A list of one or more strings separated by commas indicating a set of possible images for the user agent to use. 每一个字符串由以下组成:
 <ol>
  <li>一个图像的&nbsp;URL</li>
  <li>可选的,whitespace followed 以下任选其一:
   <ul>
    <li>一个宽度描述符,那是一个相对的整数 直接跟在&nbsp;'<code>w</code>' 后面。宽度描述符被隔开&nbsp;by the source size given in the&nbsp;<code>sizes</code>&nbsp;attribute to calculate the effective pixel density.</li>
    <li>一个像素密度描述符,那是一个相对的浮点数,直接跟在&nbsp;'<code>x</code>' 后面。</li>
   </ul>
  </li>
 </ol>

 <p>如果没有指定源描述符,那它会被指定为默认的&nbsp;<code style="font-style: normal;">1x。</code></p>

 <p>在相同的&nbsp;<code style="font-style: normal;">srcset</code>&nbsp;属性中混合使用宽度描述符和像素密度描述符时,会导致该值无效。重复的描述符(比如,两个源 在相同的srcset两个源都是&nbsp;'2<code style="font-style: normal;">x</code>')也是无效的。</p>

 <p>浏览器选择在给出的时间点显示大部分&nbsp;adequate 图片。</p>
 </dd>
 <dt>{{htmlattrdef("width")}}</dt>
 <dd>图像的宽度,在&nbsp;{{HTMLVersionInline(5)}} 中单位是&nbsp;CSS 像素,&nbsp;在&nbsp;{{HTMLVersionInline(4)}} 中可以是像素也可以是百分比。</dd>
 <dt>{{htmlattrdef("usemap")}}</dt>
 <dd>与元素相关联的的&nbsp;<a href="/en-US/docs/HTML/Element/map" title="HTML/Element/Map">image map</a>&nbsp;的部分 URL(以&nbsp;'#' 开始的部分)。
 <div class="note">
 <p><strong>使用说明:&nbsp;</strong>你不能使用这个属性,如果&nbsp;<code>&lt;img&gt;</code>&nbsp;元素是 {{htmlelement("a")}} 或&nbsp;{{HTMLElement("button")}} 元素的后代元素。</p>
 </div>
 </dd>
 <dt>{{htmlattrdef("vspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}</dt>
 <dd>插入到图像的上方和下方的空白像素的数组。</dd>
</dl>

<h2 id=".E6.94.AF.E6.8C.81.E7.9A.84.E5.9B.BE.E5.83.8F.E6.A0.BC.E5.BC.8F">支持的图像格式</h2>

<p>HTML 标准并没有给出必须支持的图像格式的列表,因此每个用户代理支持一组不同的格式。&nbsp;Gecko 支持:</p>

<ul>
 <li><a class="external" href="https://en.wikipedia.org/wiki/JPEG" title="https://en.wikipedia.org/wiki/JPEG">JPEG</a></li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/Graphics_Interchange_Format" title="https://en.wikipedia.org/wiki/Graphics_Interchange_Format">GIF</a>,包括动态的GIFs</li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/Portable_Network_Graphics" title="https://en.wikipedia.org/wiki/Portable_Network_Graphics">PNG</a></li>
 <li><a href="/en-US/docs/Animated_PNG_graphics" title="Animated PNG graphics">APNG</a> {{gecko_minversion_inline("1.9.2")}}</li>
 <li><a href="/en-US/docs/SVG" title="SVG">SVG</a> {{gecko_minversion_inline("2.0")}}</li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/BMP_file_format" title="https://en.wikipedia.org/wiki/BMP_file_format">BMP</a></li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/ICO_%28file_format%29" title="https://en.wikipedia.org/wiki/ICO_%28file_format%29">BMP ICO</a></li>
 <li><a class="external" href="https://en.wikipedia.org/wiki/ICO_%28file_format%29" title="https://en.wikipedia.org/wiki/ICO_%28file_format%29">PNG ICO</a> {{gecko_minversion_inline("9.0")}}</li>
</ul>

<div class="note">
<p><strong>说明:</strong><br />
 对&nbsp;<a class="external" href="https://en.wikipedia.org/wiki/X_BitMap" title="https://en.wikipedia.org/wiki/X_BitMap">XBM</a>&nbsp;格式的支持已经在&nbsp;Gecko 的 1.9.2 版本中被移除了。</p>
</div>

<h2 id=".E4.B8.8E_CSS_.E7.9A.84.E4.BA.A4.E4.BA.92">与 CSS 的交互</h2>

<p>关于 CSS,<code>&lt;img&gt;</code>&nbsp;是一个&nbsp;<a href="/en-US/docs/CSS/Replaced_element" title="CSS/Replaced_element">替换元素</a>。它没有基线,这意味着当在一个行内格式的上下文中使用&nbsp;{{cssxref("vertical-align")}}<code>: baseline 时,图像的底部将会与容器的基线对齐。</code></p>

<p>根据它的类型,图像可能会有一个本征维数(intrinsic dimension),但这不是一个必要条件:SVG图像就没有,而光栅图像有。</p>

<h2 id=".E4.BE.8B_1">例 1</h2>

<pre class="brush: html">
&lt;img src="mdn-logo-sm.png" alt="MDN"&gt;
</pre>

<p><img alt="MDN" src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" /></p>

<h2 id=".E4.BE.8B_2.EF.BC.9A.E5.9B.BE.E5.83.8F.E9.93.BE.E6.8E.A5">例 2:图像链接&nbsp;</h2>

<pre class="brush: html">
&lt;a href="https://developer.mozilla.org/"&gt;&lt;img src="mdn-logo-sm.png" alt="MDN"&gt;&lt;/a&gt;</pre>

<p><a href="https://developer.mozilla.org/"><img alt="MDN" src="https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png" /> </a></p>

<h2 id="Specifications" name="Specifications">例&nbsp;3:使用&nbsp;<code>srcset</code>&nbsp;属性</h2>

<p>在支持 <span style="font-family:consolas,monaco,andale mono,monospace">srcset 的用户代理中,</span><code>src</code>&nbsp;属性是&nbsp;<code>1x</code>&nbsp;候选项。</p>

<pre class="brush: html">
 &lt;img src="mdn-logo-sm.png" 
      alt="MDN" 
      srcset="mdn-logo-HD.png 2x"&gt;</pre>

<h2 id="Specifications" name="Specifications" style="line-height: 30px; font-size: 2.14285714285714rem;">例&nbsp;4:使用&nbsp;<code>srcset</code>&nbsp;和&nbsp;<code>sizes</code>&nbsp;属性</h2>

<p>在支持&nbsp;<code>srcset 的用户代理中,当使用</code>&nbsp;'<code>w</code>' 描述符时,src 属性会被忽略。当匹配了媒体条件&nbsp;<code>(min-width: 600px)</code>&nbsp;时,图像将宽&nbsp;200px,否则宽&nbsp;50vw(视图宽度的50%)。</p>

<pre class="brush: html" style="font-size: 14px; word-spacing: 0px;">
 &lt;img src="clock-demo-thumb-200.png" 
      alt="Clock" 
      srcset="clock-demo-thumb-200.png 200w, clock-demo-thumb-400.png 400w"
&nbsp;     sizes="(min-width: 600px) 200px, 50vw"&gt;</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-img-element', '&lt;img&gt;')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', 'embedded-content-0.html#the-img-element', '&lt;img&gt;')}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML4.01', 'struct/objects.html#h-13.2', '&lt;img&gt;')}}</td>
   <td>{{Spec2('HTML4.01')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器的兼容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>特性</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>基本支持</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>srcset 属性</td>
   <td>{{CompatChrome(34.0)}}</td>
   <td>{{CompatGeckoDesktop("32.0")}} (behind a pref)</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera(21)}}</td>
   <td>{{CompatSafari(7.1)}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>基本支持</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>srcset 属性</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>iOS 8</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id=".E5.8F.A6.E8.A7.81">另见</h2>

<ul>
 <li>{{HTMLElement("object")}} 和&nbsp;{{HTMLElement("embed")}} 元素</li>
</ul>

<div>{{HTMLRef}}</div>
恢复到这个版本