简体   繁体   English

W3C验证SRCSET

[英]W3C Validation of SRCSET

W3C Validator is throwing an error on the following code: W3C验证程序在以下代码上引发错误:

<img alt="My Unique Alternative Text" class="news-image-left" height="480" src="/images/en-us/news/20140214-01.jpg" srcset="/images/en-us/news/20140214-01.jpg 1x, /images/en-us/news/20140214-01-mobile.jpg 640w" style="border-width: 0px;" title="A Unique Image" width="1420" itemprop="image">

The error is: 错误是:

Bad value /images/en-us/news/20140214-01.jpg 1x, /images/en-us/news/20140214-01-mobile.jpg 640w for attribute srcset on element img: No width specified for image /images/en-us/news/20140214-01.jpg. 元素img上的属性srcset的错误值/images/zh-CN/news/20140214-01.jpg 1x,/ images / zh-CN / news / 20140214-01-mobile.jpg 640w:未为图像/ images /指定宽度EN-US /新闻/ 20140214-01.jpg。 (Because one or more image candidate strings specify a width (eg, the string for image /images/en-us/news/20140214-01-mobile.jpg), all image candidate strings must specify a width.) (由于一个或多个图像候选字符串指定了宽度(例如,图像字符串/images/en-us/news/20140214-01-mobile.jpg),因此所有图像候选字符串都必须指定宽度。)

So, how do I specify both an image for pixel density (1x) -- down the road I may want to do one for high-density displays (2x) and for displays less than 640 pixels wide? 因此,如何同时为像素密度(1x)指定一幅图像-后来我可能想为高密度显示器(2x)和宽度小于640像素的显示器做一幅图像? That is, in a way that will make the W3C validator happy. 也就是说,以一种使W3C验证程序满意的方式。


UPDATE: 更新:

Well, I tinkered around some more with it and now the validator is no longer complaining but it doesn't appear to be working right so I don't think I have won the battle yet. 好吧,我对此进行了一些修改,现在验证器不再抱怨,但是它似乎并没有运行正常,因此我认为我还没有赢得这场战斗。

I now have this: 我现在有这个:

<img alt="My Unique Alternative Text" class="news-image-left" height="480" src="/images/en-us/news/20140214-01.jpg" srcset="/images/en-us/news/20140214-01-mobile.jpg 320w" sizes="(min-width:640px) 1420px, 320px" style="border-width: 0px;" title="A Unique Image" width="1420" itemprop="image"> 

BUT, now on Chrome I am getting just the "-mobile" image, regardless of how wide my screen is. 但是,现在在Chrome上,无论屏幕有多宽,我都只会得到“移动”图像。 I want it to display the full 1420px wide image on any display that is wider than 640px. 我希望它在比640px宽的任何显示器上显示完整的1420px宽的图像。

I am guessing I have just one thing out of place... any help would be appreciated. 我猜我只有一件事了……任何帮助将不胜感激。

This answers only for your update: The reason for this is, that the browser chooses one candidate from your srcset , the src attribute is used as a fallback. 这仅针对您的更新进行回答:原因是浏览器从srcset选择一个候选srcsetsrc属性用作后备。 This means to get the larger candidate you need to also add it to your srcset : 这意味着要获得更大的候选者,您还需要将其添加到您的srcset

<img srcset="/images/en-us/news/20140214-01-mobile.jpg 320w, /images/en-us/news/20140214-01.jpg 1420w" sizes="(min-width:640px) 1420px, 320px" />

I don't really know your image layout, but I doubt, that sizes is correct here. 我不太了解您的图片布局,但是我怀疑这里的sizes是否正确。 If this image is displayed fullwidth until 1420px your sizes attribute should look like this. 如果此图片显示为全角,直到1420px,您的sizes属性应如下所示。

sizes="(min-width: 1420px) 1420px, 100vw"

or 要么

sizes="(max-width: 1420px) 100vw, 1420px"

Additionally you should use more images. 此外,您应该使用更多图像。 Note if you use the width descriptor the browser automatically handles the density for you (for retina devices). 请注意,如果您使用宽度描述符,浏览器会自动为您处理密度(对于视网膜设备)。 Use at least 640w, 980w/1024w, 1420w, 2048w. 至少使用640w,980w / 1024w,1420w,2048w。

Please refer http://www.webkit.org/demos/srcset/ or https://ericportis.com/posts/2014/srcset-sizes/ . 请参阅http://www.webkit.org/demos/srcset/https://ericportis.com/posts/2014/srcset-sizes/ Based on the w3c validator, if you specify width descriptor for one of the image candidate string, then you should use width descriptor for all of them. 基于w3c验证程序,如果为图像候选字符串之一指定宽度描述符,则应为所有图像候选字符串使用宽度描述符。

Alternate option could be to use CSS media queries. 替代选项可以是使用CSS媒体查询。

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

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