html - 如何在 flexbox 中居中文本?

标签 html css flexbox center

为什么下面代码中的文字没有居中? (标题和标语)。

应该是,see image

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>

为什么上面代码中的文字没有居中? (标题和标语)

应该是,see image

最佳答案

在你的 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>

关于html - 如何在 flexbox 中居中文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396021/

相关文章:

javascript - ReactJS引导表单无需刷新页面

html - 我的下拉菜单有什么问题?

javascript - 如何实现从右到左、从下到上的文字输入动画?

javascript - 需要使用 JavaScript 创建可点击的自动幻灯片

php - Yii2:如何将某些东西放入面包屑小部件中?

html - 引导 CDN 不工作

ios - alignItems 无法正常工作。 React Native flexbox Yaga

html - 如何在不给出屏幕高度的情况下垂直对齐 div?

html - 如何摆脱 Chromium Flexbox 中不需要的空间?

javascript - 更改 tr 的多个类 (HTML/JQUERY)