如何设置项目垂直居中

时间:2019-02-09 03:44:45

标签: html css

我是Web开发的初学者,我正尝试在p元素中心下方垂直对齐,但是它不能正常工作,请有人帮助我

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
    <style>
    p{
    top: 50%;
    transform: translateY(-50%);
     }
    </style>
</head>
<body>

<header class="d-flex flex-column align-content-center bg-secondary py-5">
  <div class="d-flex">
    <img src="img/Icons-14.png">
    <p>hgjsafakfjlajflksjf</p>
  </div>
</header>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试一下:

    d-flex {
        position:relative;
    }
    p {
        position: absolute
        top: 50%;
        transform: translateY(-50%);
     }

或尝试这种方式:

d-flex {
    display:flex;
    align-items:center;
}