图像到bootstrap中div的中心

时间:2016-11-08 10:11:12

标签: css twitter-bootstrap

所有图像都应该居中对齐,而不管其大小。 我尝试使用以下代码,但它对我不起作用。 任何帮助将不胜感激



.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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:6)

您可以使用 CSS Flexbox 。只需将Flex属性添加到.border-class

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

请参阅下面的代码:

.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>

希望这有帮助!

答案 1 :(得分:1)

使用cover的{​​{1}}属性:

background-size