Ganesh Putta
Ganesh Putta

Reputation: 2681

images to the center of the div in bootstrap

all the images should be center aligned irrespective to its size. i have tried with below code, but it doesn't work for me. any help would be appreciated

.border-class{
  border:1px solid #ddd;
  padding:5px;
  min-height:160px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
 <div class="col-xs-3"> 
   <div class="border-class">
  <img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre"/> 
     </div>
</div>
<div class="col-xs-3">   
  <div class="border-class">
  <img src="https://cdn2.iconfinder.com/data/icons/hawcons-gesture-stroke/32/icon_27_one_finger_click-128.png" class="img-responsive" alt="Cinque Terre"/> 
    </div>
</div>
<div class="col-xs-3">   
  <div class="border-class">
  <img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre"/> 
    </div>
</div>
<div class="col-xs-3"> 
  <div class="border-class">
  <img src="http://www.w3schools.com/images/colorpicker.png" class="img-responsive" alt="Cinque Terre"/> 
    </div>
</div>
</div>

Upvotes: 1

Views: 845

Answers (2)

singe batteur
singe batteur

Reputation: 400

Use the cover property of background-size :

background-image : ...
background-size : cover;
background-position : center;

Upvotes: 1

Saurav Rastogi
Saurav Rastogi

Reputation: 9731

You can use CSS Flexbox. Just add Flex properties to .border-class

.border-class {
  display: flex;
  justify-content: center;
  align-items: center;
}

See the snippet below:

.border-class{
  /* Add these 'flex' properties */
  display: flex;
  justify-content: center;
  align-items: center;
  border:1px solid #ddd;
  padding:5px;
  min-height:160px;
}
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
 <div class="col-xs-3"> 
   <div class="border-class">
  <img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre"/> 
     </div>
</div>
<div class="col-xs-3">   
  <div class="border-class">
  <img src="https://cdn2.iconfinder.com/data/icons/hawcons-gesture-stroke/32/icon_27_one_finger_click-128.png" class="img-responsive" alt="Cinque Terre"/> 
    </div>
</div>
<div class="col-xs-3">   
  <div class="border-class">
  <img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre"/> 
    </div>
</div>
<div class="col-xs-3"> 
  <div class="border-class">
  <img src="http://www.w3schools.com/images/colorpicker.png" class="img-responsive" alt="Cinque Terre"/> 
    </div>
</div>
</div>

</body>
</html>

Hope this helps!

Upvotes: 6

Related Questions