如何水平和垂直对齐“块”中的 div 元素?

时间:2021-05-08 03:50:43

标签: html css reactjs alignment vertical-alignment

我在一个 div 中有 3 个元素,我希望它们在一个块中(垂直)并水平和垂直对齐。这是它现在的样子:

shouldChangeTextIn:

这是我的代码:

<div className="parent">
        <p className="heading">TITLE</p>
        <p className="description"><strong>DISCLAIMER</strong>: The site contains offensive, vulgar langauge which may not be<br/>suitable for many, so enter with caution and do not reveal any personal details</p>
        <Link to="/school"><button className="enterButton">enter the website</button></Link>
    </div>

----------------------
stylesheet.css
----------------------
.parent {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .heading {
    font-size: 76px;
    text-align: center;
  }

  .description {
    font-size: 18px;
    opacity: .7;
    text-align: center;
    line-height: 26px;
  }

  .enterButton {
      width: 360px;
      height: 100px;
      background-color: #E1E8ED;
      color: #15202B;
      font-size: 32px;
      font-weight: bolder;
      text-align: center;
      border: none
  }

我希望它们对齐并在彼此下方,例如:

TITLE
Disclaimer
Enter the website

1 个答案:

答案 0 :(得分:1)

Flex-direction 列用于设置元素在 flex 父元素内的对齐方式。您已经使用 align-items: centerjustify-content: center 对两个轴进行了中心对齐。

.parent {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

会做的伎俩...