Responsive images implementation

Responsive design is an approach that enables your website to fluidly adjust within the parameters of any device – involving a minimum of scrolling or zooming.

Responsive Image creating great-looking sites which are dynamic and flexible enough to be visually responsive to any screen, from mobile widths right up to desktop format. And to make that possible a flexible approach to images.

Responsive Image is an image which is displayed in its best form on a web page, depending on the device in your website is being viewed from.

Responsive Images are now widely used and fairly easy to use in basic layouts

How to Enable Responsive Images

There are a number of ways to enable responsive behavior of images. We need a better way to deal with responsive images. The recommended ways are,

srcset attribute

sizes attribute

picture element

The srcset Attribute:

Srcset is a new attribute which allows you to specify different kind of images for different screen-sizes/orientation/display-types. The usage is really simple, you just provide a lot of different images separating them with a comma like this,

The actual implementation where you would want a different size image (different height, width) on different screen sizes is accomplished by using sizes attribute along with the w descriptor of srcset attribute. A couple of examples are,

You want the image to be displayed in half of the viewport width when the viewport width is greater than 40em, but the image should occupy the complete width when the viewport width is less than or equal to 40em. This is how you’ll approach it:
<img src=”images/space-needle.jpg” sizes=”(max-width: 40em) 100vw, 50vw”
srcset=”images/space-needle.jpg 200w, images/space-needle-2x.jpg 400w,
images/space-needle-hd.jpg 600w”>

The picture Element

The Picture element is used when you want to show a different image depending on the rendered size of the image. The picture element is a container which contains other elements that control the image to be downloaded.

The picture element itself does not display anything. Even the source element within the picture element does not represent anything of its own. The source element must contain the srcset attribute and it may have sizes, media and type attributes. It’s necessary to add img element within picture. You won’t see any image without the img element. The examples are,