简体   繁体   English

引导文本在div中垂直居中

[英]Bootstrap vertically center text in div

I'm trying to vertically center text in a div, but I can't get it working. 我正在尝试在div中垂直居中放置文本,但无法正常工作。

The code: 编码:

<div>
    <h3><i class="fa fa-cloud fa fa-3x"></i> Text</h3>
</div>

在此处输入图片说明

Does anyone have a solution for this? 有人对此有解决方案吗? I tried many possibilities but none of them seem to work. 我尝试了很多可能性,但似乎没有一个可行。

This is how I ended up centering the cloud glyphicon: 这就是我最终使云字形居中的方式:

CSS 的CSS

<style>
    h3 {
        line-height: 48px;
    }
    span {
        vertical-align: middle;
    }
    .glyphicon-cloud {
        font-size: 48px;
    }
</style>

HTML 的HTML

<div>
    <h3>
        <span class="glyphicon glyphicon-cloud"></span>
         Text
    </h3>
</div>

I set the line-height and font-size of the glyphicon and h3 to the same value of 48px and then applied vertical-align: middle; 我将glyphicon和h3line-heightfont-size设置为相同的48px值,然后应用vertical-align: middle; to the <span> that contained the glyphicon. 到包含字形图标的<span>

edit: 编辑:

Taken from the Bootstrap API website , you shouldn't mix classes with the <span> , as you seem to have: 来自Bootstrap API网站 ,您不应将类与<span>混合使用,因为您似乎拥有:

Don't mix with other components Icon classes cannot be directly combined with other components. 不要与其他组件混用图标类不能直接与其他组件组合。 They should not be used along with other classes on the same element. 它们不应与同一元素上的其他类一起使用。 Instead, add a nested and apply the icon classes to the <span> . 而是添加一个嵌套并将图标类应用于<span>

I would suggest vertical-align: middle applied your icon glyph. 我建议vertical-align: middle应用图标字形。 For example: 例如:

h3 .fa {
    vertical-align: middle;
}

http://jsfiddle.net/22cYh/ http://jsfiddle.net/22cYh/

垂直对齐到img上,而不是文本上。

您必须添加显示:inline-block

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

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