Bootstrap Images
Bootstrap provides useful classes to make images responsive, rounded, circular, thumbnail-style, aligned and properly displayed on different devices.
What are Images?
Images are visual elements used to display photos, graphics, logos, banners and other visual content on a web page.
In HTML, images are normally displayed using the
<img> element.
.img-fluid,
.rounded,
.rounded-circle and
.img-thumbnail.
1. Basic Image
A normal HTML image can be displayed using the
<img> tag.
<img src="/Images/Boostrap/Image1.png"
alt="Bootstrap Basic Image">
Output:
2. Responsive Images
Responsive images automatically adjust their size according to the available screen width.
Bootstrap provides the
.img-fluid class for responsive images.
<img src="/Images/Boostrap/Image2.png"
class="img-fluid"
alt="Responsive Image">
.img-fluid applies
max-width: 100% and
height: auto.
3. Rounded Images
The .rounded class adds rounded corners
to an image.
<img src="/Images/Boostrap/Image3.png"
class="img-fluid rounded"
alt="Rounded Image">
Output:
4. Circular Images
The .rounded-circle class makes an image
circular.
<img src="/Images/Boostrap/Image4.png"
class="rounded-circle"
alt="Circular Image">
Output:
5. Image Thumbnail
The .img-thumbnail class creates an
image with a border and rounded corners.
<img src="/Images/Boostrap/Image5.png"
class="img-thumbnail"
alt="Thumbnail Image">
Output:
6. Align Image to the Left
Bootstrap's .float-start utility can
be used to move an image toward the start of the
container.
<img src="/Images/Boostrap/Image2.png"
class="float-start me-3 rounded"
alt="Left Image">
This text appears beside the image. The
me-3 class provides spacing between
the image and the text.
7. Align Image to the Right
The .float-end class moves an image
toward the end of the container.
<img src="/Images/Boostrap/Image3.png"
class="float-end ms-3 rounded"
alt="Right Image">
This text flows around the image. The
ms-3 class adds spacing between the
text and image.
8. Center an Image
To center an image, we can use
d-block together with
mx-auto.
<img src="/Images/Boostrap/Image4.png"
class="d-block mx-auto rounded"
alt="Centered Image">
Output:
9. Image Width Utilities
Bootstrap provides width utilities that can be combined with images.
<img src="/Images/Boostrap/Image1.png"
class="w-25"
alt="Image">
<img src="/Images/Boostrap/Image1.png"
class="w-50"
alt="Image">
<img src="/Images/Boostrap/Image1.png"
class="w-75"
alt="Image">
<img src="/Images/Boostrap/Image1.png"
class="w-100"
alt="Image">
10. Bootstrap Figure
Bootstrap provides figure classes for displaying an image together with a caption.
<figure class="figure">
<img src="/Images/Boostrap/Image1.png"
class="figure-img img-fluid rounded"
alt="Figure Image">
<figcaption class="figure-caption">
Bootstrap Figure Example
</figcaption>
</figure>
11. Responsive Figure
We can combine Bootstrap's figure and responsive image classes to create a responsive figure.
<figure class="figure">
<img src="/Images/Boostrap/Image2.png"
class="figure-img img-fluid rounded"
alt="Responsive Figure">
<figcaption class="figure-caption">
Responsive Figure
</figcaption>
</figure>
12. CIIT Training Institute Example
Suppose CIIT Training Institute wants to display course images on its website.
<div class="text-center">
<img src="/Images/Boostrap/Image1.png"
class="img-fluid rounded shadow"
alt=".NET Full Stack">
<h4 class="mt-3">
.NET Full Stack Development
</h4>
</div>
.NET Full Stack Development
Live Project | Internship | Interview Preparation
Important Bootstrap Image Classes
| Class | Purpose |
|---|---|
.img-fluid |
Makes an image responsive. |
.rounded |
Adds rounded corners. |
.rounded-circle |
Creates a circular image. |
.img-thumbnail |
Creates a bordered thumbnail image. |
.float-start |
Moves the image toward the start. |
.float-end |
Moves the image toward the end. |
.d-block |
Makes the image a block element. |
.mx-auto |
Helps center a block-level image. |
.w-25 |
Sets width to 25%. |
.w-50 |
Sets width to 50%. |
.w-75 |
Sets width to 75%. |
.w-100 |
Sets width to 100%. |
.figure |
Creates a figure container. |
.figure-img |
Styles an image inside a figure. |
.figure-caption |
Styles the figure caption. |
Common Mistakes
-
Forgetting the
altattribute. -
Using very large images without
.img-fluid. -
Using
.rounded-circleon a non-square image and expecting a perfect circle. -
Using
float-startorfloat-endwithout clearing the floating content when necessary. - Using fixed image dimensions that break the mobile layout.
- Forgetting to provide meaningful alternative text for accessibility.
Summary
- Bootstrap provides many useful classes for images.
-
.img-fluidmakes images responsive. -
.roundedcreates rounded corners. -
.rounded-circlecreates circular images. -
.img-thumbnailcreates thumbnail-style images. -
.float-startand.float-endhelp align images. -
d-block mx-autocan be used to center an image. -
Width utilities such as
w-25,w-50,w-75andw-100control image width. - Bootstrap's figure classes can be used to display images with captions.