其他分享
首页 > 其他分享> > CSS样式-position为absolute的情况下如何居中

CSS样式-position为absolute的情况下如何居中

作者:互联网

<!DOCTYPE html> <html lang="en"> <head>     <title>Document</title>     <style type="text/css">         div {             width: 300px;             height: 300px;             background-color: #f00;             position: absolute;             left: 50%;             /* 用width/2 */             margin-left: -150px;

 

            top: 50%;             /* 用height/2 */             margin-top: -150px;                          /* 当position为absolute时,用下面auto这种方式进行居中,不再起作用 */             /* margin: 0 auto; */         }     </style> </head> <body>     <div></div> </body> </html>

标签:top,height,position,absolute,margin,CSS,150px
来源: https://www.cnblogs.com/Friday1/p/14532860.html