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 965453 of <img>

  • 版本网址缩略名: Web/HTML/Element/img
  • 版本标题: <img>
  • 版本 id: 965453
  • 创建于:
  • 创建者: fuchao2012
  • 是否是当前版本?
  • 评论 翻译了 CORS 部分
标签: 

修订内容

总结

HTML Image 元素( <img> )代表文档中的一个图像。

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

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

 属性

这个元素包括 全局 属性

{{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
执行一个跨域的请求(比如,有 Origin: HTTP header)。但是没有发送证书(比如,没有 cookie,没有 X.509 证书,没有 HTTP 基本的授权认证)。如果服务器没有给源站证书(没有设置 Access-Control-Allow-Origin: HTTP头),图像会被污染而且它的使用会被限制。
use-credentials
一个有证书的跨域请求(比如,有 Origin: HTTP header)被发送 (比如,a cookie, a certificate, and HTTP Basic authentication is performed)。如果服务器没有给源站发送证书(通过 Access-Control-Allow-Credentials: HTTP header),图像将会被污染,且它的使用会受限制。
当用户并未显式使用本属性时,默认不使用 CORS 发起请求(例如,不会向服务器发送 HTTP 头部信息),用以防止其在{{HTMLElement('canvas')}}中的使用。如果无效,默认当做anonymous关键字生效。更多信息,请查看 CORS 属性设置 。
 
{{htmlattrdef("height")}}
图像的高度,在 {{HTMLVersionInline(5)}} 中的单位是 CSS 像素,在 {{HTMLVersionInline(4)}} 中可以是像素也可以是百分比。
{{htmlattrdef("hspace")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
插入到图像的左侧和右侧的空白像素的值。
{{htmlattrdef("ismap")}}
这个布尔属性表示图像是否是服务器端map的一部分。如果是, 那么点击的精准坐标将会被发送到服务器。

使用说明:只有当 <img> 是 {{htmlattrxref("href","a")}} 属性是有效的 {{htmlelement("a")}} 元素的后代元素时,这个属性才被允许使用。

{{htmlattrdef("longdesc")}}{{HTMLVersionInline(4)}} only
URL 描述要显示图像的URL描述,是对 alt 文本的补充。
{{htmlattrdef("name")}} {{deprecatedGeneric('inline','HTML4.01')}}, {{obsoleteGeneric('inline','HTML5')}}
元素的名字。在 {{HTMLVersionInline(4)}} 只向后兼容。 使用 id 属性替代。
{{htmlattrdef("sizes")}}{{HTMLVersionInline(5)}}
表示资源大小的以逗号隔开的一个或多个字符串。每一个资源大小包括:
  1. 一个媒体条件。最后一项一定是忽略了的。
  2. 一个资源尺寸的值。

资源尺寸的值被用来指定图像的预期尺寸,因为当它使用 'w' 描述符要从 srcset 属性提供的列表中选择一个资源。被选中的尺寸影响图像的内在尺寸大小(如果没有冲突的CSS样式那么就会使用图像的内在尺寸)。如果没有srcset,或者没值,那么这个尺寸属性也就没什么作用了。

{{htmlattrdef("src")}}
图像的 URL,这个属性对 <img> 元素来说是必需的。在支持 srcset 的浏览器中,src 被当做拥有一个像素密度的描述符 1x 的候选图像处理,除非一个图像拥有这个像素密度描述符已经被在 srcset 或者 srcset 包含 'w' 描述符中定义了.
 
{{htmlattrdef("srcset")}}{{HTMLVersionInline(5)}}
以逗号分隔的一个或多个字符串列表表明一系列用户代理使用的可能的图像。每一个字符串由以下组成:
  1. 一个图像的 URL。
  2. 可选的,空格后跟以下的其一:
    • 一个宽度描述符,那是一个相对的整数 直接跟在 'w' 后面。宽度描述符被sizes属性给出的资源大小隔开以计算有效的像素密度。
    • 一个像素密度描述符,那是一个相对的浮点数,直接跟在 '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="总结">总结</h2>

<p><em>HTML Image 元素</em>(&nbsp;<code>&lt;img&gt;</code>&nbsp;)代表文档中的一个图像。</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">全局 属性</a>。</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>执行一个跨域的请求(比如,有&nbsp;<code>Origin:</code> HTTP header)。但是没有发送证书(比如,没有&nbsp;cookie,没有&nbsp;X.509 证书,没有&nbsp;HTTP 基本的授权认证)。如果服务器没有给源站证书(没有设置&nbsp;Access-Control-Allow-Origin:&nbsp;HTTP头),图像会被污染而且它的使用会被限制。</dd>
  <dt>use-credentials</dt>
  <dd>一个有证书的跨域请求(比如,有&nbsp;<code>Origin:</code> HTTP header)被发送&nbsp;(比如,a cookie, a certificate, and HTTP Basic authentication is performed)。如果服务器没有给源站发送证书(通过 Access-Control-Allow-Credentials: HTTP header),图像将会被污染,且它的使用会受限制。</dd>
 </dl>
 当用户并未显式使用本属性时,默认不使用 CORS 发起请求(例如,不会向服务器发送 HTTP 头部信息),用以防止其在{{HTMLElement('canvas')}}中的使用。如果无效,默认当做<strong>anonymous</strong>关键字生效。更多信息,请查看&nbsp;<a href="mailto:[email protected]" title="CORS settings attributes">CORS 属性设置</a>&nbsp;。</dd>
 <dt>&nbsp;</dt>
 <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>使用说明:</strong>只有当&nbsp;<code>&lt;img&gt;</code>&nbsp;是&nbsp;{{htmlattrxref("href","a")}} 属性是有效的 {{htmlelement("a")}} 元素的后代元素时,这个属性才被允许使用。</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>表示资源大小的以逗号隔开的一个或多个字符串。每一个资源大小包括:
 <ol>
  <li>一个媒体条件。最后一项一定是忽略了的。</li>
  <li>一个资源尺寸的值。</li>
 </ol>

 <p>资源尺寸的值被用来指定图像的预期尺寸,因为当它使用 'w' 描述符要从 srcset 属性提供的列表中选择一个资源。被选中的尺寸影响图像的内在尺寸大小(如果没有冲突的CSS样式那么就会使用图像的内在尺寸)。如果没有srcset,或者没值,那么这个尺寸属性也就没什么作用了。</p>
 </dd>
 <dt>{{htmlattrdef("src")}}</dt>
 <dd>图像的 URL,这个属性对&nbsp;<code>&lt;img&gt;</code>&nbsp;元素来说是必需的。在支持 <code><strong>srcset</strong></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>以逗号分隔的一个或多个字符串列表表明一系列用户代理使用的可能的图像。每一个字符串由以下组成:
 <ol>
  <li>一个图像的&nbsp;URL。</li>
  <li>可选的,空格后跟以下的其一:
   <ul>
    <li>一个宽度描述符,那是一个相对的整数 直接跟在&nbsp;'<code>w</code>' 后面。宽度描述符被sizes属性给出的资源大小隔开以计算有效的像素密度。</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="支持的图像格式">支持的图像格式</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="与_CSS_的交互">与 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="例_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="例_2:图像链接">例 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="另见">另见</h2>

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

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