css实现垂直水平居中

1.通过flex布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css垂直水平居中</title>
    <style>
       html, body {
        margin: 0;
        padding: 0;
        }
      .main {
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .content {
          width: 300px;
          height: 300px;
          background: rgb(10, 75, 136);
      }
    </style>
</head>
<body>
  <div class="main">
      <div class="content"></div>
  </div>
</body>
</html>

2.transferm+margin

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css垂直水平居中</title>
    <style>
      html, body {
        margin: 0;
        padding: 0;
        height: 100%;
      }
      .content {
        width: 300px;
        height: 300px;
        background: rgb(10, 75, 136);
        margin: 0 auto;
        position: relative;
        top: 50%;
        transform: translateY(-50%);
      }
    </style>
</head>
<body>
  <div class="content"></div>
</body>
</html>

相关推荐