css:水平居中 垂直居中

居中是样式中常用的操作,这里简单做一下总结:

下面的是基础文件样式:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .parent {
      width: 200px;
      height: 100px;
      position: relative;
      background-color: red;
    }

    .parent .child {
      width: 100px;
      height: 50px;
      background-color: yellow;
    }

  </style>
</head>

<body>
  <div class="parent">
    <div class="child"></div>
  </div>

</body>

</html>

一、水平居中

二、垂直居中

三、水平和垂直居中

猜你喜欢

转载自www.cnblogs.com/cyycyhcbw/p/12896324.html