Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

Responsive Images

Responsive images automatically adapt to different screen sizes while maintaining their proportions and fitting properly inside the available space.


What is a Responsive Image?

A responsive image is an image that adjusts its size according to the width of the screen or the size of its parent container.

This is important because a large image that looks good on a desktop may overflow or become difficult to display on a mobile screen.

Responsive website displayed on different screen sizes

Responsive images resize according to the available screen space.

Responsive Image Concept

Desktop
IMAGE
Large Width
→
Tablet
IMAGE
Medium Width
→
Mobile
IMAGE
Small Width

Simple Explanation

The same image can be displayed on desktop, tablet, and mobile devices.

CSS can make the image shrink or expand according to the available width without breaking the page layout.

Making an Image Responsive

One of the simplest ways to make an image responsive is to use max-width: 100% and height: auto.

.responsive-image {
    max-width: 100%;
    height: auto;
}

max-width: 100%

The image cannot become wider than its parent container. This prevents horizontal overflow.

height: auto

The browser automatically adjusts the image height while maintaining the original aspect ratio.

Example: Responsive Image

.responsive-image-example {
    width: 100%;
    max-width: 700px;
    height: auto;
    border-radius: 8px;
}

Output

Laptop showing web development

Resize the browser window to see how the image adapts to the available width.

Using width: 100%

You can also use width: 100% when you want an image to occupy the full width of its container.

.full-width-image {
    width: 100%;
    height: auto;
}

Difference

width: 100% makes the image fill the available container width, while max-width: 100% prevents the image from becoming larger than its container.

Responsive Images with object-fit

The object-fit property controls how an image fits inside a specific width and height.

.cover-image {
    width: 100%;
    height: 250px;
    object-fit: cover;
    border-radius: 8px;
}

Output

Modern workspace

object-fit: cover fills the defined area while keeping the image proportionally cropped.

Using the Picture Element

HTML also provides the <picture> element for serving different image files depending on screen conditions.

<picture>
    <source media="(max-width: 600px)"
            srcset="mobile-image.jpg">

    <source media="(max-width: 1000px)"
            srcset="tablet-image.jpg">

    <img src="desktop-image.jpg"
         alt="Responsive example">
</picture>

Why use picture?

The <picture> element can allow the browser to use different image files for different screen conditions. This can improve control over responsive images.

Responsive Background Images

Background images can also be adapted using CSS properties such as background-size: cover.

.hero-image {
    width: 100%;
    min-height: 250px;
    background-image: url("image.jpg");
    background-size: cover;
    background-position: center;
}

Common Properties for Responsive Images

Property Purpose
width: 100% Makes image fill the container width
max-width: 100% Prevents image from overflowing
height: auto Maintains image aspect ratio
object-fit: cover Fills a defined area while cropping if necessary
object-fit: contain Fits the complete image inside the available area

Responsive Image Best Practices

  • Use max-width: 100% for flexible images.
  • Use height: auto when maintaining the original aspect ratio.
  • Use appropriate image sizes instead of extremely large files.
  • Always provide meaningful alt text.
  • Use the <picture> element when different image sources are required.

Important Note

A responsive image should fit the available layout without causing horizontal scrolling or distortion.

Real-World Uses

  • Training institute websites
  • E-commerce product images
  • Portfolio websites
  • Blog and news websites
  • Landing pages
  • Responsive dashboards

Summary

Responsive images automatically adapt to the available screen or container size. CSS properties such as max-width, width, height, and object-fit help images display correctly across mobile, tablet, and desktop devices.