简体   繁体   English

当使用Bootstrap 3进行图像处理时,使DIV中的文本垂直居中(垂直对齐)

[英]Center text vertical in a DIV when images is involve using Bootstrap 3 (vertical-align)

I am trying to center a text vertically so it looks nice using Twitter Bootstrap 3 and CSS but I can't get it to work. 我正在尝试将文本垂直居中,因此使用Twitter Bootstrap 3和CSS看起来不错,但我无法使其正常工作。 I have the following HTML: 我有以下HTML:

<div class="col-md-3 vcenter">
  <img src="https://dummyimage.com/30x30/000/fff" class="pull-left">
</div>
<div class="col-md-9 vcenter">
  <h3>header 3</h3>
</div>

Then I am applying the following CSS (found here ): 然后,我将应用以下CSS(在此处找到):

.vcenter {
  display: inline-block;
  vertical-align: middle;
  float: none;
}

Here is Fiddle with an example of what's currently happening. 这是Fiddle,提供了一个有关当前情况的示例。 Can I get some help? 我可以帮忙吗?

 img{ width:200px; height:200px; } .vcenter { display: inline-block; vertical-align: middle; } 
 <div class="col-md-3 vcenter"> <img src="https://i.stack.imgur.com/wwy2w.jpg" class="pull-left"> </div> <div class="col-md-9 vcenter"> <h3>header 3</h3> </div> 

If able, can you modify your HTML to use a single column row? 如果可以,您可以修改HTML以使用单列行吗?

Attached JS Fiddle: https://jsfiddle.net/qgas0tej/3/ 附加的JS小提琴: https : //jsfiddle.net/qgas0tej/3/

Here is a possible solution that may be what you're looking for: 这是您正在寻找的可能的解决方案:

HTML: HTML:

<div class="row">
  <div class="col-md-12">
    <div class="vcenter">
      <img src="https://dummyimage.com/30x30/000/fff">
      <h3>header 3</h3>
    </div>
  </div>
</div>

CSS: CSS:

.vcenter {
  display: table;
}
img {
  display: block;
  float: left;
}
h3 {
  padding-left: 10px;
  display: table-cell;
  vertical-align: middle;
  }

There's nothing wrong with your vcenter style. 您的vcenter样式没有任何问题。 The problem lies in the bootstrap margin of h3 tag not being vertically consistent, detailed below. 问题在于h3标签的引导程序边距在垂直方向上不一致,详情如下。

This shows the bootstrap inconsistent vertical margin: 这表明引导程序的垂直边距不一致:

SS 1

Just adding margin to your h3 tag makes it vertically consistent and fixes the issue: 只需在h3标签上增加边距,即可使其在垂直方向上保持一致并解决以下问题:

SS 2

CSS: CSS:

.vcenter {
  display: inline-block;
  vertical-align: middle;
  float: none;
}

h3 {
   margin-bottom: 20px;
}

HTML: HTML:

<div class="col-md-3 vcenter">
  <img src="https://dummyimage.com/30x30/000/fff" class="pull-left">
</div>
<div class="col-md-9 vcenter">
  <h3>header 3</h3>
</div>

Solution in JSFiddle JSFiddle中的解决方案

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

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