Responsive Images with CSS

What You'll Learn

  • Control how images fill containers with object-fit (cover, contain, fill)
  • Position images within containers using object-position
  • Maintain aspect ratios with the modern aspect-ratio property
  • Create responsive image foundations with max-width: 100%
  • Apply object-fit patterns for hero images, cards, avatars, and galleries
  • Understand when to use HTML solutions (srcset, picture) vs CSS solutions
  • Build production-ready responsive image patterns

Introduction to Responsive Images

Responsive images adapt to different screen sizes, device capabilities, and layout contexts. While HTML provides tools like srcset and picture for serving different image files, CSS provides powerful properties for controlling how images display within their containers. The modern CSS approach using object-fit, object-position, and aspect-ratio creates flexible, maintainable image layouts without JavaScript.

These CSS properties solve common problems: preventing image distortion, maintaining consistent dimensions across varying content, cropping images intelligently, and ensuring images look good at any size. Combined with HTML's responsive image features, CSS creates a complete solution for modern image handling.

object-fit - How Images Fill Containers

The object-fit property controls how replaced content (images, videos) resizes to fit its container. This solves the common problem of images distorting when forced into specific dimensions.

/* Cover: fills entire container, may crop */ img { width: 100%; height: 300px; object-fit: cover; } /* Contain: fits inside container, maintains aspect ratio */ img { object-fit: contain; } /* Fill: stretches to fill container (distorts) */ img { object-fit: fill; /* Default behavior */ } /* Scale-down: smaller of 'none' or 'contain' */ img { object-fit: scale-down; } /* None: ignores container size, may crop */ img { object-fit: none; }

object-position - Positioning Images

When using object-fit: cover or object-fit: none, the image may be cropped. The object-position property controls which part of the image remains visible.

/* Named positions */ img { object-fit: cover; object-position: top; /* Values: top, bottom, left, right, center */ } /* Percentage values (x y) */ img { object-position: 75% 25%; /* 75% from left, 25% from top */ } /* Pixel values */ img { object-position: 20px 30px; } /* Combinations */ img { object-position: center bottom; object-position: right 10px top 20px; } /* Focus on faces in portraits */ .portrait { object-fit: cover; object-position: center 30%; }

aspect-ratio - Modern Layout Control

The aspect-ratio property maintains an element's width-to-height ratio, preventing layout shift and eliminating the need for padding-bottom hacks.

/* Video aspect ratio */ .video-container { width: 100%; aspect-ratio: 16 / 9; } /* Square images */ .thumbnail { width: 200px; aspect-ratio: 1; /* Same as 1 / 1 */ } /* Photo aspect ratio */ .photo { aspect-ratio: 4 / 3; } /* Golden ratio */ .hero { aspect-ratio: 1.618; } /* Combined with object-fit */ img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }

Responsive Image Foundation

Start with this foundation to make all images responsive by default. These rules prevent images from exceeding their container width while maintaining their aspect ratio.

/* Foundation: all images responsive */ img { max-width: 100%; height: auto; display: block; } /* With container */ .img-container { width: 100%; max-width: 600px; } .img-container img { width: 100%; height: auto; border-radius: 8px; }

Hero Image Pattern

Hero images need to fill a specific height while cropping intelligently. This pattern is perfect for large header images that need to work across all viewport sizes.

.hero-img { width: 100%; height: 400px; object-fit: cover; object-position: center; } /* Responsive height */ @media (max-width: 768px) { .hero-img { height: 250px; } } /* Focus on specific area */ .hero-portrait { object-position: center 30%; }

Card Image Pattern

Card images need consistent heights regardless of the source image's aspect ratio. This pattern ensures a uniform grid of cards with perfectly sized images.

.card { max-width: 400px; background: white; border-radius: 8px; overflow: hidden; } .card-img { width: 100%; height: 200px; object-fit: cover; object-position: center; } /* Or with aspect-ratio */ .card-img-alt { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }

Avatar/Profile Image Pattern

Circular avatar images need to be square first, then given a 50% border-radius. The object-fit: cover ensures faces stay centered.

.avatar { width: 150px; height: 150px; object-fit: cover; border-radius: 50%; border: 4px solid white; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2); } /* Responsive sizing */ @media (max-width: 640px) { .avatar { width: 100px; height: 100px; } } /* With aspect-ratio */ .avatar-modern { width: 150px; aspect-ratio: 1; object-fit: cover; border-radius: 50%; }

Gallery Grid Pattern

Image galleries need uniform sizing regardless of source image dimensions. Combine CSS Grid with aspect-ratio and object-fit for perfect galleries.

.gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; } .gallery-item { aspect-ratio: 1; overflow: hidden; border-radius: 8px; } .gallery-item img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.3s; } .gallery-item img:hover { transform: scale(1.05); }

HTML Picture Element

While this tutorial focuses on CSS, the HTML picture element works hand-in-hand with CSS for complete responsive image solutions. Use it for art direction and format selection.

<!-- Art Direction: different images for different sizes --> <picture> <source media="(min-width: 1024px)" srcset="hero-desktop.jpg"> <source media="(min-width: 768px)" srcset="hero-tablet.jpg"> <img src="hero-mobile.jpg" alt="Hero image" style="width: 100%; object-fit: cover;"> </picture> <!-- Format Selection: modern formats with fallback --> <picture> <source srcset="image.avif" type="image/avif"> <source srcset="image.webp" type="image/webp"> <img src="image.jpg" alt="Description"> </picture> <!-- Resolution Switching: different sizes --> <img srcset="small.jpg 480w, medium.jpg 800w, large.jpg 1200w" sizes="(max-width: 600px) 480px, (max-width: 900px) 800px, 1200px" src="medium.jpg" alt="Description">

Complete Responsive Image Patterns

Here are production-ready patterns combining all techniques for common responsive image needs.

Foundation (All Images)

/* Apply to all images */ img { max-width: 100%; height: auto; display: block; }

Hero Section

.hero-img { width: 100%; height: 60vh; min-height: 400px; max-height: 600px; object-fit: cover; object-position: center; } @media (max-width: 768px) { .hero-img { height: 40vh; min-height: 250px; } }

Card Images

.card-img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }

Avatar/Profile

.avatar { width: 150px; aspect-ratio: 1; object-fit: cover; border-radius: 50%; }

Gallery

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 1rem; } .gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; }

Logo

.logo { max-width: 200px; height: auto; object-fit: contain; }

Key Takeaways