Getting to Know How You Can Create Responsive Images Using CSS

As we all know, images are one of the most vital pieces of information included in a website. When it comes to making the images responsive, they aren’t adaptable at all. That’s because, images used in responsive designs are fixed.

Fortunately, there are techniques using which you can create responsive images. But, these techniques may vary in complexity and browser support. For instance, the <picture> element combined with the “srcset” and “sizes” attributes are considered an effective responsive image technique. However, using the srcset attribute presents a complex way to make images responsive. Although, this technique can be used by experienced designers to meet their objectives (i.e. make their images responsive), but novice designers might find the technique a lot challenging to implement.

In this post, we’ll discuss about how you can create responsive images easily with the help of CSS width and height properties.

Note: Before getting started, make sure that you have a responsive or fluid design layout.

Create Responsive Images Using CSS

Keep in mind that, even though, we’ll be using several different responsive image technique variations the concept on which those techniques rely will remain the same: we will use CSS to change the width of the image to “percentage-length” or a “rel unit”, and the height property will be set to auto as shown below:

img {
width: 100%;
height: auto;
}

Getting Started With Creating a Simple Responsive Image

Let us begin with, creating a simple responsive image. To do so, let’s take an example where ‘div’ is used as the container of an image element (i.e. <img>).

HTML

<div class="container">
<img src="myfirst_image.jpg" width="940" height="620"> </div>

Assume that the container has a width of 94% so that it can have some left as well as right margins. Apart from this, the container’s max-width is set to 940px that ensures that the layout doesn’t dominate the large screens (i.e. become too wide for devices with large screens).

The width of the image element within the container is set to 100%, so that it becomes equal to its container width. This will ensure that the image will scale in accordance with the container’s size regardless of the viewport it is being accessed on. This will eventually make the image responsive. Furthermore, the image height is set to auto to make the image scale proportionally.

Here’s how the CSS will look like after implementing the above discussed changes.

div.container {
width: 94%;
max-width: 940px;
margin: 0 auto; /* to center the container */
}
img {
width: 100%;
height: auto;
}

Note: Once the aforementioned technique is implemented, the image element will remain responsive even if it contains a fixed width and height. For example, we’ve defined fixed attributes for the <img> element in the HTML markup (i.e. width=”940″ height=”620″). This is a good approach for sites that contains content with fixed dimensions set using  HTML.

Implementing Responsive Images In Column-Layout

You might have stumbled upon responsive web designs where images are displayed in a column. For example, image gallery in a website is usually presented in the form of a grid having lots of images and thumbnails. If you too want to make the images responsive in columns, simply reduce the CSS width property value, and then assign the display property of the <img> elements a value of inline-block.

Advertisements

Let us now talk about the different column layout options, you can use for implementing responsive images in columns:

1. Two-column Responsive Image Layout

If you would like to present responsive images in two-column layout, then you just need to lower the CSS width property to one-half of the width of the container (i.e. 47%). You can see that the value of the CSS width property isn’t assigned to 50% so that the images can have proper space (i.e. margins on the left and right sides).

HTML

&lt;div class="container"&gt;
&lt;img src="myfirst_image.jpg" width="940" height="620" /&gt;
&lt;img src="mysecond_image.jpg" width="940" height="620" /&gt;
&lt;/div&gt;

CSS

img {

width: 47%;

display: inline-block;

}

2. Three-column Responsive Image Layout

The three-column layout scheme uses the similar approach as the two-column layout scheme, except that here the value of the CSS width will be one-third of the width of the container (i.e. 31%).

HTML

&lt;div class="container"&gt;
&lt;img src="myfirst_image.jpg" width="940" height="620" /&gt;
&lt;img src="mysecond_image.jpg" width="940" height="620" /&gt;
&lt;img src="mythird_image.jpg" width="940" height="620" /&gt;
&lt;/div&gt;

CSS

.three-columns {
width: 31%;
display: inline-block;
}

Using Conditional Breakpoints to Create Responsive Images

When creating responsive images to be included in the columns, you’ll occasionally need to resize the images according to small-screen or large-screen sizes. But, resizing the images for different device screen separately doesn’t make sense. A better approach requires to determine conditional breakpoints for the responsive images in columns. This can be achieved with the help of media queries.

Below is an example that will help you better understand to use media queries for presenting responsive images in one-column (for Smartphones), two-columns (for tablets), and three-column (for desktops):

HTML

&lt;div class="container"&gt;
&lt;img src="myfirst_image.jpg" width="940" height="620" /&gt;
&lt;img src="mysecond_image.jpg" width="940" height="620" /&gt;
&lt;img src="mythird_image.jpg" width="940" height="620" /&gt;
&lt;img src="myfourth_image.jpg" width="940" height="620" /&gt;
&lt;/div&gt;

CSS

/* Code for responsive images for small devices (i.e. Smartphones) */

img {

max-width: 100%;

display: inline-block;

}

/* Code for responsive images for medium-size devices (i.e. tablets) */

@media (min-width: 420px) {

img {

max-width: 47%;

}

}

/*  Code for responsive images for large devices (i.e. desktops) */

@media (min-width: 760px) {

img {

max-width: 24%;

}

}

Conclusion

Often after creating a well-designed responsive or fluid layout, you may fail to create responsive images that automatically resize according to the viewport size. Although, there are several techniques that can be used to make the images responsive, but they’re hardly comprehensible. So, if you’re a beginner and want to use simple techniques to create responsive images, then this post will serve as a handy guide for you.

Author Bio:

Samuel Dawson is a complete professional in web development & design industry Designs2HTML Ltd which is one of the most reliable psd to responsive html service company.