Is it possible to make images responsive in AMP HTML or I need to declare the image size for each screen resolution in my media queries?

ε祈祈猫儿з 提交于 2019-12-04 04:38:26

问题


Most images on my website occupy the whole screen width, so in my current website version (not AMP) I set all the images in my CSS to fill the whole screen width by doing this:

body img { 
  width:100%
}

Is there anything equivalent to this in AMP?


回答1:


If you specify layout="responsive" as an attribute of the <amp-img> tag then the image will behave roughly like you've described. See an example at http://www.mirror.co.uk/news/uk-news/bonfire-night-2015-how-keep-6767214.amp

The <amp-img> tag requires a height & width. The height and width are used to make sure the space is held open for the image (because AMP images are lazy loaded) and the aspect ratio is respected when it is resized.




回答2:


Strictly speaking that is scaling the image and not delivering responsive images. If you want to ensure you send only the image size the device actually needs then you need to use srcset and sizes attributes.




回答3:


You can see an example where we use amp-img with srcset for responsive images here: https://ampbyexample.com/components/amp-img/




回答4:


A simple responsive image - width and height are used to determine the aspect ratio. Use alt to specify text that can act as an alternative for the image.

https://amp.dev/documentation/examples/components/amp-img/?referrer=ampbyexample.com



来源:https://stackoverflow.com/questions/33878352/is-it-possible-to-make-images-responsive-in-amp-html-or-i-need-to-declare-the-im

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!