html - Bootstrap 如何让文本在 div 容器中垂直对齐

标签 html css twitter-bootstrap twitter-bootstrap-3 vertical-alignment

垂直对齐列中间文本的最佳/正确方法是什么?图片高度在 CSS 中静态设置。

我已经尝试将外部 div 设置为 display: table 并将内部 div 设置为 display: table-cellvertical-align: middle 但这也不起作用。

enter image description here

HTML

<section id="browse" class="browse">
    <div class="container">
        <div class="row">
            <div class="col-md-5 col-sm-5">
                <h2 class="text-left">Link up with other gamers all over the world who share the same tastes in games.</h2>
            </div>
            <div class="col-md-1"></div>
            <div class="col-md-6 col-sm-7 animation_container">
                <img id="animation_img2" class="animation_img animation_img2" src="images/section2img2.png"/>
                <img id="animation_img1" class="animation_img animation_img1" src="images/section2img1.png"/>
            </div>
        </div>
    </div>
</section>

CSS

.browse .container, .blind_dating .container { padding-bottom: 0; }
.animation_container { position: relative; }
.browse .animation_container { height: 430px; }
.animation_img { position: absolute; bottom: 0; }
.animation_img1 { right: 25%; }
.animation_img2 { right: 25%; }

最佳答案

HTML:

首先,我们需要向您的文本容器添加一个类,以便我们可以相应地访问它并为其设置样式。

<div class="col-xs-5 textContainer">
     <h3 class="text-left">Link up with other gamers all over the world who share the same tastes in games.</h3>
</div>

CSS:

接下来,我们将根据旁边图像 div 的大小应用以下样式使其垂直对齐。

.textContainer { 
    height: 345px; 
    line-height: 340px;
}

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

全部完成!如果您认为上面的样式仍然稍微不对齐,请调整行高和高度。

WORKING EXAMPLE

关于html - Bootstrap 如何让文本在 div 容器中垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25538732/

相关文章:

jquery - 背景渐变不适用于 Bootstrap navbar-fixed-top

css - 在 bootstrap 3 上提前输入

javascript - 从事件监听器获取嵌套的 li 'keyup'

html - 使用以 % 为单位的高度和以 px 为单位的边框时,框尺寸过大

javascript - .addClass 使用 else if 语句 - jQuery?

javascript - 将按钮类型从按钮更改为提交

html - Bootstrap Divs 或 Container 覆盖的背景图像

javascript - 如何从另一个同级组件(​​跳过链接)定位 Angular 中的 html 元素?

javascript - 在 SVG 中切换图层

html - 我的 CSS 中的媒体查询不起作用