2011-12-21 179 views
2

如何将3个浮动div对齐在页面中心对齐?中心对齐div内div?

<div style="width:100%"> 
    <div style="width:90%"> 
      <div style="width:80%"> 
       //Content 
      </div> 
    </div> 
</div> 
+0

可能重复[如何DIV中心DIV?](http://stackoverflow.com/questions/114543/how-to-center-div-in-div) – ChrisR 2011-12-21 09:57:51

+0

这里有两个简单的方法来垂直,水平或两者(纯CSS)中的中心divs:http://stackoverflow.com/a/31977476/3597276 – 2015-08-20 15:23:14

回答

12

使用margin: auto在CSS居中。

<div style="background-color: red; height: 100px; width: 500px; margin: auto;"> 
    <div style="background-color: green; height: 100px; width: 300px; margin: auto;"> 
      <div style="background-color: blue; height: 100px; width: 100px; margin: auto;"> 
       //Content 
      </div> 
    </div> 
</div> 
+0

谢谢,但我的div有100%,80%,50%等百分比的宽度。 – 2011-12-21 10:06:16

+1

只是将像素宽度更改为百分比,它仍然可以工作。我的代码中重要的是'margin:auto'。 – socha23 2011-12-21 10:10:36

+0

谢谢,我懂了。 – 2011-12-21 10:11:03

3

如果你为中心元素具有一定的宽度,你可以用margin: 0 auto