css——宽度不固定,水平居中

宽度不固定,水平居中

<div id="box"> 
     <div id="inner">11111111111111111111</div>
</div>

1.相对定位:

 #box{
    
    position:relative; left:50%; float:left;}

 #inner{
    
    position:relative; left:-50%;}

需考虑浮动带来的问题

2.利用diplay:table来解决:

#box{
    
     display:table;margin:0 auto}

/* 不支持ie6,ie7*/

3.CSS3 flex-box 居中方案

#box{
    
    position: fixed; top: 0; left: 0; right: 0; bottom: 0; display: -webkit-box;  -webkit-box-align: center;-webkit-box-pack: center;}

#inner{
    
    -webkit-box-flex: 0;background: #CCC;}

猜你喜欢

转载自blog.csdn.net/ws19900201/article/details/123958585
今日推荐