简体   繁体   English

如何在响应HTML5图像中使用srcset和size作为容器百分比的图像?

[英]How do I use srcset and sizes in responsive HTML5 images for an image that's a percentage of it's container?

I'm confused by a few things in using the srcset and sizes attributes on an img . 我被一些事情在使用混淆srcsetsizes上的属性img (I'm using picturefill but it follows the original spec) (我使用的是picturefill,但它遵循原始规范)

I have an image and on large screens, it's 25em in width but on small phone-screens, it's 85% of its container, not of the viewport. 我有一个图像,在大屏幕上,它的宽度为25em ,但在小手机屏幕上,它是其容器( 而不是视口)的85% I do not want to give it a set size on smaller screens - instead I want it to adapt to its container (which is a div ). 不想给它在小屏幕上设置大小-相反,我希望它适应它的容器(这是一个div )。 I cannot figure out how to do this. 我不知道该怎么做。

I also am not happy that I have to hardcode sizes and media queries into the HTML instead of putting them in CSS - it puts the same information in two places. 我也很高兴我必须将大小和媒体查询硬编码到HTML中,而不是将它们放在CSS中-它会将相同的信息放在两个地方。 Is there a way around this? 有没有解决的办法?

What I have: 我有的:

<!--
    Is there any way to make the sizes be percentages of the container?
-->
<img 
    sizes="(min-width: 1000px) 750px, (min-width: 200px) 50vw" 
    srcset="images/full_0.png 750w, images/small_0.png 187w" 
    alt="Sizing">

What I want: 我想要的是:

<!--
    The "use-750w" tells it which image to load but NOT the size
    to make it. Instead, it lets CSS size the image.
-->
<img 
    sizes="(min-width: 1000px) use-750w, (min-width: 200px) use-187w" 
    srcset="images/full_0.png 750w, images/small_0.png 187w" 
    alt="Sizing">

You can't because the HTML is separated from the CSS and the browser must know the image size before the CSS is loaded. 您不能这样做,因为HTML与CSS分离,并且浏览器必须在加载CSS之前知道图像大小。 Therefore the browser does not know the width of your div . 因此,浏览器不知道div的宽度。

You can give lazysizes a try. 您可以尝试lazysizes In this case all image width must be computable before the image is loaded via CSS. 在这种情况下,在通过CSS加载图像之前,所有图像宽度都必须是可计算的。

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

相关问题 在 TinyMCE 中使用响应式图像(尺寸 + srcset) - Use responsive images (sizes + srcset) with TinyMCE 如何使用React的Context功能将HTML5 Canvas上下文传递给this.props.children? - How do I use React's Context feature to pass an HTML5 Canvas context to this.props.children? 如何在Google Chrome扩展程序中使用HTML5的localStorage? - How do I use HTML5's localStorage in a Google Chrome extension? 使用已编译的 HTML 时,如何启用 vue.js 以使用 srcset 显示图像? - How do I enable vue.js to display images with srcset when using already compiled HTML? 使用jQuery以基于srcset的响应式图像替换UIKit的Slideshow源图像 - Using jQuery to replace UIKit's Slideshow source image with a responsive one based on srcset 当图像大小不同时,如何在 carasoul 中制作响应式图像? - How to make responsive image in carasoul while the images are in different sizes? 你如何处理html5的画布图像加载异步? - How do you deal with html5's canvas image load asynchrony? 按百分比在HTML5画布上缩放点的XY - Scale a dot's X Y on HTML5 canvas by percentage 如何处理HTML5画布中的许多图像? - How do I handle many images in my HTML5 canvas? ckeditor 4.4使用旧图像插件和自定义文件管理器上传响应图像,以添加srcset,sizes属性 - ckeditor 4.4 responsive image upload with old image plugin and custom filemanager to add srcset, sizes attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM