Open In App

Bootstrap 4 | Images

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Bootstrap offers different classes for images to make their appearance better and also to make them responsive. Making an image responsive means it should scale according to its parent element. That is, the size of the image should not overflow its parent element and will grow and shrink according to the change in the size of its parent without losing its aspect ratio.

The different classes available in Bootstrap for images are as explained below:

Responsive Images: The .img-fluid class is used within the <img> tag to create the responsive image. The responsive image is used to adjust the image automatically to the specified box. 

Syntax:

<img src="image_source" class="img-fluid" ...>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Images</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <div class="container">
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
        <h2>Responsive Image</h2>
        <img class="img-fluid" src=
            alt="Responsive image" width="667" height="440"/>
    </div>
</body>
</html>


Output:

Rounded Corners Image: The .rounded class is used to create a rounded corner image. This class is used with <img> tag.

Syntax:

<img src="image_source" class="rounded" ...>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Images</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <div class="container">
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
        <h2>Rounded Corner Image</h2>
        <img class="rounded" src=
            alt="Responsive image" width="367" height="340"/>
    </div>
</body>
</html>


Output:

Circle Image: The .rounded-circle class is used to create the circle shape image. 

Syntax:

<img src="image_source" class="rounded-circle" ...>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Images</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <div class="container">
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
        <h2>Circle Image</h2>
        <img class="rounded-circle" src=
            alt="Responsive image" width="467" height="340"/>
    </div>
</body>
</html>


Output:

Thumbnail Image: The .img-thumbnail class is used to create a thumbnail (bordered) image. 

Syntax:

<img src="image_source" class="img-thumbnail" ...>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Images</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <div class="container">
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
        <h2>Thumbnail Image</h2>
        <img class="img-thumbnail" src=
            alt="Responsive image" width="467" height="340"/>
    </div>
</body>
</html>


Output:

Aligning Image: The .float-left and .float-right class is used to set the left and right alignment of the image.

Syntax:

<img src="image_source" class="float-left/float-right" ...>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Images</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <div class="container">
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
        <h2>Aligning Image</h2>
        <!-- Bootstrap float-left class -->
        <img class="float-left" src=
            alt="Responsive image" width="250" height="250"/>
        <!-- Bootstrap float-right class -->
        <img class="float-right" src=
            alt="Responsive image" width="250" height="250"/>
    </div>
</body>
</html>


Output:

Centered Image: The .mx-auto (margin:auto) and .d-block (display:block) classes are used to set the image into center.

Syntax:

<img src="image_source" class="mx-auto d-block" ...>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Images</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
<body>
    <div class="container">
        <h1 style="color:green;text-align:center;">
            GeeksforGeeks
        </h1>
        <h2 style="text-align:center;">Centered Image</h2>
        <img class="mx-auto d-block" src=
            alt="Responsive image" width="350" height="250"/>
    </div>
</body>
</html>


Output:


Supported Browser:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


Last Updated : 28 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads