2015-02-23 90 views
1

您好在下面的代碼中如何將空間劃分爲所有的iteams列表,並且它應該是中心的。 除了家庭如何劃分3個相等部分如何在html中劃分3個相等部分li

預計產量: 網站首頁|關於|服務

#section ul { 
 
    \t width: 1050px; 
 
    \t margin: 1px auto 0 auto; 
 
    \t height: 50px; 
 
    \t padding: 0; 
 
    \t float: relative; 
 
    \t border-style: solid; 
 
     border-width: 1px; 
 
    \t background-color: #556B2F; 
 
    } 
 

 
    #section ul li { 
 
    \t position: relative; 
 
    \t list-style-type: none; 
 
     display: inline; 
 
    } 
 

 
    #section li:before { 
 
    \t content: " | "; 
 
    } 
 

 
    #section li:first-child:before { 
 
     content: none; 
 
    } 
 
    
<div id="section"> 
 
    \t <ul> 
 
      <li><a href="#">Home</a></li> 
 
      <li><a href="#">About</a></li> 
 
      <li><a href="#">Service</a></li> 
 
     </ul> 
 
    </div>

+0

這是我的代碼 – user 2015-02-23 11:57:58

+0

你只是想爲中心的鏈接或做你想讓他們寬度相等嗎?如果只是居中,那麼我會從ul中刪除'float:relative'(相對不是float的有效值),並添加'text-align:center',否則我會去Paulie的解決方案 – Pete 2015-02-23 12:00:57

回答

5

您可以使用display:table & display:table-cell舒展列表項等於寬度。

#section ul { 
 
    width: 100%; 
 
    max-width: 1050px; 
 
    margin: 1px auto 0 auto; 
 
    height: 50px; 
 
    padding: 0; 
 
    /* float:relative*/ 
 
    /* NO Such Property */ 
 
    border-style: solid; 
 
    border-width: 1px; 
 
    //background-color:#556B2F; 
 
    display: table; 
 
    text-align: center; 
 
} 
 
#section ul li { 
 
    list-style-type: none; 
 
    display: table-cell; 
 
    border-style: solid; 
 
    border-width: 1px; 
 
    vertical-align: middle; 
 
} 
 
#section ul li a { 
 
display: block; 
 
}
<div id="section"> 
 
    <ul> 
 
    <li><a href="#">Home</a> 
 
    </li> 
 
    <li><a href="#">About</a> 
 
    </li> 
 
    <li><a href="#">Service</a> 
 
    </li> 
 
    </ul> 
 
</div>

+0

非常感謝你 – user 2015-02-23 11:59:46

+0

如何使文字稍微有點下降 – user 2015-02-23 12:01:08

+0

更新爲垂直對齊。 – 2015-02-23 12:03:51

1

你在你的CSS錯誤,從relative改變floatleft

#section ul{ 
    width: 1050px; 
    margin: 1px auto 0 auto; 
    height:50px; 
    padding:0; 
    float: left; // change this, or simply remove because of `display: inline;` for `#section ul li` 
    border-style: solid; 
    border-width: 1px; 
    background-color:#556B2F; 
} 
+0

應該刪除浮點數 - 那裏不需要它 – Pete 2015-02-23 11:55:15

+0

是的位置:相對;這一個我想要 – user 2015-02-23 11:55:57

+0

是的,可以因爲'display:inline;'而被刪除''#section ul li'' – Legionar 2015-02-23 11:57:09

相關問題