Lesson 20 of 60 – Bootstrap Images
33%

Bootstrap Images

Bootstrap provides useful classes for displaying images in a responsive and attractive way. You can make images responsive, add rounded corners, create circles, add thumbnails, align images, and control how images behave on different screen sizes.

Note: The .img-fluid class is one of the most commonly used Bootstrap image classes. It makes an image responsive by applying max-width: 100% and height: auto.

1. What Are Bootstrap Images?

Bootstrap images are normal HTML images enhanced with Bootstrap utility classes.

<img src="image.jpg"
alt="Example Image">

Bootstrap does not require a special image element. Instead, you add Bootstrap classes to the normal HTML <img> element.

2. Basic HTML Image

Before using Bootstrap image utilities, remember the basic HTML image syntax.

<img src="photo.jpg"
alt="Student Photo">

The src attribute specifies the image location and the alt attribute provides alternative text.

Tip: Always provide useful alt text for meaningful images.

3. The .img-fluid Class

The .img-fluid class makes an image responsive.

<img src="image.jpg"
class="img-fluid"
alt="Responsive Image">

A responsive image automatically scales down when the available width becomes smaller.

.img-fluid {
    max-width: 100%;
    height: auto;
}

4. Why Use Responsive Images?

A large fixed-size image can cause horizontal scrolling on smaller screens.

Responsive images help the image fit inside its available container.

<div class="container">

    <img src="course.jpg"
    class="img-fluid"
    alt="Course">

</div>

This is especially useful for mobile-friendly websites.

5. Responsive Image Example

The following example demonstrates a responsive image.

<img src="student.jpg"
class="img-fluid"
alt="Student">
Responsive Example

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

6. Rounded Images

The .rounded class adds rounded corners to an image.

<img src="image.jpg"
class="rounded"
alt="Rounded Image">
Rounded Example

Rounded corners can make images look softer and more polished.

7. Rounded Circle Images

The .rounded-circle class makes a square image appear circular.

<img src="profile.jpg"
class="rounded-circle"
alt="Profile Photo">
Profile Example

This is commonly used for profile pictures and user avatars.

8. Image Thumbnail

The .img-thumbnail class gives an image a bordered thumbnail appearance.

<img src="image.jpg"
class="img-thumbnail"
alt="Thumbnail">
Thumbnail Example

Thumbnails are useful when you want an image to look like a small preview.

9. Responsive Rounded Image

You can combine multiple Bootstrap classes on the same image.

<img src="image.jpg"
class="img-fluid rounded"
alt="Responsive Rounded Image">

Here, img-fluid makes the image responsive and rounded adds rounded corners.

10. Responsive Thumbnail

You can combine .img-fluid and .img-thumbnail.

<img src="image.jpg"
class="img-fluid img-thumbnail"
alt="Responsive Thumbnail">

This provides a responsive image with thumbnail styling.

11. Image Alignment with text-start

Bootstrap text alignment utilities can be used to position images inside suitable containers.

<div class="text-start">

    <img src="image.jpg"
    class="img-fluid"
    alt="Image">

</div>
Left Image

12. Centering an Image

Use .d-block together with .mx-auto to center a block-level image.

<img src="image.jpg"
class="img-fluid d-block mx-auto"
alt="Centered Image">
Centered Image

The mx-auto utility applies automatic horizontal margins.

13. Floating an Image

Bootstrap provides .float-start and .float-end utilities for floating elements.

<img src="image.jpg"
class="img-fluid float-start me-3"
alt="Image">

Floating can be useful for wrapping text around an image, although modern layouts often use Flexbox or Grid for more predictable layouts.

14. Float End Image

The .float-end utility moves an image toward the end side of its container.

<img src="image.jpg"
class="img-fluid float-end ms-3"
alt="Image">

Use this technique carefully and test the result on smaller screens.

15. Image Inside a Card

Images are frequently used at the top of Bootstrap cards.

<div class="card">

    <img src="course.jpg"
    class="card-img-top"
    alt="Course Image">

    <div class="card-body">

        <h5 class="card-title">
        Web Development
        </h5>

        <p class="card-text">
        Learn web development.
        </p>

    </div>

</div>
Course Image
Web Development

Learn web development.

16. Card Image at the Bottom

You can place an image below the card content using .card-img-bottom.

<div class="card">

    <div class="card-body">

        <h5 class="card-title">
        Course
        </h5>

        <p class="card-text">
        Course description.
        </p>

    </div>

    <img src="course.jpg"
    class="card-img-bottom"
    alt="Course">

</div>

17. Image and Grid System

Images can be placed inside Bootstrap grid columns.

<div class="row">

    <div class="col-md-6">

        <img src="image.jpg"
        class="img-fluid"
        alt="Image">

    </div>

    <div class="col-md-6">

        <p>
        Image description.
        </p>

    </div>

</div>

On smaller screens, the columns can stack according to the Bootstrap grid breakpoint used.

18. Image with Width Utilities

Bootstrap width utilities can be used to control the width of an image relative to its container.

<img src="image.jpg"
class="w-100"
alt="Full Width Image">
Full Width Image

The w-100 utility sets the element width to 100%.

19. Image with Height Utilities

Bootstrap also provides height utilities such as h-100. However, setting a fixed height does not automatically make an image look correct because the image's aspect ratio may be affected by the available space.

<div style="height:200px;">

    <img src="image.jpg"
    class="h-100"
    alt="Image">

</div>

When controlling image dimensions, consider the image's aspect ratio and the layout requirements.

20. Object Fit Utilities

When an image must fit into a fixed-size area, CSS object-fit can be useful.

<img src="image.jpg"
style="width:100%; height:200px; object-fit:cover;"
alt="Course Image">

object-fit: cover allows an image to fill the specified area while maintaining its aspect ratio, although some parts of the image may be cropped.

21. Image with Alt Text

The alt attribute provides alternative text for an image.

<img src="student.jpg"
class="img-fluid"
alt="Student attending a computer class">

If the image cannot be displayed, the alternative text can provide useful information about the image.

Accessibility Tip: Write concise and meaningful alternative text for informative images.

22. Decorative Images

If an image is purely decorative and provides no meaningful information, its alternative text can be empty.

<img src="decoration.png"
alt="">

An empty alt value helps assistive technologies treat the image as decorative.

23. Responsive Image Gallery

Bootstrap's grid can be used to create a responsive image gallery.

<div class="row g-3">

    <div class="col-6 col-md-4">
        <img src="image1.jpg"
        class="img-fluid rounded"
        alt="Image 1">
    </div>

    <div class="col-6 col-md-4">
        <img src="image2.jpg"
        class="img-fluid rounded"
        alt="Image 2">
    </div>

    <div class="col-6 col-md-4">
        <img src="image3.jpg"
        class="img-fluid rounded"
        alt="Image 3">
    </div>

</div>
Gallery Image 1
Gallery Image 2
Gallery Image 3

24. Profile Image

A circular responsive image is commonly used for user profile photos.

<img src="profile.jpg"
class="img-fluid rounded-circle"
alt="User Profile"
style="width:150px;height:150px;object-fit:cover;">
User Profile

25. Image in a Responsive Card

Combining responsive images with cards is useful for course and product layouts.

<div class="card">

    <img src="course.jpg"
    class="card-img-top img-fluid"
    alt="Course">

    <div class="card-body">

        <h5 class="card-title">
        Python Course
        </h5>

        <p class="card-text">
        Learn Python programming.
        </p>

        <a href="#"
        class="btn btn-primary">
        Learn More
        </a>

    </div>

</div>
Python Course
Python Course

Learn Python programming.

Learn More

26. Common Image Mistakes

Some common mistakes when working with images include:

  • Using very large images without optimization.
  • Forgetting meaningful alt text.
  • Using fixed dimensions that cause distortion.
  • Ignoring mobile screen sizes.
  • Using too many decorative images.
  • Stretching images without considering their aspect ratio.
Tip: Always test important images on both desktop and mobile layouts.

27. Image Accessibility

Images should be accessible and meaningful to users of assistive technologies.

  • Use descriptive alt text for informative images.
  • Use alt="" for purely decorative images when appropriate.
  • Do not communicate essential information through an image alone.
  • Maintain sufficient contrast around images when needed.
  • Do not make text inside images the only source of important information.

28. Bootstrap Image Classes Reference

Important Bootstrap image-related classes include:

.img-fluid
.img-thumbnail
.rounded
.rounded-circle
.rounded-pill
.float-start
.float-end
.d-block
.mx-auto
.w-100
.card-img-top
.card-img-bottom

These classes can be combined with Bootstrap's grid and utility classes to create responsive image layouts.

29. Practical Course Gallery

Here is a practical course gallery using Bootstrap's responsive grid and image utilities.

<div class="row g-4">

    <div class="col-6 col-md-3">

        <img src="html.jpg"
        class="img-fluid rounded"
        alt="HTML Course">

        <p class="text-center mt-2">
        HTML
        </p>

    </div>

    <div class="col-6 col-md-3">

        <img src="css.jpg"
        class="img-fluid rounded"
        alt="CSS Course">

        <p class="text-center mt-2">
        CSS
        </p>

    </div>

</div>
HTML Course

HTML

CSS Course

CSS

JavaScript Course

JavaScript

Python Course

Python

30. Complete Bootstrap Image Example

The following example combines a responsive image, rounded corners, badge, card content, and button.

<div class="card" style="max-width:400px;">

    <img src="course.jpg"
    class="card-img-top img-fluid"
    alt="Web Development Course">

    <div class="card-body">

        <span class="badge text-bg-success">
        Popular
        </span>

        <h5 class="card-title mt-2">
        Web Development
        </h5>

        <p class="card-text">
        Learn HTML, CSS, JavaScript and Bootstrap.
        </p>

        <a href="#"
        class="btn btn-primary">
        View Course
        </a>

    </div>

</div>
Web Development Course
Popular
Web Development

Learn HTML, CSS, JavaScript and Bootstrap.

View Course

📌 Key Points

  • Use .img-fluid to make images responsive.
  • Use .rounded for rounded image corners.
  • Use .rounded-circle for circular images.
  • Use .img-thumbnail for thumbnail styling.
  • Use .d-block and .mx-auto to center an image.
  • Use .float-start and .float-end when floating is appropriate.
  • Use .card-img-top and .card-img-bottom with cards.
  • Use Bootstrap's grid system to create responsive image galleries.
  • Use meaningful alt text for informative images.
  • Use alt="" for purely decorative images when appropriate.
  • Consider aspect ratio when controlling image dimensions.
  • Test images on different screen sizes.

🧠 Quick Quiz

Question: Which Bootstrap class makes an image responsive?