.wrap{position:relative; left:50%; }
.wrap-center
{
background:#ccc; position:relative; left:-50%;
}
结果是这样的
跟最初一样,没动
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>不定宽块状元素水平居中</title> <style> .container{ float:left; position:relative; left:50% } .container ul{ list-style:none; margin:0; padding:0; position:relative; left:-50%; } .container li{float:left;display:inline;margin-right:8px;} /*下面是代码任务区*/ .wrap{position:relative;left:50%;} .wrap-center{ background:#ccc;position:relative;left:-50%; } </style> </head> <body> <div class="container"> <ul> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> </ul> </div> <!--下面是代码任务区--> <div class="wrap"> <div class="wrap-center">我们来学习一下这种方法。</div> </div> </body> </html>
只看任务区域的.
刚毅87