在bootstrap col / row

时间:2016-06-15 15:05:03

标签: javascript jquery html css twitter-bootstrap

我知道这个话题已经无休止地讨论了,但我已经尝试了很多解决方案而且都没有。在下面的代码段中,如何使图像在col / row div中垂直居中?使用jQuery的解决方案是首选(我更有可能打破CSS)。我事先并不知道图像的高度。



.row {
  /* Just for demo */
  border: 2px solid black;
}

/* My latest attempt at centering - this code can all be removed if desired*/
.book-image-container {
  display: table;
  table-layout: fixed;
}
.book-image {
  display: table-cell;
  vertical-align: middle;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-2">
      <div class="book-image-container">
        <img class="book-image" src="http://static.loot.co.za/images/x80/691858851600179215$622D8483">
      </div>
    </div>
    <div class="col-xs-6">
      <h3>Lorem ipsum</h3>
      <p>Bla bla bla</p>
      <p>Bla bla bla</p>
      <p>Bla bla bla</p>
      <p>Bla bla bla</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

编辑:

感谢Jurik工作,这里需要最小的CSS:

&#13;
&#13;
.row {
  /* Just for demo */
  border: 2px solid black;
}

.book-image-container {
  display: table-cell;
  vertical-align: middle;
  height: 116px;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-2">
      <div class="book-image-container">
        <img class="book-image" src="http://static.loot.co.za/images/x80/691858851600179215$622D8483">
      </div>
    </div>
    <div class="col-xs-6">
      <h3>Lorem ipsum</h3>
      <p>Bla bla bla</p>
      <p>Bla bla bla</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您必须为您的班级book-image-container提供display: table-cell css属性和高度。

.book-image-container {
  display: table;
  table-layout: fixed;
  display: table-cell;
  vertical-align: middle;
  height: 116px;
}

答案 1 :(得分:0)

我不会用jQuery解决这个问题,但是嘿,你问过它:http://jsbin.com/dumifahuja/edit?html,css,js,console,output

找到最近的父行的高度,从中减去图像的高度,然后将其除以2并将call sub_that_wants_3_elm_array((/1,2,3/),output_arg) 设置为该数量。