css居中对齐

转  css居中对齐

需求:练习要求:

 

 

 方法1:

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #allcontent {
            width: 100%;
            height: 500px;
            border: 1px solid blue;
        }
        
        #first {
            height: 30px;
            background-color: red;
        }
        
        #second {
            /* position: relative; */
            height: 400px;
            display: flex;
        }
        
        #left {
            width: 100px;
            height: 400px;
            background-color: rosybrown;
        }
        
        #right {
            /* 子绝父相 */
            position: relative;
            /* width: 400px; */
            width: 100%;
            height: 400px;
            background-color: purple;
        }
        
        #right,
        #left,
        #circle {
            float: left;
        }
        
        #circle {
            position: absolute;
            /* 方法1:因为绝对定位后盒子是脱标状态,所以在position:absolute之后,设置的top bottom left right等为50%,然后再使用margin-():(xxx)px减去盒子一半的数字即可 */
            top: 50%;
            bottom: 50%;
            left: 50%;
            right: 50%;
            margin-top: -50px;
            margin-left: -50px;
            /* transform: translate(50%, 50%); */
            /* translate定位,定位位置之后  
            transform:translate(X,Y);
            transform:translateX();
            transform:translateY(); */
            /* 方法2:transform这边把圆形的圆心位置移动到原点。然后再布局。 */
            /* transform: translate(-50px, -50px);
            top: 50%;
            left: 50%; */
            /* top: 52px; */
            /* top: 150px; */
            /* left: 150px; */
            /* text-align: center;
            transform: translateX(50%); */
            width: 100px;
            height: 100px;
            border: 1px solid blue;
            border-radius: 50%;
            background-color: lightseagreen;
        }
        
        #third {
            height: 70px;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div id="allcontent">
        <div id="first"></div>
        <div id="second">
            <div id="left"></div>
            <div id="right">
                <div id="circle"></div>
            </div>
            <!-- <div id="circle"></div> -->
        </div>
        <div id="third"></div>
    </div>
</body>

</html>
复制代码

方法2:

 

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #allcontent {
            width: 100%;
            height: 500px;
            border: 1px solid blue;
        }
        
        #first {
            height: 30px;
            background-color: red;
        }
        
        #second {
            /* position: relative; */
            height: 400px;
            display: flex;
        }
        
        #left {
            width: 100px;
            height: 400px;
            background-color: rosybrown;
        }
        
        #right {
            /* 子绝父相 */
            position: relative;
            /* width: 400px; */
            width: 100%;
            height: 400px;
            background-color: purple;
        }
        
        #right,
        #left,
        #circle {
            float: left;
        }
        
        #circle {
            position: absolute;
            /* 方法1:因为绝对定位后盒子是脱标状态,所以在position:absolute之后,设置的top bottom left right等为50%,然后再使用margin-():(xxx)px减去盒子一半的数字即可 */
            /* top: 50%;
            bottom: 50%;
            left: 50%;
            right: 50%;
            margin-top: -50px;
            margin-left: -50px; */
            /* transform: translate(50%, 50%); */
            /* translate定位,定位位置之后  
            transform:translate(X,Y);
            transform:translateX();
            transform:translateY(); */
            /* 方法2:transform这边把圆形的圆心位置移动到原点。然后再布局。 */
            transform: translate(-50px, -50px);
            top: 50%;
            left: 50%;
            /* top: 52px; */
            /* top: 150px; */
            /* left: 150px; */
            /* text-align: center;
            transform: translateX(50%); */
            width: 100px;
            height: 100px;
            border: 1px solid blue;
            border-radius: 50%;
            background-color: lightseagreen;
        }
        
        #third {
            height: 70px;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div id="allcontent">
        <div id="first"></div>
        <div id="second">
            <div id="left"></div>
            <div id="right">
                <div id="circle"></div>
            </div>
            <!-- <div id="circle"></div> -->
        </div>
        <div id="third"></div>
    </div>
</body>

</html>
复制代码

 

复制代码
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 7     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 8     <title>Document</title>
 9     <style>
10         #allcontent {
11             width: 100%;
12             height: 500px;
13             border: 1px solid blue;
14         }
15         
16         #first {
17             height: 30px;
18             background-color: red;
19         }
20         
21         #second {
22             /* position: relative; */
23             height: 400px;
24             display: flex;
25         }
26         
27         #left {
28             width: 100px;
29             height: 400px;
30             background-color: rosybrown;
31         }
32         
33         #right {
34             /* 子绝父相 */
35             position: relative;
36             /* width: 400px; */
37             width: 100%;
38             height: 400px;
39             background-color: purple;
40         }
41         
42         #right,
43         #left,
44         #circle {
45             float: left;
46         }
47         
48         #circle {
49             position: absolute;
50             /* 方法1:因为绝对定位后盒子是脱标状态,所以在position:absolute之后,设置的top bottom left right等为50%,然后再使用margin-():(xxx)px减去盒子一半的数字即可 */
51             /* top: 50%;
52             bottom: 50%;
53             left: 50%;
54             right: 50%;
55             margin-top: -50px;
56             margin-left: -50px; */
57             /* transform: translate(50%, 50%); */
58             /* translate定位,定位位置之后  
59             transform:translate(X,Y);
60             transform:translateX();
61             transform:translateY(); */
62             /* 方法2:transform这边把圆形的圆心位置移动到原点。然后再布局。 */
63             transform: translate(-50px, -50px);
64             top: 50%;
65             left: 50%;
66             /* top: 52px; */
67             /* top: 150px; */
68             /* left: 150px; */
69             /* text-align: center;
70             transform: translateX(50%); */
71             width: 100px;
72             height: 100px;
73             border: 1px solid blue;
74             border-radius: 50%;
75             background-color: lightseagreen;
76         }
77         
78         #third {
79             height: 70px;
80             background-color: orange;
81         }
82     </style>
83 </head>
84 
85 <body>
86     <div id="allcontent">
87         <div id="first"></div>
88         <div id="second">
89             <div id="left"></div>
90             <div id="right">
91                 <div id="circle"></div>
92             </div>
93             <!-- <div id="circle"></div> -->
94         </div>
95         <div id="third"></div>
96     </div>
97 </body>
98 
99 </html>
复制代码

 

 

 

 



 





 

居中:

因为绝对定位后盒子是脱标状态,所以在position:absolute之后,设置的top bottom left right等为50%,然后再使用margin-():(xxx)px减去盒子一半的数字即可

 

位移居中:

transform:translate(-50%,-50%),就不用像上面还要手动计算减去margin-()盒子的一半,这个命令会自己计算减去的值达到居中的效果

 
标签: css进阶

posted on   xiaoluoke  阅读(48)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
历史上的今天:
2020-12-01 HTMLCSS第7课笔记PS切图
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示