简体   繁体   English

如何创建响应式标签式导航栏?

[英]How to create a responsive tabbed style navigation bar?

In the example code, notice the .current class has a grey gap at the bottom. 在示例代码中,请注意.current类的底部有一个灰色间隙。 My intent is to make that white all the way to the bottom, but any attempt to increase the size of the li just also expands the ul. 我的目的是将白色调到最底,但是任何增加li尺寸的尝试都只会扩大ul。

Overflow hidden is a problem for me because I still want the offset on the top of the li. 隐藏的溢出对我来说是个问题,因为我仍然希望li的顶部具有偏移量。

How can I give the effect of the .current li background color of white extending to the bottom of the ul element? 如何赋予白色的.current li背景色延伸到ul元素底部的效果?

http://codepen.io/anon/pen/rVrvZv http://codepen.io/anon/pen/rVrvZv

 .site-nav { display: block; } .site-nav ul { background: linear-gradient(0deg, #d0d0d0, #fefefe); border-top: #bdbdbd solid 1px; border-bottom: #bdbdbd solid 1px; } .site-nav ul li { display: inline-block; width: 16.366666%; height: 2.5rem; text-align: center; line-height: 2.5rem; background: linear-gradient(0deg, #d0d0d0, #fefefe); } .site-nav ul li:hover { background: linear-gradient(0deg, #babcbf, #e1e3e6); } .site-nav ul li.current { position: relative; background: #fff; width: 16%; border-top: #bdbdbd solid 1px; border-left: #bdbdbd solid 1px; border-right: #bdbdbd solid 1px; top: -5px; } .site-nav a { font-family: Helvetica, sans-serif; color: #616264; text-transform: uppercase; letter-spacing: .08rem; text-shadow: rgb(224, 224, 224) 1px 1px 0px; filter: progid:DXImageTransform.Microsoft.Shadow(OffX=1, OffY=1, color=#e0e0e0); font-size: .75rem; } 
 <div class="site-nav"> <ul> <li><a href="#">one</a></li> <li><a href="#">two</a></li> <li><a href="#">three</a></li> <li class="current"><a href="#">four</a></li> <li><a href="#">five</a></li> <li><a href="#">six</a></li> </ul> </div> 

You could set the current highlight style on the <a> element rather than the <li> , I also suggest to use a pseudo element :before to create the extra space at the top. 您可以在<a>元素而不是<li>上设置当前突出显示样式,我还建议使用伪元素:before在顶部创建额外的空间。

JSFIDDLE DEMO JSFIDDLE演示

 .site-nav ul { padding: 0; margin: 0; font-size: 0; /*remove inline block gap*/ } .site-nav ul li { background: linear-gradient(#fefefe, #d0d0d0); border-top: #bdbdbd solid 1px; border-bottom: #bdbdbd solid 1px; display: inline-block; width: 16.66%; height: 2.5rem; line-height: 2.5rem; text-align: center; } .site-nav ul li a { display: block; font-family: Helvetica, sans-serif; font-size: .75rem; /*reset font size*/ text-transform: uppercase; letter-spacing: .08rem; text-decoration: none; color: #616264; text-shadow: rgb(224, 224, 224) 1px 1px 0px; } .site-nav ul li a:hover { background: linear-gradient(#e1e3e6, #babcbf); } .site-nav ul li.current a { background: #fff; border: #bdbdbd solid 1px; border-width: 0 1px; } .site-nav ul li.current a:before { content: ""; height: 4px; display: block; } 
 <div class="site-nav"> <ul> <li><a href="#">one</a></li> <li><a href="#">two</a></li> <li><a href="#">three</a></li> <li class="current"><a href="#">four</a></li> <li><a href="#">five</a></li> <li><a href="#">six</a></li> </ul> </div> 

If you care about outdated IEs, follow this post for creating CSS3 cross browser linear gradient . 如果您关心过时的IE,请按照此帖子创建CSS3跨浏览器线性渐变

If your style has to be on the list element then you can add margin-bottom:-5px; 如果您的样式必须在list元素上,则可以添加margin-bottom:-5px; and padding-bottom:5px; padding-bottom:5px; to your .current class. 到您的.current类。 Otherwise I would suggest going with Pangloss's answer of styling the anchor element. 否则,我建议遵循Pangloss关于锚元素样式的答案。

  .site-nav { display: block; } .site-nav ul { background: linear-gradient(0deg, #d0d0d0, #fefefe); border-top: #bdbdbd solid 1px; border-bottom: #bdbdbd solid 1px; } .site-nav ul li { display: inline-block; width: 16.366666%; height: 2.5rem; text-align: center; line-height: 2.5rem; background: linear-gradient(0deg, #d0d0d0, #fefefe); } .site-nav ul li:hover { background: linear-gradient(0deg, #babcbf, #e1e3e6); } .site-nav ul li.current { position: relative; background: #fff; width: 16%; border-top: #bdbdbd solid 1px; border-left: #bdbdbd solid 1px; border-right: #bdbdbd solid 1px; top: -5px; padding-bottom:5px; /* add this */ margin-bottom:-5px; /* add this */ } .site-nav a { font-family: Helvetica, sans-serif; color: #616264; text-transform: uppercase; letter-spacing: .08rem; text-shadow: rgb(224, 224, 224) 1px 1px 0px; filter: progid:DXImageTransform.Microsoft.Shadow(OffX=1, OffY=1, color=#e0e0e0); font-size: .75rem; } 
 <div class="site-nav"> <ul> <li><a href="#">one</a></li> <li><a href="#">two</a></li> <li><a href="#">three</a></li> <li class="current"><a href="#">four</a></li> <li><a href="#">five</a></li> <li><a href="#">six</a></li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM