TExcelTAD
Home › Bootstrap › BS5 Images

Bootstrap 5 Images

Images are easy to break on small screens: a 1200px photo will overflow a phone. Bootstrap gives you a few classes to make images responsive, add frames and corners, and align them. The examples below use inline SVG data URIs instead of photos, so they work everywhere.

Image class reference

Class Effect
.img-fluid max-width: 100%; height: auto; — never wider than its parent
.img-thumbnail Padding, a 1px border and rounded corners
.rounded Rounded corners (.rounded-0 … .rounded-5 for sizes)
.rounded-circle Circle (or ellipse if the image isn't square)
.rounded-pill Fully rounded ends
.float-start / .float-end Float left / right (text wraps around)
.mx-auto .d-block Centre a block-level image
.object-fit-cover Crop to fill a fixed box (5.3)

Shapes

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="d-flex gap-3 align-items-center flex-wrap">
    <figure class="text-center">
      <img class="rounded" width="110" height="110" alt="Rounded"
           src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' fill='%237952b3'/%3E%3Ctext x='50' y='60' font-size='30' text-anchor='middle' fill='white'%3EBS%3C/text%3E%3C/svg%3E">
      <figcaption class="small">.rounded</figcaption>
    </figure>
    <figure class="text-center">
      <img class="rounded-circle" width="110" height="110" alt="Circle"
           src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' fill='%23198754'/%3E%3Ctext x='50' y='62' font-size='36' text-anchor='middle'%3E😀%3C/text%3E%3C/svg%3E">
      <figcaption class="small">.rounded-circle</figcaption>
    </figure>
    <figure class="text-center">
      <img class="img-thumbnail" width="110" height="110" alt="Thumbnail"
           src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' fill='%23fd7e14'/%3E%3C/svg%3E">
      <figcaption class="small">.img-thumbnail</figcaption>
    </figure>
  </div>
</body>
</html>

Responsive images

.img-fluid lets an image shrink with its container while keeping its aspect ratio. The image below is "800px wide", but it never overflows the frame. Resize your browser window to watch it scale.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <img class="img-fluid rounded-3" alt="A wide banner" width="800" height="200"
       src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='800' height='200'%3E%3Cdefs%3E%3ClinearGradient id='g'%3E%3Cstop offset='0' stop-color='%237952b3'/%3E%3Cstop offset='1' stop-color='%230dcaf0'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='800' height='200' fill='url(%23g)'/%3E%3Ctext x='400' y='115' font-size='40' text-anchor='middle' fill='white' font-family='sans-serif'%3E800 x 200 banner%3C/text%3E%3C/svg%3E">
  <p class="mt-2 small">Remove <code>img-fluid</code> and run again: the image overflows.</p>
</body>
</html>

Aligning images

Float an image to wrap text around it, or centre it as a block:

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="clearfix">
    <img class="float-start me-3 rounded-circle" width="70" height="70" alt=""
         src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Crect width='10' height='10' fill='%23dc3545'/%3E%3C/svg%3E">
    <p>This text wraps around the floated image on the left. The <code>.clearfix</code> parent makes sure the next content starts below the float, even when the text is short.</p>
  </div>
  <img class="d-block mx-auto rounded" width="60" height="60" alt=""
       src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Crect width='10' height='10' fill='%230d6efd'/%3E%3C/svg%3E">
  <p class="text-center small">Centred with <code>d-block mx-auto</code></p>
</body>
</html>

Fixed-size boxes with object-fit

When images of different shapes must fill identical boxes (e.g. a gallery or avatars), give them a fixed size and use .object-fit-cover — they are cropped instead of stretched.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="d-flex gap-3">
    <img class="object-fit-fill border rounded" width="120" height="120" alt=""
         src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='100'%3E%3Crect width='300' height='100' fill='%23ffc107'/%3E%3Ccircle cx='150' cy='50' r='40' fill='%23dc3545'/%3E%3C/svg%3E">
    <img class="object-fit-cover border rounded" width="120" height="120" alt=""
         src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='100'%3E%3Crect width='300' height='100' fill='%23ffc107'/%3E%3Ccircle cx='150' cy='50' r='40' fill='%23dc3545'/%3E%3C/svg%3E">
  </div>
  <p class="small mt-2">Left: <code>object-fit-fill</code> (stretched circle). Right: <code>object-fit-cover</code> (cropped, circle stays round).</p>
</body>
</html>

Tip: Always set alt text. Describe the image if it carries information, or use alt="" for purely decorative images so screen readers skip them.

Test yourself

Which class stops an image from becoming wider than its container?

  1. .img-responsive
  2. .img-fluid
  3. .img-thumbnail
  4. .w-auto

How do you centre a block-level image horizontally?

  1. .text-center on the image
  2. .d-block .mx-auto
  3. .float-center
  4. .align-middle

What does .rounded-circle do to a non-square image?

  1. Crops it to a square
  2. Makes it an ellipse
  3. Nothing
  4. Makes it a pill shape