html - 垂直和水平居中时出现滚动条,无法摆脱

标签 html css twitter-bootstrap

使用 Bootstrap 3.0.3,我试图将具有硬编码宽度和高度的 div 水平和垂直居中。 JSFiddle 中包含最新代码,此处也报告与 SO 关于 JSFiddle 的规则一致。

http://jsfiddle.net/alex_kurilin/pNYg9/

HTML:

<div class="text-center full-height">
    <div class="inline full-height">
        <div class="fake-table full-height">
            <div class="fake-table-cell full-height">
                <div class="content fake-table">
                    <div class="fake-table-cell">foobar</div>
                </div>
            </div>
        </div>
    </div>
</div>

CSS:

html, body {
    height: 100%;
}

.fake-table {
    display: table;
}
.fake-table-cell {
    display: table-cell;
    vertical-align: middle;
}
.inline {
    display: inline-block;
}
.text-center {
    text-align: center;
}
.full-height {
    height: 100%;
}
.content {
    background-color: grey;
    width: 300px;
    height: 150px;
}

我在 JSFiddle 中展示的内容似乎“有效”,但出于某种原因它添加了一个垂直滚动条。有趣的是,改变 body 的 font-sizefont-familyline-height 似乎会影响滚动条,因此我想与 height: 100%inline-block div 有关。

我希望得到关于如何使这个特定布局正确发生的两个指示,因为我怀疑我这样做很困难。

最佳答案

为什么不只使用内容元素并删除其他元素:

.content {
    background-color: grey;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -150px;
    margin-top: -75px;
    width: 300px;
    height: 150px;
}

关于html - 垂直和水平居中时出现滚动条,无法摆脱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448162/

相关文章:

css - bootstrap.css : . 容器:显示表之前

html - 内容区域向下滑动到左侧边栏的左侧

javascript - 如何使滚动到顶部的按钮动画流畅

html - 如何使用动态内容的 CSS 流体间距保持对称

css - 在 WebStorm 的单行注释 block 中添加空格

CSS 位置 div 在背景图像上的响应

twitter-bootstrap - Twitter Bootstrap 和 ReCaptcha

html - 如何使用 HTML/CSS 为我的网站创建要点?

html - 在所有分辨率下保持元素的相同高度

javascript - 单击链接后自动将文本输入外部网站的文本框