Mopecat / Daily-Mission-Board

每日任务公告板。
4 stars 1 forks source link

任务一:写出所有垂直居中布局的方式。 #1

Open Mopecat opened 4 years ago

Mopecat commented 4 years ago

来吧 开始吧 我的小伙伴们

LB-nan commented 4 years ago
  1. 绝对定位 + margin负值
  2. 绝对定位 + transform:translate
  3. position: absolute; top: 0;bottom: 0;margin: auto;
  4. 使用padding
  5. flex布局
  6. display: table; + display: table-cell;vertical-align: middle;
Mopecat commented 4 years ago

写了一个小练习做的相关实践,代码 主要可以分成几种情况:

HHardyy commented 4 years ago

grid

Mopecat commented 4 years ago

grid

回头我试试,这玩意我还没用过呢

zengkaiz commented 4 years ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TEST</title>
    <style>
       .wrap {
           width: 500px;
           height: 500px;
           background: green;
           /* NO.1 */
           /* display: flex;
           align-items: center;
           justify-content: center; */
           /* NO.2 */
           /* padding:150px;
           box-sizing: border-box; */
           /* No.3 */
           position: relative;
       }
       .inner {
            width:200px;
            height: 200px;
           background: red;
           /* No.3 */
           /* position: absolute;
           top: 50%;
           left:50%;
           margin-top: -100px;
           margin-left: -100px; */
           /* No.4 */
           /* position: absolute;
           top: 50%;
           left:50%;
           transform: translate(-50%, -50%); */
           /* NO.5 */
           position: absolute;
           top:0;
           left:0;
           right: 0;
           bottom:0;
           margin: auto;
       }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="inner"></div>
    </div>
</body>
<script src="./2.index.js"></script>
</html>