水平对齐将div与红色边框对齐

时间:2018-03-16 07:27:57

标签: html css html5 css3 twitter-bootstrap-3

我想水平对齐div的中心和红色边框,并在图像中间垂直对齐。垂直对齐中间工作但水平对齐中心不起作用。这是我的代码:



.vcenter {
  display: table-cell;
  vertical-align: middle;
}

<div class="text-center" style="border: 1px solid black;padding-bottom:10px; width:100%;height:100%;">
  <h2>xyz</h2>

  <div style="height: 500px;width: 500px;border: 1px solid red; text-align: center; margin: 0 auto" class="vcenter center-block"><img src="" alt="Cinque Terre" width="304" height="236">
  </div>

</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
.vcenter {
  display: flex;
  justify-content:center; //making content center;
  align-items:center; //vertical align to middle of div  
}
&#13;
<div class="text-center" style="border: 1px solid black;padding-bottom:10px; width:100%;height:100%;">
  <h2>xyz</h2>

  <div style="height: 500px;width: 500px;border: 1px solid red; text-align: center; margin: 0 auto" class="vcenter center-block"><img src="" alt="Cinque Terre" width="304" height="236">
  </div>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
.vcenter {
  display: table-cell;
  vertical-align: middle;
}
.text-center div{
  display: flex;
}
&#13;
<div class="text-center" style="border: 1px solid black;padding-bottom:10px; width:100%;height:100%;">
  <h2>xyz</h2>

  <div style="height: 500px;width: 500px;border: 1px solid red; text-align: center; margin: 0 auto" class="vcenter center-block"><img src="" alt="Cinque Terre" width="304" height="236">
  </div>

</div>
&#13;
&#13;
&#13;