【发布时间】:2020-12-04 18:52:56
【问题描述】:
我无法让文本出现在网页的屏幕中间(高度方向)。该网站的 HTML 是:
<html lang="en">
<head>
<title>example</title>
<link href="example.css" rel="stylesheet">
</head>
<body>
<div class="home-container">
<div class="home-row">
<div class="some-other-class">
<p>text that should be in the middle</p>
</div>
</div>
</div>
</body>
</html>
我想要做的是让 home-container 元素一直延伸到页面底部,并在其中间放置 text that should be in the middle。我的css 看起来像:
html, body{
height:100%;
}
.home-container{
width: 100%;
height: 100%;
background-color: rgba(139,0,0,0.4);
}
.home-row{
vertical-align: middle;
}
我知道,如果我改成这样home-container,我想做的事情是可能的:
.home-container{
width: 100%;
height: 100%;
background-color: rgba(139,0,0,0.4);
align-items: center;
display: flex;
}
但这不适用于所有浏览器。我对 vertical-align 属性做错了什么?在我的示例中,Id 并没有真正做任何事情...
【问题讨论】:
-
要能够使用
vertical-align,您需要在大容器(.home-container)上使用display:table,在.home-row上使用display:table-cell -
使用 CSS 表格和定位属性:stackoverflow.com/a/31977476/3597276