Как центрировать текст во flexbox? - PullRequest
0 голосов
/ 28 марта 2019

Почему тексты не в центре в приведенном ниже коде?(Название и метка).

Должно быть, 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

Ответы [ 3 ]

3 голосов
/ 28 марта 2019

используйте это в вашем 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>
1 голос
/ 28 марта 2019

Добавить css

text-align:center

в body или p тег

1 голос
/ 28 марта 2019

Вы должны выровнять содержимое р тоже. Вы можете сделать это с выравниванием текста: по центру или с отображением: 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>
...