Hit enter after type your search item

CSS image size settings

In today’s world, more people are using smart-phones than desktop computers. So, making images responsive that sets with the user screens is very important for a good user experience.

In this tutorial, we will how to set the image size by using CSS properties. I will also show you ways of making the images responsive – so as the screen gets smaller (from the computer desktop to smartphone) the image resizes itself by using CSS.

How to set image size using CSS?

First, let us look at the basics – setting the image dimensions in CSS. For that, I used the same image twice in the example below. The first image is the original size whereas the second image is given a CSS class. Have a look:

See online demo and code

In the second img tag, you can see a CSS class (imgsize). There, we provided the width and height in pixels. However, if you resize the window or look at the example in the smartphone, there should be scroll bars while images should be of the same sizes as in the bigger screen.

First, have a look at sizing the images in percentage and then I will show you how to make those images responsive.

The example of image size by percentage

The example below shows using the % for setting the image size. In the example, the first image is the original size, second is 50% of the container while the third is 150%:

See online demo and code

How to make an image responsive using CSS?

There are certain tricks that let you make the images responsive as using CSS. It depends on the requirement – for example allowing the image to be responsive for any height and width as it changes. Similarly, you want to make the image responsive for a certain width or height etc.

The first example below shows making an image responsive as the screen size varies – no limit is set for this. Have a look and try resizing your screen and see how image adjusts without scroll bars:

See online demo and code

So, create a CSS class and just specify the width 100% and height value is auto. Apply this class to the img tag for image that you want to make responsive.

Setting the maximum height and width for responsive image example

You may want to specify the maximum height and width of the image in order to keep the quality of the image for the responsive image. The above example keeps on increasing the image dimensions with screen height and width.

For setting the max height/width, you may use the max-height and max-width properties of CSS as shown in the example below:

See online demo and code

Similarly, you may set the min-height and min-width properties.

Make images responsive by srcset and sizes attribute

If you are not satisfied with automatic responsive images as shown in the above examples – that used one image and adjusted the size according to the device then you may use another technique that gives you more control.

By this technique, you may specify different images for various devices/dimensions of the screen. So, you may resize the images yourself by using tools like Photoshop. You will be able to direct which image is used for a particular device.

The tags for this technique are srcset and size attribute that is used in the HTML img tag. See an example below:

See online demo and code

For clarity purpose, I used two different images (the one in the above demos and a banana.jpg). In live sites, you may want to use the same images by resizing yourself.

Also, the above demo works for two screen sizes i.e. 480 and 800px. You may specify more width as per the need.

For learning more about srcset, go to its tutorial here.

Responsive images by using Bootstrap

If your project is based on the Bootstrap framework then making images responsive is pretty simple. All you have to do is using the responsive class:

  • .img-fluid

An example of a responsive image by using Bootstrap 4 is given below:

See online demo and code

Resize the screen and you can see image adjusting itself.
This div height required for enabling the sticky sidebar