Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Responsive Images in Bootstrap 5

Responsive Images: Learn how to make images scale properly across different screen sizes and devices.

Why Responsive Images Matter

Responsive images adapt to different screen sizes, ensuring they look great on mobile, tablet, and desktop devices. They prevent horizontal scrolling and improve user experience.

❌ Without .img-fluid
Non-responsive example

Overflows on smaller screens

✅ With .img-fluid
Responsive example

Scales to fit container

The .img-fluid Class

How .img-fluid Works

The .img-fluid class applies these CSS properties:

.img-fluid {
            max-width: 100%;
            height: auto;
            }
What it does:
  • max-width: 100% - Never exceeds container width
  • height: auto - Maintains aspect ratio
  • Works with any container size
  • Compatible with all breakpoints
When to use:
  • All images in responsive layouts
  • Hero images and banners
  • Content images in articles
  • Product images in e-commerce
  • Gallery and portfolio images

Basic Implementation

Responsive demo
Code Example
<Image width={600} height={400} 
            src="image.jpg" 
            alt="Description"
            class="img-fluid"
            width="800"
            height="400"
            />
Tip: Include width and height attributes to prevent layout shifts.

Responsive Images in Different Containers

Container Comparison
Full Width Container
Full width

Takes 100% of container width

Medium Container (col-md-8)
Medium width

Scales to fit 8 columns on medium+ screens

Small Container (col-md-4)
Small width

Scales to fit 4 columns on medium+ screens

<!-- Full width -->
            <div class="col-12">
            <Image width={600} height={400} src="image.jpg" class="img-fluid" alt="Full width">
            </div>

            <!-- Medium width -->
            <div class="col-md-8">
            <Image width={600} height={400} src="image.jpg" class="img-fluid" alt="Medium width">
            </div>

            <!-- Small width -->
            <div class="col-md-4">
            <Image width={600} height={400} src="image.jpg" class="img-fluid" alt="Small width">
            </div>

Advanced Responsive Techniques

Srcset and Sizes Attributes

For optimal performance, use srcset and sizes attributes to serve different image resolutions:

Responsive with srcset
<Image width={600} height={400} 
            src="image-800.jpg" 
            srcset="image-400.jpg 400w,
                    image-800.jpg 800w,
                    image-1200.jpg 1200w"
            sizes="(max-width: 576px) 100vw,
                    (max-width: 768px) 50vw,
                    33vw"
            alt="Responsive image"
            class="img-fluid"
            />
How srcset works:
  • Browser chooses appropriate image based on screen size
  • Reduces bandwidth usage on mobile devices
  • Provides high-quality images on retina displays
  • Improves page load performance

Responsive Image Gallery

Gallery image 1

Image 1: Responsive in grid

Gallery image 2

Image 2: Responsive in grid

Gallery image 3

Image 3: Responsive in grid

Gallery image 4

Image 4: Responsive in grid

Gallery image 5

Image 5: Responsive in grid

Gallery image 6

Image 6: Responsive in grid

Gallery image 7

Image 7: Responsive in grid

Gallery image 8

Image 8: Responsive in grid

<!-- Responsive image gallery -->
            <div class="row g-3">
            <div class="col-xl-3 col-lg-4 col-md-6 col-sm-6">
                <div class="card h-100">
                <Image width={600} height={400} 
                    src="image.jpg" 
                    class="card-img-top img-fluid"
                    alt="Gallery image"
                />
                <div class="card-body">
                    <p class="card-text">Description</p>
                </div>
                </div>
            </div>
            <!-- Repeat with different column classes -->
            </div>
Column Breakpoints:
  • .col-sm-6: 2 columns on mobile
  • .col-md-6: 2 columns on tablet
  • .col-lg-4: 3 columns on laptop
  • .col-xl-3: 4 columns on desktop
Gallery Best Practices:
  • Use consistent aspect ratios
  • Optimize images for web
  • Add descriptive alt text
  • Consider lazy loading
  • Provide image captions

Lazy Loading Images

Improve Performance with Lazy Loading
Above the fold

Loads immediately

Below the fold

Loads when scrolled into view

<!-- Above the fold - load immediately -->
            <Image width={600} height={400} 
            src="hero-image.jpg" 
            class="img-fluid"
            alt="Hero image"
            />

            <!-- Below the fold - lazy load -->
            <Image width={600} height={400} 
            src="content-image.jpg" 
            class="img-fluid"
            alt="Content image"
            loading="lazy"
            />

            <!-- With modern browser support -->
            <Image width={600} height={400} 
            src="image.jpg" 
            class="img-fluid"
            alt="Lazy loaded"
            loading="lazy"
            decoding="async"
            />
Lazy Loading Considerations:
  • Use for below-the-fold images: Improves initial page load
  • Avoid for hero images: Critical content should load immediately
  • Browser support: Modern browsers support native lazy loading
  • Fallback: Consider JavaScript polyfills for older browsers

Common Issues and Solutions

IssueCauseSolution
Image too small on large screensFixed width attribute or small source imageUse larger source image with .img-fluid
Image blurry on high-DPI screensLow-resolution source imageUse high-res images with srcset
Layout shift during loadingMissing width/height attributesAlways include width and height attributes
Poor performanceLarge file sizesOptimize images, use modern formats, lazy load
Inconsistent aspect ratiosImages with different proportionsUse CSS aspect-ratio or object-fit

Best Practices Checklist

Responsive Image Checklist
Performance Tips:
  • Image compression: Use tools like Squoosh, TinyPNG
  • Modern formats: WebP provides 30% smaller files than JPEG
  • CDN delivery: Use image CDNs for faster loading
  • Cache headers: Implement proper caching strategies
  • Responsive breakpoints: Create multiple image sizes