兩種方法實現響應式圖片:css
<picture>
<img>
在該兩個標籤中使用srcset
和
gitsizes屬性,如:
<article> <img src="small.png" srcset="large.png 1280w, medium.png 640w, small.png 320w" sizes="(man-width:500px) 700x, 100vm" alt="Alternate text" /> </article>
配合的css以下github
article{ margin: 0 auto; max-width: 700px; } @media (max-width: 500px) { img {width: 250px }
}
<picture> <source media="(min-width: 36em)" srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w" sizes="33.3vw" /> <source srcset="cropped-large.jpg 2x, cropped-small.jpg 1x" /> <img src="small.jpg" alt="A rad wolf" /> </picture>
轉載摘抄翻譯自: ide