简体   繁体   English

在缩略图和网格中具有Bootstrap的垂直居中图像

[英]Vertically centered images with Bootstrap in thumbnail and grid

I want vertically centered images with Bootstrap inside an grid class and in thumbnail class. 我想在网格类和缩略图类中使用Bootstrap垂直居中放置图像。 The thumbnail classes should be same height. 缩略图类的高度应相同。 And the paragraph should be at the same level below the images. 并且该段落应位于图片下方的同一级别。 Have tried everything possible and on the Internet search I have not found a suitable solution that works. 尝试了所有可能的方法,并且在Internet搜索上我没有找到合适的解决方案。 Examples there are only images with grid or images with thumbnail, but not in combination with grid and thumbnail. 例如,仅存在带有网格的图像或带有缩略图的图像,而没有与网格和缩略图组合的图像。 Does anyone have a solution to my problem? 有人能解决我的问题吗?

<div class="row">
          <div class="col-md-3">
            <div class="thumbnail">
              <div><img src="pics/logo_pic_height_100px.jpg" ></div>
            </div>
            <p>Some Text</p>
          </div><!--

          --><div class="col-md-3">
            <div class="thumbnail">
              <div><img src="pics/logo_pic_height_60px" ></div>
            </div>
            <p>Some Text</p>
          </div><!--

          --><div class="col-md-3">
            <div class="thumbnail">
              <div><img src="pics/logo_pic_height_120px.jpg" ></div>
            </div>
            <p>Some Text</p>            
          </div><!--

          --><div class="col-md-3">
            <div class="thumbnail">
              <div><img src="pics/logo_pic_height_80px.jpg" ></div>
            </div>
            <p>Some Text</p>
          </div>
        </div>

That doesn't work! 那不行!

.thumbnail{
    display: inline-block;
    vertical-align: middle;
}

The parent element containing .thumbnail must have display:table; 包含.thumbnail的父元素必须具有display:table; for vertical-align: middle; 对于vertical-align: middle; to work. 上班。 Best-practice is to use a <table> layout and assign vertical-align to elements you want aligned middle vertically, such as: 最佳实践是使用<table>布局,然后将vertical-align分配给要垂直居中vertical-align的元素,例如:

table, tbody, thead, th, tr, td, td table {
  padding: 0;
  margin: 0;
  vertical-align: middle;
}
td, th {
  display: table-cell;
  vertical-align: inherit;
}

td elements (or .thumbnail in your case) should be display: table-cell; td元素(或您的情况下的.thumbnail )应display: table-cell; inside the display:table; display:table;内部display:table; element. 元件。

Note that line-height value assigned to the parent element also has an effect when using vertical-align: middle; 请注意,当使用vertical-align: middle;时,分配给父元素的line-height值也会起作用vertical-align: middle; on it's children. 在它的孩子上。

I would not advise using top: 50%; transform: translateY(-50%); 我不建议使用top: 50%; transform: translateY(-50%); top: 50%; transform: translateY(-50%); as suggest above by others, it's simply not best-practice. 正如上面其他人所建议的那样,这根本不是最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM