简体   繁体   English

如何使此img srcset代码正常工作?

[英]How can I get this img srcset code working?

I'm trying to use the srcset attribute of the img tag in HTML5. 我正在尝试在HTML5中使用img标签的srcset属性。 My goal is to switch to a smaller (file size and dimensions) image depending on the viewport size of the device. 我的目标是根据设备的视口大小切换到较小的(文件大小和尺寸)图像。 My three breakpoints are Desktop, Tablet, and Mobile. 我的三个断点是台式机,平板电脑和移动设备。

I've followed several online tutorials on how to use the srcset attribute of the img tag, but I cannot seem to get it to work as people are saying it should. 我已经阅读了几本有关如何使用img标签的srcset属性的在线教程,但是我似乎无法像人们所说的那样使它起作用。 No matter if I copy and paste the various tutorials' exact code into my site and put my image URLs and sizing into it, Chrome and other browsers are completely ignoring it. 无论我是否将各种教程的确切代码复制并粘贴到我的网站中,然后将图像的URL和大小调整到其中,Chrome和其他浏览器都完全忽略了它。 Is there a CSS or JS component to this I'm unaware of and that people are just assuming I would know to use? 我是否不知道有CSS或JS组件,人们只是假设我会使用?

<img 
     srcset="https://dev-fenfast.pantheonsite.io/wp-content/uploads/2019/06/FENFAST_sidebar_1_desktop_447w.jpg 447w, https://dev-fenfast.pantheonsite.io/wp-content/uploads/2019/06/FENFAST_sidebar_1_tablet_537w.jpg 537w, https://dev-fenfast.pantheonsite.io/wp-content/uploads/2019/06/FENFAST_sidebar_1_mobile_217w.jpg 217w" 
     sizes="(max-width: 1440px) 447w, (max-width: 768px) 537w, (max-width: 354px) 217w" 
     src="https://dev-fenfast.pantheonsite.io/wp-content/uploads/2019/06/FENFAST_sidebar_1_desktop_447w.jpg">

Here's a CodePen of the most basic thing I'm trying --> https://codepen.io/anon/pen/XLzoaZ 这是我正在尝试的最基本的东西的CodePen-> https://codepen.io/anon/pen/XLzoaZ

Here's an example URL where I'm trying it out for real. 这是一个示例网址,我在其中进行了实际尝试。 The graphic is on the right sidebar just above the "Search" field. 该图形位于右侧栏的“搜索”字段上方。 The other images are not yet implemented with srcset. 其他映像尚未使用srcset实现。 --> https://dev-fenfast.pantheonsite.io/health/ -> https://dev-fenfast.pantheonsite.io/health/

I can't find any error messages in the Inspector Console regarding this seemingly simple tag. 我在Inspector控制台中找不到有关此看似简单标记的任何错误消息。

I have better option for you if you like. 如果您愿意,我有更好的选择。 Instead of scrset you can use pure css or bootstrap. 可以使用纯CSS或引导程序代替scrset。 These links might help you https://getbootstrap.com/docs/4.0/layout/media-object/ https://getbootstrap.com/docs/4.0/layout/overview/ Also learn more about how to use @media in css or more it will be easier if you use it with bootstrap. 这些链接可能会帮助您https://getbootstrap.com/docs/4.0/layout/media-object/ https://getbootstrap.com/docs/4.0/layout/overview/还了解有关如何在CSS中使用@media的更多信息或更多,如果与引导程序一起使用,会更容易。 One of those links explaining to you how to use @media with bootstrap. 这些链接之一向您解释如何在引导程序中使用@media。 The media work like this. 媒体是这样工作的。 If the screen smaller than min-width: 768px it's mean it's a tablet and so on. 如果屏幕小于最小宽度:768像素,则表示它是平板电脑,依此类推。 Here you can see the media in css with live examples. 在这里,您可以在CSS中看到带有实时示例的媒体。 https://www.w3schools.com/css/css_rwd_mediaqueries.asp https://www.w3schools.com/css/css_rwd_mediaqueries.asp

For responsive images use width: 100% and high: auto or in bootstrap 3 use the class img-responsive or in bootstrap 4 use img-fluid 对于自适应图像,请使用width:100%和high:自动或在bootstrap 3中使用img-response类,或在bootstrap 4中使用img-fluid

Update: Use this in bootstrap 4 更新:在bootstrap 4中使用它

// Extra small devices (portrait phones, less than 576px)
// No media query since this is the default in Bootstrap

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) { ... }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) { ... }

// Large devices (desktops, 992px and up)
@media (min-width: 992px) { ... }

// Extra large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

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

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