两行样式解决垂直居中问题

2022/03/25 CSS

方法

  1. 父容器display: flex;
  2. 目标容器margin: auto;

代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>垂直居中</title>
</head>
<style>
    html,body {
        width: 100%;
        height: 100%;
    }
    body {
        background-color: aqua;
        display: flex;
    }
    div {
        width: 300px;
        height: 300px;
        background-color: red;
        margin: auto;

    }
</style>
<body>
<div>垂直居中</div>
<script>

</script>

</body>
</html>

Search

    逃离地球的Sandy

    logo of sandy

    Table of Contents