如何让div垂直居中?

发布于 2024-10-07 04:01:13 字数 460 浏览 0 评论 0原文

可能的重复:
将 Div 垂直居中的最佳方式是什么CSS

.title_text_only{
position:absolute;
width:100%;
display:none;
z-index:9;
top:50%;
bottom:50%;
text-align:center;
color:#fff;
font-size:18px;
text-shadow: 1px 1px 1px #666;
}

现在,它似乎不起作用。前 50% 不垂直居中。有点到底部了。就好像顶部边框是 50% 一样。

Possible Duplicate:
What's The Best Way of Centering a Div Vertically with CSS

.title_text_only{
position:absolute;
width:100%;
display:none;
z-index:9;
top:50%;
bottom:50%;
text-align:center;
color:#fff;
font-size:18px;
text-shadow: 1px 1px 1px #666;
}

Right now, it doesn't seem to work. top 50% doesn't center it vertically. It's a little to the bottom. It's as if the top border is 50%.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂归处 2024-10-14 04:01:13

如果可以指定盒子的高度,可以使用 margin-top: -[height / 2]px (填写[height / 2],注意大多数浏览器将在 100% 缩放时将小数像素四舍五入)。

如果您可以指定其父级的高度,则可以执行以下操作:

parent {
    height: [height]px;
}
child {
    /* Make the element part of the inline flow, as vertical-align isn't supported on block elements */
    display: -moz-inline-box; /* Old Firefox doesn't support inline-block */
    display: inline-block;
    /* IE < 8 doesn't support inline-block on native block-level elements */
    *display: inline;
    *zoom: 1;

    /* The interesting bit */
    line-height: [height]px;
    vertical-align: middle;
}

如果预计子级的内容会换行到多行,则可以将其包装在重置 line-height< 的子子级中/代码>。

If you can specify the height of the box, you can use margin-top: -[height / 2]px (fill in [height / 2] and note that most browsers will round fractional pixels up at 100% zoom).

If you can specify the height of its parent, you can do something like this:

parent {
    height: [height]px;
}
child {
    /* Make the element part of the inline flow, as vertical-align isn't supported on block elements */
    display: -moz-inline-box; /* Old Firefox doesn't support inline-block */
    display: inline-block;
    /* IE < 8 doesn't support inline-block on native block-level elements */
    *display: inline;
    *zoom: 1;

    /* The interesting bit */
    line-height: [height]px;
    vertical-align: middle;
}

If the content of the child is expected to wrap onto multiple lines, you can wrap it in a sub-child that resets the line-height.

最终幸福 2024-10-14 04:01:13

top: 50%; 的作用正是您所怀疑的:它将顶部边缘置于 50% 的高度。您还可以通过应用等于元素高度一半的负垂直边距来抵消此效果。例如,如果元素高 100px,您将添加以下属性:

margin-top: -50px;

top: 50%; does exactly what you suspect: it places the top edge at 50% height. You can offset this effect by also applying a negative vertical margin equal to half of the height of the element. For example, if the element was 100px high, you would add this property:

margin-top: -50px;
柏拉图鍀咏恒 2024-10-14 04:01:13

如果元素的高度是固定的,则执行以下

Css:

.title_text_only{
position:absolute;
width:100%;
display:none;
z-index:9;
**top:50%;**
bottom:50%;
text-align:center;
color:#fff;
font-size:18px;
text-shadow: 1px 1px 1px #666;
**margin-top: -(half the height)**
}

否则要以动态高度垂直居中 div,您将需要 javascript..

document.getElementById('myElement').style.marginTop = (-1) * document.getElementById('myElement').offsetHeight / 2

If height of element is fixed, then do following

Css:

.title_text_only{
position:absolute;
width:100%;
display:none;
z-index:9;
**top:50%;**
bottom:50%;
text-align:center;
color:#fff;
font-size:18px;
text-shadow: 1px 1px 1px #666;
**margin-top: -(half the height)**
}

else to vertically center a div with dynamic height, you would need javascript..

document.getElementById('myElement').style.marginTop = (-1) * document.getElementById('myElement').offsetHeight / 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文