We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用弹性盒子布局,只需要给待处理的块状元素的父元素添加属性:
/* 弹性盒子布局 */ display: flex; /* 水平居中 */ justify-content: center; /* 垂直居中 */ align-items: center;
该方案在实际开发中应用颇多,也比较简单直观,不需要确定子盒子宽高就可以实现。
实例代码,复制即可查看效果:
<!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> .box { height: 300px; width: 300px; background-color: orangered; /* 弹性盒子布局 */ display: flex; /* 水平居中 */ justify-content: center; /* 垂直居中 */ align-items: center; } .test-div { height: 100px; width: 100px; background-color: blue; } </style> </head> <body> <section class="box"> <div class="test-div"></div> </section> </body> </html>
效果:
position: relative
position: absolute
top: 50%; left: 50%;
margin-left
margin-top
margin-left: -50px; margin-top: -50px;
height: 100px; width: 100px;
<!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> .box { height: 300px; width: 300px; background-color: rgb(201, 211, 58); /* 父盒子 相对定位 */ position: relative; } .test-div { height: 100px; width: 100px; background-color: rgb(211, 18, 98); /* 子盒子 绝对定位 */ position: absolute; top: 50%; left: 50%; margin-left: -50px; margin-top: -50px; } </style> </head> <body> <section class="box"> <div class="test-div"></div> </section> </body> </html>
只需将上面例子中的:子元素设置margin-left和margin-top为自身宽高一半的负值 替换为:transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
即:
这个方法比较特殊,虽说只改了一行代码,但是原理是什么?只是背一行代码就会显得十分草率。
所以再放实例代码之前,我们先来看看这个方法如何实现了水平垂直居中。
transform: translate(x,y);的语法定义了元素的2D转换,也就是元素移动(平移),讲完这个我们再来看,子元素设置绝对定位,top和left各移动50%后,子元素左上角正好在父盒子的中心,此时还没有实现子元素的水平垂直居中(即子元素的中心点在父盒子的中心,而不是左上角在父盒子中心)。接着往下看:
transform: translate(x,y);
translate(x,y) 括号里填百分比数据的话,会以本身的长宽做参考,注意,不是以父盒子宽高为参考,这就是该方法最需要理解的一个点。比如,本身的宽为100px,高为100px. 那填translate(50%,50%)就是向右和向下各移动50px,添加负号就是向着相反的方向移动。移动后的效果就是子元素的中心点在父元素的中心,实现了水平垂直居中。
translate(x,y)
translate(50%,50%)
<!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> .box { height: 300px; width: 300px; background-color: rgb(231, 69, 20); /* 父盒子 相对定位 */ position: relative; } .test-div { height: 100px; width: 100px; background-color: rgb(67, 33, 218); /* 子盒子 绝对定位 */ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <section class="box"> <div class="test-div"></div> </section> </body> </html>
效果: http://www.imooc.com/qadetail/129282
top: 0; bottom: 0; left: 0; right: 0;
margin: auto;
<!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> .box { height: 300px; width: 300px; background-color: rgb(74, 199, 43); /* 父盒子 相对定位 */ position: relative; } .test-div { height: 100px; width: 100px; background-color: rgb(20, 38, 117); /* 子盒子 绝对定位 */ position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; } </style> </head> <body> <section class="box"> <div class="test-div"></div> </section> </body> </html>
The text was updated successfully, but these errors were encountered:
yuanyuanbyte
No branches or pull requests
1. flex
使用弹性盒子布局,只需要给待处理的块状元素的父元素添加属性:
该方案在实际开发中应用颇多,也比较简单直观,不需要确定子盒子宽高就可以实现。
实例代码,复制即可查看效果:
效果:
![在这里插入图片描述](https://camo.githubusercontent.com/4eac156ea4766f7bf5ce05f95e73e31ea2b91bb5047685ed7c0a7ff9ad6ced35/68747470733a2f2f696d672d626c6f672e6373646e696d672e636e2f32303231303430323139303833353834332e706e673f782d6f73732d70726f636573733d696d6167652f77617465726d61726b2c747970655f5a6d46755a33706f5a57356e6147567064476b2c736861646f775f31302c746578745f6148523063484d364c7939696247396e4c6d4e7a5a473475626d56304c33646c61586870626c38304e5467304e4441304f513d3d2c73697a655f31362c636f6c6f725f4646464646462c745f3730)
2. position (需要确定子元素宽高)
position: relative
;子元素设置为绝对定位:position: absolute
top: 50%; left: 50%;
margin-left
和margin-top
为自身宽高一半的负值:margin-left: -50px; margin-top: -50px;
(自身宽高为height: 100px; width: 100px;
)实例代码,复制即可查看效果:
效果:
![在这里插入图片描述](https://camo.githubusercontent.com/b05ad2204aec032bd7d36262f099da867ce26fc6bcd5bb66a6476522e85639f4/68747470733a2f2f696d672d626c6f672e6373646e696d672e636e2f32303231303431333135343231383337322e706e673f782d6f73732d70726f636573733d696d6167652f77617465726d61726b2c747970655f5a6d46755a33706f5a57356e6147567064476b2c736861646f775f31302c746578745f6148523063484d364c7939696247396e4c6d4e7a5a473475626d56304c33646c61586870626c38304e5467304e4441304f513d3d2c73697a655f31362c636f6c6f725f4646464646462c745f3730)
3. position + transform 元素平移
只需将上面例子中的:子元素设置
margin-left
和margin-top
为自身宽高一半的负值替换为:
transform: translate(-50%,-50%);
即:
position: relative
;子元素设置为绝对定位:position: absolute
top: 50%; left: 50%;
transform: translate(-50%,-50%);
这个方法比较特殊,虽说只改了一行代码,但是原理是什么?只是背一行代码就会显得十分草率。
所以再放实例代码之前,我们先来看看这个方法如何实现了水平垂直居中。
transform: translate(x,y);
的语法定义了元素的2D转换,也就是元素移动(平移),讲完这个我们再来看,子元素设置绝对定位,top和left各移动50%后,子元素左上角正好在父盒子的中心,此时还没有实现子元素的水平垂直居中(即子元素的中心点在父盒子的中心,而不是左上角在父盒子中心)。接着往下看:translate(x,y)
括号里填百分比数据的话,会以本身的长宽做参考,注意,不是以父盒子宽高为参考,这就是该方法最需要理解的一个点。比如,本身的宽为100px,高为100px. 那填translate(50%,50%)
就是向右和向下各移动50px,添加负号就是向着相反的方向移动。移动后的效果就是子元素的中心点在父元素的中心,实现了水平垂直居中。实例代码,复制即可查看效果:
效果:
![在这里插入图片描述](https://camo.githubusercontent.com/407f206fcba1c3e2969ac8f02036802a0949eb74f515adf47288087d27b2c744/68747470733a2f2f696d672d626c6f672e6373646e696d672e636e2f32303231303431333136343034363931352e706e673f782d6f73732d70726f636573733d696d6167652f77617465726d61726b2c747970655f5a6d46755a33706f5a57356e6147567064476b2c736861646f775f31302c746578745f6148523063484d364c7939696247396e4c6d4e7a5a473475626d56304c33646c61586870626c38304e5467304e4441304f513d3d2c73697a655f31362c636f6c6f725f4646464646462c745f3730)
http://www.imooc.com/qadetail/129282
4. position + margin:auto
position: relative
;子元素设置为绝对定位:position: absolute
top: 0; bottom: 0; left: 0; right: 0;
margin: auto;
实例代码,复制即可查看效果:
效果:
![在这里插入图片描述](https://camo.githubusercontent.com/87101eb7722e47dcef7bd18c960b2d7f0c3c9a48c0b4f67c3b074cf9fc2fc392/68747470733a2f2f696d672d626c6f672e6373646e696d672e636e2f323032313034313331363032343239352e706e673f782d6f73732d70726f636573733d696d6167652f77617465726d61726b2c747970655f5a6d46755a33706f5a57356e6147567064476b2c736861646f775f31302c746578745f6148523063484d364c7939696247396e4c6d4e7a5a473475626d56304c33646c61586870626c38304e5467304e4441304f513d3d2c73697a655f31362c636f6c6f725f4646464646462c745f3730)
参考
The text was updated successfully, but these errors were encountered: