简体   繁体   English

如何在flexbox中居中放置文字?

[英]How do I center text in flexbox?

Why the texts are not at the center in the below code? 为什么以下代码中的文本不在中心? (Title & Tag Line). (标题和标签行)。

It should be like, 应该是这样 看图片

 body { margin: 0; padding: 0; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; justify-content: center; align-items: center; height: 100vh; background: #000; } p { position: relative; font-family: sans-serif; text-transform: uppercase; font-size: 2em; letter-spacing: 4px; overflow: hidden; background: linear-gradient(90deg, #000, #000fe6, #000); background-repeat: no-repeat; background-size: 80%; animation: animate 3.75s linear infinite; -webkit-background-clip: text; -webkit-text-fill-color: rgba(45, 45, 45, .05); } @keyframes animate { 0% { background-position: -500%; } 100% { background-position: 500%; } } 
 <body> <div> <p>Title</p> <p>Tag Line</p> </div> </body> 

Why the texts are not at the center in the above code? 为什么文本不在以上代码的中心? (Title & Tag Line) (标题和标语线)

It should be like, 应该是这样 看图片

use this in your p 在你的p使用它

 margin:0 auto;
  display:block;
  text-align:center;

 body { margin: 0; padding: 0; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; justify-content: center; align-items: center; height: 100vh; background: #000; } p { position: relative; font-family: sans-serif; text-transform: uppercase; margin:0 auto; display:block; text-align:center; font-size: 2em; letter-spacing: 4px; overflow: hidden; background: linear-gradient(90deg, #000, #000fe6, #000); background-repeat: no-repeat; background-size: 80%; animation: animate 3.75s linear infinite; -webkit-background-clip: text; -webkit-text-fill-color: rgba(45, 45, 45, .05); } @keyframes animate { 0% { background-position: -500%; } 100% { background-position: 500%; } } 
 <body> <div> <p>Title</p> <p>Tag Line</p> </div> </body> 

You have to align the p's content too. 您还必须对齐p的内容。 You can do it with text-align: center or with display:flex + justify-content:center 您可以使用text-align:centerdisplay:flex + justify-content:center来实现

 body { margin: 0; padding: 0; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; justify-content: center; align-items: center; height: 100vh; background: #000; } p { position: relative; font-family: sans-serif; text-transform: uppercase; font-size: 2em; letter-spacing: 4px; overflow: hidden; background: linear-gradient(90deg, #000, #000fe6, #000); background-repeat: no-repeat; background-size: 80%; animation: animate 3.75s linear infinite; -webkit-background-clip: text; -webkit-text-fill-color: rgba(45, 45, 45, .05); text-align: center; } @keyframes animate { 0% { background-position: -500%; } 100% { background-position: 500%; } } 
 <body> <div> <p>Title</p> <p>Tag Line</p> </div> </body> 

Add css 添加CSS

text-align:center

in body or p tag bodyp标签中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM