简体   繁体   English

后备图为<PICTURE>

[英]Fallback picture for <PICTURE>

Is <PICTURE> officially in HTML5? HTML4中是否正式<PICTURE> I can't find it in w3schools.com. 我在w3schools.com找不到它。

Assuming it is official, is the source attribute of the fallback <img> element src or srcset . 假设它是官方的,是fallback <img>元素srcsrcset的source属性。 I am seeing some web sites using srcset and they don't work in any version of IE, but src works in IE. 我看到一些使用srcset网站,它们不适用于任何版本的IE,但src适用于IE。

Yes, the <picture> tag is an official part of the standard, but HTML5 is a living standard, and this tag is considered "experimental". 是的, <picture>标签是标准的官方部分,但HTML5是一种生活标准,这个标签被认为是“实验性的”。

The <picture> tag is currently accepted into the WHATWG HTML5 Living Standard . <picture>标签目前已被WHATWG HTML5生活标准所接受。 That does not mean it is set in stone, and it is still considered an "experimental technology". 这并不意味着它是一成不变的,它仍然被认为是一种“实验技术”。 You should not depend on browsers to support this technology. 您不应该依赖浏览器来支持这项技术。 You can see an (unofficial) overview of browser support here . 您可以在此处查看(非官方)浏览器支持概述。

As stated on the Mozilla Developer's Network: 正如Mozilla开发人员网络上所述:

This is an experimental technology. 这是一项实验技术。 Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. 由于此技术的规范尚未稳定,请检查兼容性表以获取在各种浏览器中使用的正确前缀。 Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes. 另请注意,随着规范的变化,实验技术的语法和行为可能会在未来版本的浏览器中发生变化。

The " living standard " model of standardizing HTML means that elements are accepted more easily but may not remain in the standard or may be changed if a good argument is made against their current use. 标准化HTML的“ 生活标准 ”模型意味着元素被更容易接受,但可能不会保留在标准中,或者如果针对其当前使用提出了良好的论据,则可能会被更改。

The <img> element must be included, and this has the side-effect of offering a fall-back option. 必须包含<img>元素,这具有提供后备选项的副作用。

The picture element, by necessity, must have an <img> tag inside it, alongside the <source> elements. 必要时,picture元素必须在<source>元素旁边有一个<img>标记。 This has the side-effect of providing a fall-back option, but is also necessary to provide the base image and metadata (especially to provide the required alt attribute). 这具有提供后备选项的副作用,但也需要提供基本图像和元数据(尤其是提供所需的alt属性)。 The <source> elements merely override the src attribute of the <img> tag (under specified circumstances). <source>元素仅覆盖<img>标记的src属性(在指定的情况下)。

Here is an example of the picture element, used properly. 这是一个正确使用的图片元素的例子。 This example comes from the Mozilla Developer's Network . 此示例来自Mozilla Developer's Network

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

The src attribute should always be included, even if you use the srcset attribute in addition. 即使您另外使用srcset属性,也应始终包含src属性。

The srcset attribute is (from what I understand) an "older" technique of defining sources for different resolutions. srcset属性(根据我的理解)是一种定义不同分辨率源的“旧”技术。 It does not use standard-syntax media queries or have the other flexibilities afforded by using the <picture> and <source> elements ( although Chris Coyier of CSS-tricks disagrees with me here ), but may be supported by some browsers which don't support the newer standard. 它不使用标准语法媒体查询或使用<picture><source>元素提供的其他灵活性( 虽然CSS-tricks的Chris Coyier在这里不同意我的观点 ),但可能会受到某些浏览器的支持。支持更新的标准。 Including the srcset attribute on your <img> tag might be desirable, but, in these cases, you still need to include the src attribute as well (which provides a default when none of the srcset files are appropriate, and provides a fall-back for browsers that don't support srcset ). <img>标记中包含srcset属性可能是可取的,但是,在这些情况下,您仍然需要包含src属性(当没有srcset文件适合时提供默认值,并提供后备对于不支持srcset浏览器)。 All image tags always need src and alt attribute -- these are required attributes for the <img> tag. 所有图像标记始终需要srcalt属性 - 这些是<img>标记的必需属性。

A valid example of the <picture> tag, including the srcset attribute as a fall-back, and the src attribute as a worst-case-scenario fall-back, is below. <picture>标记的有效示例,包括作为srcset属性,以及作为最坏情况下降的src属性,如下所示。

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

This Smashing Magazine article gives a much more in-depth analysis of the different responsive images techniques and when to use each one. 这篇Smashing Magazine文章对不同的响应式图像技术以及何时使用每种技术进行了更深入的分析。


Aside: Please don't trust W3Schools as an official source. 旁白:请不要相信W3Schools是官方消息来源。 W3Schools chose a name that is similar to W3C (the World Wide Web Consortium), but they are not, in fact, related to the official standards body. W3Schools选择了一个类似于W3C(万维网联盟)的名称,但实际上它们并不与官方标准组织相关。 From their about page: "The site derives its name from the World Wide Web (W3), but is not affiliated with the W3C." 从他们的关于页面:“该网站的名称来自万维网(W3),但不隶属于W3C。”

Yes <picture> tag is part of html5 and according to documentation, its fallback is <img src=... > which will work even in very old browsers. <picture>标签是html5的一部分,根据文档,它的后备是<img src=... > ,它甚至可以在非常旧的浏览器中使用。

<picture>
   <source media="(min-width: 64em)" src="high-res.jpg">
   <source media="(min-width: 37.5em)" src="med-res.jpg">
   <source src="low-res.jpg">
   <img src="fallback.jpg" alt="This picture loads on non-supporting browsers.">
   <p>Accessible text.</p>
</picture>

Its also worth noting that other solutions, such as the srcset attribute, are also being discussed and are starting to see support. 值得注意的是,其他解决方案,例如srcset属性,也正在讨论中,并开始看到支持。 The srcset attribute was implemented in Webkit a while ago. srcset属性不久前在Webkit中实现。 However srcset attribute is not used for fallback but allows you to specify higher-quality images for your users who have high-resolution displays, without penalizing the users who don't. 但是, srcset属性不用于后备,但允许您为具有高分辨率显示的用户指定更高质量的图像,而不会惩罚不这样做的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM