Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 58 ยท Content

Images and Figures

Learn how to make images responsive in Bootstrap 5.3 with img-fluid, style them with img-thumbnail and rounded, use picture, figure and align images.

An image that is wider than its container is one of the most common causes of a page that scrolls sideways on a phone. A photo that is 1,600 pixels wide happily overflows a 360-pixel screen unless you tell it not to. Bootstrap's image classes fix that and add a few finishing touches such as rounded corners, thumbnail borders and figure captions.

In this lesson you will make images responsive, style them, build a <picture> with a responsive fallback, mark up images with captions using <figure>, and align images left, right or center. For fixing aspect ratios and cropping, see Sizing Utilities.

All the images in this lesson are tiny inline SVGs written directly in the src, so the examples work in the sandbox without any files. In your own pages you will point src at a real image file.

Making an image responsive

Add img-fluid to the <img>. It applies two rules, max-width: 100% and height: auto. The image can then shrink with its container but never grows past its natural size, and the aspect ratio is preserved.

Without and with img-fluid

Narrow your window: the first banner needs scrolling, the second one keeps scaling. Notice that both images carry width and height attributes. Keep them. The browser uses them to reserve space before the file loads, so the page does not jump around (this protects the layout-shift part of Core Web Vitals), and img-fluid overrides the displayed size with CSS anyway.

flowchart LR
  A["Image wider than container"] --> B{"img-fluid?"}
  B -- "No" --> C["Overflows, page scrolls sideways"]
  B -- "Yes" --> D["max-width 100%<br/>height auto<br/>scales down, keeps ratio"]

img-fluid keeps an image inside its container without distorting it.

Always write the alt attribute. Describe what the image shows in the context of the page. If the image is purely decorative, use an empty alt="" so screen readers skip it.

Image shapes

Bootstrap includes a small set of shape and border helpers. Add them next to img-fluid:

Class Effect
rounded slightly rounded corners
rounded-circle a circle (needs a square image)
rounded-pill a pill shape
rounded-0 no rounding
img-thumbnail padding, a 1px border and rounded corners, like a photo print
rounded, circle and thumbnail

A common use is an avatar: a square image with rounded-circle. If the source image is not square, the circle becomes an ellipse. Make the box square with object-fit: cover (class object-fit-cover) as shown in Sizing Utilities.

img-thumbnail is about a bordered frame, so it fits galleries and previews. The border and padding colors follow the color mode, so the frame also looks right in dark mode.

Alignment

Because <img> is an inline element by default, you align it like text or like a floated box:

  • Float: float-start and float-end push the image left or right and let text wrap around it. Add clearfix to the parent if the float should not escape it.
  • Center with text align: put text-center on the parent. This works because the image is inline.
  • Center as a block: mx-auto d-block turns the image into a block and centers it with automatic side margins.
Floats and centering

The picture element

<picture> lets the browser choose between several files, for example a modern format with a fallback, or a cropped version for narrow screens. Bootstrap has no special picture class: the responsive class goes on the inner <img>, because that is the element that is actually rendered.

HTML
<picture>
  <source media="(min-width: 768px)" srcset="hero-wide.avif" type="image/avif">
  <source srcset="hero-narrow.webp" type="image/webp">
  <img src="hero-narrow.jpg" class="img-fluid rounded" alt="Mountains at dawn" width="800" height="450">
</picture>

The browser walks the <source> elements in order and uses the first that matches. The <img> is both the fallback and the element that receives your classes. Putting img-fluid on <picture> itself does nothing useful.

Figures and captions

A figure is a self-contained piece of content such as an image, chart or code listing, with an optional caption. Bootstrap styles it with three classes:

  • figure on <figure>.
  • figure-img on the image inside (adds a small bottom margin).
  • figure-caption on <figcaption> (smaller, muted text).
Figure with caption

Use <figure> and <figcaption> when the caption belongs to the image and the pair could move elsewhere on the page without losing meaning. A decorative picture in the middle of a layout does not need one.

Common mistakes

  • Forgetting img-fluid and then fixing overflow with overflow: hidden, which crops the picture.
  • Removing width and height attributes, which makes the layout jump while images load.
  • Using rounded-circle on a non-square image, producing an oval.
  • Putting img-fluid on <picture> instead of on its <img>.
  • Missing alt text. Decorative images take alt=""; informative ones need a description.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does img-fluid do?

    img-fluid applies max-width: 100% and height: auto. The image shrinks to fit but never grows beyond its natural size.
  2. 2. On which element do you place img-fluid when using a picture element?

    The img inside picture is the element that is rendered, so the class belongs there.
  3. 3. Which classes center an image as a block element?

    A block-level image with automatic left and right margins is centered. text-center works too, but on the parent element.
  4. 4. Which class pair styles a figure and its caption?

    Use figure on the figure element, figure-img on the image and figure-caption on the figcaption.

Summary

  • img-fluid makes an image responsive without distorting it; keep width, height and alt attributes anyway.
  • rounded, rounded-circle and img-thumbnail give images shapes and frames.
  • Align with float-start/float-end (plus clearfix), text-center on the parent, or d-block mx-auto.
  • With <picture>, put the Bootstrap classes on the inner <img>.
  • figure, figure-img and figure-caption style an image with its caption.

Next lesson: Tables.