简体   繁体   English

Bootstrap3 - 在h2内垂直对齐标签

[英]Bootstrap3 - vertically align label within h2

I am facing an interesting issue with Bootstrap3 where the label ("Name") seems to be aligned at the bottom of the <h2> tag (see the blue box in the image (highlighted by the Chrome dev tool). 我正面临着Bootstrap3的一个有趣问题,其中标签(“名称”)似乎在<h2>标签的底部对齐(请参阅图像中的蓝色框(由Chrome开发工具突出显示)。 h2内的标签

(From the Bootstrap 3 documentation putting labels inside headings seem to be a suggested way) (从Bootstrap 3 文档中将标签放在标题内似乎是建议的方式)

I can't figure out how to vertically align the label in center of <h2> . 我无法弄清楚如何在<h2>中心垂直对齐标签。 vertical-align doesnt work on h2 it seems. 垂直对齐似乎不适用于h2。 This answer to another SO post suggests changing the line-height but that doesn't seem to be an intuitive approach since it increases the padding on both sides of h2 (basically increasing the size of the h2 block). 这个对另一个SO帖子的回答建议改变行高,但这似乎不是一个直观的方法,因为它增加了h2两侧的填充(基本上增加了h2块的大小)。

This label is inside a table cell whose code is as follows: 此标签位于表格单元格内,其代码如下:

<td class="text-right"><h2><span class="label label-default vcenter">Name</span></h2></td>

(vcenter is the class I am trying to use to vertically align) (vcenter是我试图用来垂直对齐的类)

It looks like all solutions teach you how to vertically center your .label in the <td> . 看起来所有解决方案都教会您如何在<td>垂直居中.label However, none of them takes into consideration the difference between top and bottom margins on the h2 element. 然而,他们没有考虑到了顶部底部 边缘之间的差别h2元素。

Basically, you're trying to vertically center with something that's not vertically centered. 基本上,你试图垂直居中的东西不是垂直居中的。 The only way to account for it is to raise the label with half the margins difference, which should be .5rem . 解释它的唯一方法是将标签的边距增加一半,即.5rem So this will most likely to the job: 所以这最有可能是这项工作:

h2 > .label { 
   position: relative;
   top: -.5rem;
}

If it doesn't, you have stronger CSS rules applying and I'll need to take a look at them. 如果没有,你有更强的CSS规则应用,我需要看看它们。 Feel free to change -.5rem to whatever works for you (in rem or px ). 随意改变-.5rem到任何适合你的东西(在rempx )。


Another approach is to use any vertical centering technique and make the top and bottom margins equal on td>h2 elements: 另一种方法是使用任何垂直居中技术,并使td>h2元素的顶部和底部边距相等:

td>h2 {  margin-top: 1rem; margin-bottom: 1rem; }

After this, most of the other answers will work. 在此之后,大多数其他答案将起作用。

You can add display: flex to the headline tag and then center its children vertically: 您可以将display: flex添加到标题标记,然后将其子项垂直居中:

 h2 { display: flex; align-items: center; /* Just for presentation */ background: #ececec; height: 100px; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <td class="text-right"> <h2><span class="label label-default vcenter">Name</span></h2> </td> 

add valign middle for td td添加valign middle

<td valign="middle" class="text-right"><h2><span class="label label-default vcenter">Name</span></h2></td>

or 要么

add this css 添加此CSS

.table tbody>tr>td.vert-align
{
    vertical-align: middle;
}

<td  class="text-right vert-align"><h2><span class="label label-default vcenter">Name</span></h2></td>

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

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