赞
踩
让元素水平居中有很多方法,比如设置 text-align:center;比如设置margin:auto;再比如使用弹性布局,display:flex,justify-content:center。但是让元素垂直居中,或者是水平垂直居中却不是那么容易,今天就给大家分享一下四种让元素水平垂直居中的方法,实现如下效果。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>React App</title> </head> <style> body{ margin: 0; } #father{ display: flex; justify-content: center; align-items: center; background: rgba(0,0,0,0.7); height:100vh; } #son{ width: 80%; height: 60%; background: white; border-radius: 30px; } </style> <body> <div id="father"><div id="son"></div></div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>React App</title> </head> <style> body{ margin:0 } #father{ background: rgba(0,0,0,0.7); height:100vh; } #son{ width: 80%; height: 60%; background: white; border-radius: 30px; transform: translate(12.5%,33%); } </style> <body> <div id="father"><div id="son"></div></div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>React App</title> </head> <style> body{ margin:0 } #father{ background: rgba(0,0,0,0.7); position:fixed; left:0; right:0; top:0; bottom:0; } #son{ width: 90%; height: 60%; background: white; border-radius: 30px; margin:auto; positon:fixed; left:0; right:0; top:0; bottom:0; } </style> <body> <div id="father"><div id="son"></div></div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>React App</title> </head> <style> body{ margin:0 } #father{ background: rgba(0,0,0,0.7); position:fixed; left:0; right:0; top:0; bottom:0; } #son{ width: 90%; height: 60%; background: white; border-radius: 30px; position:fixed; left:50%; top:50%; transform: translate(-50%,-50%); } </style> <body> <div id="father"><div id="son"></div></div> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。