05. flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中)

flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中)
(1).position :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
 
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
 
        #wrap {
            width: 500px;
            height: 500px;
            background: grey;
             
            //新版本flex方法
            display: flex;
            justify-content: center; //主轴
            align-items: center;     //侧轴
             
            //旧版本的flex版本
            display: -webkit-box;
            -webkit-box-pack: center; //主轴
            -webkit-box-align: center;//侧轴
             
            // position: relative;
        }
        #box{
            width: 200px;
            height: 200px;
            background: deeppink;
            position: absolute;
             
            //第一种垂直居中法
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
             
            //第二种垂直居中法
             
            top: 50%;
            left: 50%;
            margin-top: -100px;
            margin-left: -100px;
             
            //第三种垂直居中法
             
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
    </style>
</head>
<body>
<div id="wrap">
    <div id="box"></div>
</div>
</body>
<script type="text/javascript">
    window.onload = function () {
        var box = document.getElementById('box');
    }
</script>
</html>

  

posted @   Lolita_web  阅读(657)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示