以下不是垂直对齐 div 与浏览器选项卡中的文本(它是正确水平对齐):
<div style="height: 100%; display: flex; align-items: center; justify-content: center;">
<div>
Some vertical and horizontal aligned text
</div>
</div>
怎么了?
原文由 EricC 发布,翻译遵循 CC BY-SA 4.0 许可协议
请尝试以下代码
body, html{
height:100%;
width:100%;
padding:0px;
margin:0px;
}
.demo-wp{
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background:green;
height:100%;
}
.inner-div{
background:gold;
padding:20px;
}
<div class="demo-wp">
<div class="inner-div">
Some vertical and horizontal aligned text
</div>
</div>
原文由 Santosh Khalse 发布,翻译遵循 CC BY-SA 3.0 许可协议
3 回答896 阅读✓ 已解决
4 回答1.3k 阅读✓ 已解决
2 回答925 阅读✓ 已解决
2 回答1.5k 阅读✓ 已解决
2 回答1.3k 阅读✓ 已解决
2 回答897 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
问题在于您给父容器的高度。
height :100%
不占用整个高度。将其更改为100vh
或类似的这是更新的 演示