← Back to CSS Course | Chapter 12: Responsive Design | Lesson 6 of 8

CSS RWD Images

An image with no responsive rules behaves like a framed picture bolted to the wall at a fixed size -- RWD image techniques are what let that picture resize itself down to fit a smaller wall instead of hanging off the edges.
Syntax
css
img {
  max-width: 100%;
  height: auto;
}

The Core Fluid Image Rule

Setting max-width: 100% and height: auto on an image lets it shrink to fit any container narrower than its natural size while preserving its aspect ratio, without ever growing past its actual pixel dimensions and becoming blurry.

Note:
  • Accepted values:
  • max-width: 100% — never wider than the parent
  • height: auto — keeps the aspect ratio
  • display: block — removes the inline gap
  • width: 100% — always fills the parent

Example: The Core Fluid Image Rule

css
<style>
img {
  max-width: 100%;
  height: auto;
}
</style>
<img src="https://placehold.co/1200x800" alt="Shrinks to fit, keeps its aspect ratio">

Why height: auto Matters

Setting only max-width: 100% without height: auto can leave the height fixed at the image's natural pixel height even as the width shrinks, distorting the image -- height: auto tells the browser to recalculate height proportionally as width changes.

Note:
  • Accepted values:
  • height: auto — the default, height follows the ratio
  • height: <length> — a fixed height distorts the image
  • width and height attributes — the browser reserves space with aspect-ratio
  • max-width: 100% — pairs with height: auto

Example: Why height: auto Matters

css
<style>
.distorted {
  max-width: 100%;
}
.correct {
  max-width: 100%;
  height: auto;
}
</style>
<img class="distorted" src="https://placehold.co/800x400" alt="Height stays fixed, can distort">
<img class="correct" src="https://placehold.co/800x400" alt="Height recalculates proportionally">

Serving Different Image Sizes

A single large image scaled down with CSS still downloads its full file size even on a small phone screen, wasting bandwidth -- the HTML srcset and sizes attributes (and the <picture> element for art-direction cases) let the browser choose an appropriately-sized file per device.

Note:
  • Accepted values:
  • srcset="a.jpg 480w, b.jpg 1200w" — image candidates with widths
  • sizes="(max-width: 600px) 480px, 1200px" — display width hints
  • srcset="a.jpg 1x, b.jpg 2x" — density descriptors
  • <picture> and <source media="..."> — art direction

Example: Serving Different Image Sizes

css
<style>
img { max-width: 100%; height: auto; border: 3px solid #1e6fd9; background: #cfe8ff; }
<img srcset="small.jpg 480w, large.jpg 1200w"
     sizes="(max-width: 600px) 480px, 1200px"
     src="large.jpg" alt="Browser picks the right file size per device">
</style>
<img srcset="small.jpg 480w, large.jpg 1200w"
     sizes="(max-width: 600px) 480px, 1200px"
     src="large.jpg" alt="Browser picks the right file size per device">

Background Images vs Inline Images

background-image in CSS does not respond to max-width/height: auto the same way <img> does -- responsive background images instead rely on background-size: cover or contain, combined with media queries to swap the image file itself if needed.

Note:
  • Accepted values:
  • background-image: url(...) — the image
  • background-size: cover | contain | <length> — sizing
  • background-position: <position> — placement
  • background-repeat: no-repeat — no tiling
  • image-set() — resolution switching for backgrounds

Example: Background Images vs Inline Images

css
<style>
.hero {
  background-image: url('https://placehold.co/1200x400');
  background-size: cover;
  height: 200px;
}
</style>
<div class="hero">background-size handles responsiveness here, not max-width</div>

object-fit for Cropped Responsive Images

When an image must fill a fixed-aspect-ratio box exactly (like a square avatar or card thumbnail) without distortion, object-fit: cover crops the image to fill the box while object-fit: contain letterboxes it instead -- both work with any container size.

Note:
  • Accepted values:
  • object-fit: cover — fills the box, crops overflow
  • object-fit: contain — fits inside, may letterbox
  • object-fit: fill — default, stretches to the box
  • object-fit: none | scale-down — no resize / smaller of none and contain
  • object-position: <position> — chooses the crop focus
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: object-fit for Cropped Responsive Images

css
<style>
img {
  width: 150px;
  height: 150px;
  object-fit: cover;
}
</style>
<img src="https://placehold.co/400x200" alt="Cropped to fill the square without distortion">
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting height: auto, so the image is stretched when a height is set.
  2. Using only width: 100%, which can enlarge small images and make them blurry, when max-width: 100% avoids it.
  3. Serving one large image to phones, which wastes data.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.