2014-10-02 94 views
0

我想知道如何centrilize一个div,未知width只能使用CSS两个div只使用CSS的集中div

我希望是这样的:

enter image description here

使用<center>我有:

<div style="float: left"> 
    <p:commandButton .../> 
</div> 
<center> 
    <p:commandButton ..."/> 
    <p:commandButton .../> 
</center> 
<div style="float: right"> 
    <p:commandButton .../> 
</div> 

如何只使用css做?

注:我不使用margin: 0 auto只是因为集中div的大小是未知的。

回答

0

这似乎到这里问了很多,看这里How to horizontally center a <div> in another <div>?

从本质上讲,你是想中心内部元件需要在线的显示类型:块;和包装这个元素的容器,你想在它的中心设置text-align属性:center;

1

1)上的父DIV设置text-align:center(即,已提到的元素的父)

2)用div替换center元件并设置规则display:inline-block就可以了。

FIDDLE

.parent { 
 
    text-align: center; 
 
} 
 
.middleBtns { 
 
    display: inline-block; 
 
}
<div class="parent"> 
 
    <div style="float: left"> 
 
    <button type="button"> 
 
     < </button> 
 
    </div> 
 
    <div class="middleBtns"> 
 
    <button type="button">btn1</button> 
 
    <button type="button">btn2</button> 
 
    </div> 
 
    <div style="float: right"> 
 
    <button type="button">></button> 
 
    </div> 
 
</div>