【问题标题】:Horizontal navigation make current li taller水平导航使当前li更高
【发布时间】:2013-12-26 23:19:40
【问题描述】:

我正在研究水平导航,其中列表项需要跨越整个宽度并且“活动”列表项需要高于其余项。

通过将 ul 设置为 display: table 和 list items to display: table-cell 成功地使列表项跨越整个宽度。

#l-primary-nav ul {
    width: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    display: table;
}

#l-primary-nav li {
    display: table-cell;
    text-align: center;
    line-height: 40px;
}

我不知道如何使“活动”列表项比其他项高一点。以为我会为 #l-primary-nav li(上)设置一个 margin-top: 5px 并从活动 li(下)中删除 margin-top,但是表格单元格项目不接受边距。

#l-primary-nav li.active {
    background: #fff;
    border-left: 1px solid #c2c2c2;
    border-right: 1px solid #c2c2c2;
}

到目前为止,这里是导航栏的链接:FIDDLE

我希望“关于”导航项比其他导航项高约 5 个像素。希望使用仅 CSS 的解决方案。任何见解将不胜感激。

谢谢!

【问题讨论】:

标签: html css


【解决方案1】:

这可能对你有帮助

 #l-primary-nav li {
     display:block;
     width:140px;
     float:left;
     text-align: center;
     margin-top:4px;
     border: 1px solid #c2c2c2;
 }

我已经为 li 元素提供了 display:block 并添加了宽度:

演示:http://jsfiddle.net/ggbhat/af82E/1/

【讨论】:

  • 此解决方案还会自动删除跨越整个宽度的列表项。我需要避免给列表项设置宽度。
【解决方案2】:

请参考以下fiddler:Here

HTML

<div id="l-primary-nav">
<ul class="l-inline">
    <li><a href="#">Home</a>
    </li>
    <li class="active"><a href="#">About</a>
    </li>
    <li><a href="#">Articles</a>
    </li>
    <li><a href="#">Calendar</a>
    </li>
</ul>

CSS

l-primary-nav {
width: 600px;
margin: 0 auto; 
}
.l-inline li {
display: inline-block;
}
l-primary-nav ul {
width: 99.8%;
margin: 0;
padding: 0;
border: 1px solid #c2c2c2;
float: left;
display: table;
}
l-primary-nav li {
text-align: center;
display: table-cell;
float: left;
}
l-primary-nav li a{
line-height: 40px;
height: 41px;
padding: 0 30px;
border-top: 1px transparent;
display: block;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.18, #FBFBFB), color-stop(1, #D1D1D1));
background-image: -o-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: -moz-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: -webkit-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: -ms-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: linear-gradient(to bottom, #FBFBFB 0%, #D1D1D1 100%);
}
l-primary-nav li a:hover {
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #E1E3E6), color-stop(1, #BABCBF));
background-image: -o-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: -moz-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: -webkit-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: -ms-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: linear-gradient(to bottom, #E1E3E6 0%, #BABCBF 100%);
}

l-primary-nav li.active a{
margin-top: -5px;
height: 45px;
position: relative;
background: #fff;
border-left: 1px solid #c2c2c2;
border-right: 1px solid #c2c2c2;
border-top: 1px solid #c2c2c2;
}

【讨论】:

  • 此解决方案不允许列表项自动跨越整个宽度。看起来像将 float:left 添加到 #l-primary-nav li 会占用整个跨度。有没有办法允许全宽跨度并具有不同的高度?
  • 以您的解决方案为基础,我能够保持列表项扩展到全宽,同时为活动列表项提供更高的高度。这是最终代码的链接:jsfiddle.net/melcsweeney/xc5Y2
【解决方案3】:

根据上述建议的解决方案,我能够保持列表项跨越整个宽度,同时为活动列表项提供更高的高度。代码链接:http://jsfiddle.net/melcsweeney/xc5Y2/

#l-primary-nav {
width: 600px;
margin: 0 auto;
}
.l-inline li {
display: inline-block;
}
#l-primary-nav ul {
width: 99.8%;
margin: 0;
padding: 0;
border-left: 1px solid #c2c2c2;
border-right: 1px solid #c2c2c2;
border-bottom: 1px solid #c2c2c2;
float: left;
display: table;
}
#l-primary-nav li {
text-align: center;
display: table-cell;
vertical-align: bottom;
border-top: 1px solid #c2c2c2;
}

#l-primary-nav li a{
line-height: 40px;
height: 41px;
border-top: 1px transparent;
display: block;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.18, #FBFBFB), color-stop(1, #D1D1D1));
background-image: -o-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: -moz-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: -webkit-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: -ms-linear-gradient(bottom, #FBFBFB 0%, #D1D1D1 100%);
background-image: linear-gradient(to bottom, #FBFBFB 0%, #D1D1D1 100%);
}
#l-primary-nav li a:hover {
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #E1E3E6), color-stop(1, #BABCBF));
background-image: -o-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: -moz-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: -webkit-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: -ms-linear-gradient(bottom, #E1E3E6 0%, #BABCBF 100%);
background-image: linear-gradient(to bottom, #E1E3E6 0%, #BABCBF 100%);
}

#l-primary-nav li.active a{
margin-top: -5px;
height: 45px;
position: relative;
background: #fff;
border-left: 1px solid #c2c2c2;
border-right: 1px solid #c2c2c2;
border-top: 1px solid #c2c2c2;
}

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2019-12-12
    • 2011-08-24
    • 2013-04-23
    • 2023-03-30
    相关资源
    最近更新 更多