html - 为什么文本居中时不显示我的文本?
问题描述
div
文本居中时未显示我的文本。我div
分配width
给一个div
.Text 应该next
在线。
这是我的代码
body,html{
width:100%;
height:100%;
background:blue
}
.centered {
width:100%;
height:100%;
display: flex;
align-items: center;
justify-content: center;
text-align:center;
background: red; /* not necessary just to see the result clearly */
}
.abc{
background: #fff;
width:200px;
height:400px;
line-height:400px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="centered">
<div class="abc">This text is centered horizontally and vertically</div>
</div>
</body>
</html>
解决方案
导致问题的line height
原因是,当您有换行符时,下一行将在之后,400px
但您的 div 只是400px
因此您将有溢出,因为内容的高度为800px
(2 行)。改为删除line-height
并应用 div 内的中心。Line-height
对于仅居中一行文本很有用。
body,
html {
width: 100%;
height: 100%;
margin:0;
background: blue
}
.centered {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: red;/* not necessary just to see the result clearly */
}
.abc {
background: #fff;
width: 200px;
height: 300px;
display: flex;
align-items: center;
text-align:center;
}
<div class="centered">
<div class="abc">This text is centered horizontally and vertically</div>
</div>
推荐阅读
- instance - 如何在 hazelcast 实例中设置从 JCache 包装的 hazelcast 缓存
- html - 有没有办法使用css在html中隐藏没有id或class的元素?
- python - 有没有办法从一个 url 从views.py 调用两个基于函数的视图?- Django 甚至使用基于类的视图
- javascript - Rxjs - 在订阅者取消订阅后清理 observable
- asp.net - .NET Core 2.2 无法访问控制器但脚手架登录工作
- php - 无法通过 ajax 将 js 变量传递给 php
- linux - 如何使用 Unix 选项显示与 `ps aux` 相同的结果?
- android - 圆形图像视图不将图像包装为圆形
- ionic-framework - Teamcity + Ionic - 为 iOS 和 Android 构建
- blazor - Blazor 如何创建动态表单