How To Use Images In Bootstrap

Images are very common in a website design. So, it is very important to style the images and place them properly in webpages for a better user experience. Bootstrap provides us with four built-in classes for image styling.
  1.  .img-rounded
  2.  .img-circle
  3.  .img-responsive
  4.  .img-thumbnail
Now, let us have a close look how these classes work in bootstrap.

.img-rounded

In bootstrap, this class is used to add rounded corners to an image. This class is used inside the container class. This type of image in Bootstrap is a more basic kind of image used in simple webpages. However, IE8 does not support rounded corners.

Example
 
Let us understand the use of .img-rounded class with the help of below example.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <title>Bootstrap Example</title>  
  5.   <meta charset="utf-8">  
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  9.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  10. </head>  
  11. <body>  
  12.   
  13. <div class="container">  
  14.   <h2>Rounded Corners</h2>  
  15.   <p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>              
  16.   <img src="D:\suraj\q4.jpg" class="img-rounded" alt="Cinque Terre" width="304" height="236">   
  17. </div>  
  18.   
  19. </body>  
  20. </html>  
Src is used to give the image path, width attribute is used to set the image width, height attribute is used to set the height, and alt is used to provide an alternative text if the image cannot be displayed due to some reason.
 
Output

2
 
.img-circle

In bootstrap, this class is used to give circular shape to an image. All the attributes used in .img-rounded class can be used in this class also, with the exact meaning.

Example
 
Let us see this class with the help of same image which we used in earlier example. 
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <title>Bootstrap Example</title>  
  5.   <meta charset="utf-8">  
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  9.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  10. </head>  
  11. <body>  
  12.   
  13. <div class="container">  
  14.   <h2>Rounded Corners</h2>  
  15.   <p>The .img-circle class gives circle shape to an image (not available in IE8):</p>              
  16.   <img src="D:\suraj\q4.jpg" class="img-circle" alt="Cinque Terre" width="304" height="236">   
  17. </div>  
  18.   
  19. </body>  
  20. </html>   
Output

4

Img-responsive

In bootstrap, we can make an image responsive by using this class. Responsive images automatically adjust to fit in all size screens. The .img-responsive class applies max-width i.e. 100% and height automatically to image while the display will be in block view. This way of defining images is most popular and demanding now-a-days, the reason being responsive webpages are in demand.

Example
 
Below is the bootstrap responsive image example. We have used the same set of attributes which we used in the above classes.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <title>Bootstrap Example</title>  
  5.   <meta charset="utf-8">  
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  9.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  10. </head>  
  11. <body>  
  12.   
  13. <div class="container">  
  14.   <h2>Image</h2>  
  15.   <p>Resize the browser window to see the effect:</p>  
  16.   <img class="img-responsive" src="D:\suraj\q4.jpg" alt="Chania" width="460" height="345">   
  17. </div>  
  18.   
  19. </body>  
  20. </html>  
Output

To see the responsive image effect, resize your browser or open this in different screen sizes.

6

Img-thumbnail

We can use this class to create a thumbnail image in Bootstrap. Thumbnail adds a bit of padding and grey border to the image. There are two ways by which we can use thumbnail images in bootstraps i.e. Single thumbnail and multiple thumbnail (image gallery).

Single Thumbnail Image Example

First, let us have an example of single thumbnail image. In this, we can use only one image as a thumbnail image. All attributes - src, width, height, and alt are used with the same exact purpose.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <title>Bootstrap Example</title>  
  5.   <meta charset="utf-8">  
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  9.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  10. </head>  
  11. <body>  
  12.   
  13. <div class="container">  
  14.   <h2>Thumbnail</h2>  
  15.   <p>The .img-thumbnail class creates a thumbnail of the image:</p>              
  16.   <img src="D:\suraj\q4.jpg" class="img-thumbnail" alt="Cinque Terre" width="400" height="312">   
  17. </div>  
  18.   
  19. </body>  
  20. </html>   
Output

8

Multiple Thumbnail images (Image Gallery)

We can also use .thumbnail class to create an image gallery too. For using a thumbnail image and making it as an image gallery, we have to use multiple thumbnail images. Let us see this with the help of an example. 
  1. <html lang="en">  
  2. <head>  
  3.   <title>Bootstrap Image Example</title>  
  4.   <meta charset="utf-8">  
  5.   <meta name="viewport" content="width=device-width, initial-scale=1">  
  6.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  7.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  8.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  9. </head>  
  10. <body>  
  11.   
  12. <div class="container">  
  13.   <h2>Image Gallery</h2>  
  14.   <p>The .thumbnail class can be used to create an image gallery.</p>  
  15.   <p>The .caption class adds a dark grey color to text inside thumbnails and proper padding.</p>  
  16.   <p>Click on the images to see the effects.</p>  
  17.   <div class="row">  
  18.     <div class="col-md-4">  
  19.       <div class="thumbnail">  
  20.         <a href="D:\suraj\q4.jpg" target="_blank">  
  21.           <img src="D:\suraj\q4.jpg" alt="Pulpit Rock" style="width:100%">  
  22.           <div class="caption">  
  23.             <p>Albert Einstein's words .</p>  
  24.           </div>  
  25.         </a>  
  26.       </div>  
  27.     </div>  
  28.     <div class="col-md-4">  
  29.       <div class="thumbnail">  
  30.         <a href="D:\suraj\q5.jpg" target="_blank">  
  31.           <img src="D:\suraj\q5.jpg" alt="Moustiers Sainte Marie" style="width:100%">  
  32.           <div class="caption">  
  33.             <p>Dalai Lama's words.</p>  
  34.           </div>  
  35.         </a>  
  36.       </div>  
  37.     </div>  
  38.     <div class="col-md-4">  
  39.       <div class="thumbnail">  
  40.         <a href="D:\suraj\q6.jpg" target="_blank">  
  41.           <img src="D:\suraj\q6.jpg" alt="Cinque Terre" style="width:100%">  
  42.           <div class="caption">  
  43.             <p>Steve Jobs words.</p>  
  44.           </div>  
  45.         </a>  
  46.       </div>  
  47.     </div>  
  48.   </div>  
  49. </div>  
  50.   
  51. </body>  
  52. </html>  
All attributes like src, width, height and alt are used with the same exact purpose but as you can see, we have used some more attributes like href which specifies the URL of the page the link goes to and target attribute is used to specify where to open the link. Caption class is used to add text caption to the images.
 
Output

11

In this article, we learned different ways we can style the images in Bootstrap. I hope, you have enjoyed learning.


Similar Articles