首页 > 代码库 > div元素上下左右居中

div元素上下左右居中

 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>div元素上下左右居中</title> 6 <style type="text/css"> 7 .divp{ width:300px; height:300px; background:#ccc; margin:0 auto; position:relative;} 8 .div1{ width:100px; height:100px; background:pink; margin:auto; position:absolute; top:0px; right:0px; bottom:0px; left:0px;} 9 </style>10 </head>11 <body>12 <div class="divp">13 <div class="div1"></div>14 </div>15 </body>16 </html>

 

div元素上下左右居中