CHAPTER 80

CSS Responsive Images

Learn how to make images resize correctly across different screen sizes.

01. What is CSS Responsive Images?

Responsive images adapt to the width of their containers.
max-width: 100% prevents images from overflowing.
height: auto maintains the original aspect ratio.

02. Syntax

img { max-width: 100%; height: auto; display: block; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>

img {
    max-width: 100%;
    height: auto;
    display: block;
    border-radius: 15px;
}

</style>
</head>
<body>

<img
    src="https://via.placeholder.com/1200x600"
    alt="Responsive example"
>

</body>
</html>

04. Important Points

✓ max-width: 100% prevents overflow.
✓ height: auto preserves aspect ratio.
✓ display: block can remove inline image spacing.
✓ Use optimized image files for performance.

05. Common Mistakes

⚠ Using fixed widths larger than the viewport.
⚠ Distorting images by setting incompatible width and height.
⚠ Forgetting alt text.

06. Real World Use

Responsive images are essential in blogs, portfolios, product pages, banners and education websites.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Make an Image Responsive

Add an image to a page and make sure it never becomes wider than its container.