不定高度实现居中显示
<!DOCTYPE html> <html> <meta name="name" content="content" charset='utf-8'> <head> <title>网页居中</title> </head> <style type="text/css" media="screen"> *{margin:0;padding: 0;} .al{width: 800px;height: 600px;margin: 30px auto;position: relative;border:1px solid red;} .par{position: absolute;top:50%;left:50%;} .chd{background: green;position: relative;top:-50%;left:-50%;} .ul{width: 600px;height:400px;margin: 10px auto;border:1px solid blue; } .ul li{width: 50px;height: 30px;border:1px solid red;text-align: center;line-height: 30px;list-style-type: none;float:left;margin: 20px 0 0 20px;} </style> <body> <div class="al"> <div class="par"> <div class="chd"> 我要居中我要居中 </div> </div> </div> </body> </html>
第二种方法
<!DOCTYPE html> <html> <meta name="name" content="content" charset='utf-8'> <head> <title>网页居中</title> </head> <style type="text/css" media="screen"> *{margin:0;padding: 0;} .al{width: 800px;height: 600px;margin: 30px auto;} .par{width: 800px;height: 600px;border:1px solid red;display: table-cell;text-align: center;vertical-align: middle;} .chd{background: green;display: inline-block;} </style> <body> <div class="al"> <div class="par"> <div class="chd"> 我要居中我要居中 </div> </div> </div> </body> </html>