html - 中心文本仅适用于简短的展开行?
问题描述
有没有办法让文本居中 ( text-align: center
) 仅适用于短到不能换行的文本行,否则左对齐 ( text-align: left
)?
解决方案
您可以使用 flexbox 和justify-content: center
.
div {
background-color: #e7e7e7;
width: 200px;
}
p {
display: flex;
justify-content: center;
}
<div>
<p>Short text</p>
</div>
<div>
<p>Now this is more like a long text</p>
</div>
推荐阅读
- java - 调用 stop() 时 Jcoserver 状态更改为 STOPPING(不是 STOPPED)
- node.js - api 响应包含转义字符
- vb.net - 针对性别的 LINQ 查询过滤
- javascript - C# UWP 中的发射格式
- c++ - 用于取消标志的 std::atomic_bool:std::memory_order_relaxed 是正确的内存顺序吗?
- angular - IE 浏览器和 Firefox Angular 5 材料输入延迟问题(通过创建一些记录使用该应用程序几分钟后延迟。)
- python - 平滑具有多条曲线的函数
- d3.js - 将图例添加到 dc.js 散点图
- c# - 获取C#中组合框的文本后,如何在单词中间添加特殊字符?
- sql - 将 Where 子句存储在表中以获取状态