2015-02-23 37 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