菜单列表项:全宽(边框底部)?

时间:2015-10-15 11:45:15

标签: css list menu

我从列表中有一个菜单。在小宽度上我希望border-bottom使用全宽:

enter image description here

HTML:

<div id="wrapper">
    <ul id="menu">
        <li class="active"><a href="#">Item 1</a></li>
        <li><a href="#">Item 2 with Text</a></li>
        <li><a href="#">Item 3 with more Text</a></li>
        <li><a href="#">Item 4</a></li>
        <li><a href="#">Item 5 with Text</a></li>
    </ul>
</div>

CSS:

#menu li {
    display: inline-block;
}
#menu li:not(.active) {
    border-bottom: 2px solid #ccc;
}
#menu li.active {
    border-bottom: 2px solid red;
}
#menu li a {
    padding: .5em 1em;
    box-sizing: border-box;
    display: block;
}

测试: Fiddle

2 个答案:

答案 0 :(得分:3)

如果没有背景图片,您将不得不发挥创意。怎么样......

#menu {
    overflow: hidden;   
    padding: 0 0 2px;
}
#menu li {
    display: inline-block;
    position: relative;
    z-index: 2;
}
#menu li:after {
    content: " ";
    position: absolute;
    bottom: -2px;
    right: -1000px;
    width: 1000px;
    height: 0;
    border-bottom: 2px solid #ccc;
    z-index: 1;
}

更新了小提琴:jsfiddle.net/xrz39p4L/12/

答案 1 :(得分:1)

flexbox(由路易斯P.A.提及)可以帮助li填充ul的全宽并根据需要进行包装。

Flexbox支持是IE10及更高版本。

&#13;
&#13;
var li = $("#menu li");

li.click(function() {
  li.removeClass("active");
  $(this).addClass("active");
});
&#13;
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body {
  background-color: #eee;
}
#wrapper {
  width: 300px;
  padding: 1em;
  background-color: #fff;
}
a {
  color: inherit;
  text-decoration: none;
}
#menu {
  list-style: none;
  padding: 0;
  -webkit-margin-before: 0;
  -webkit-margin-after: 0;
  margin: 0 1em 1em;
  overflow: hidden;
  display: flex;
  flex-wrap: wrap;
}
#menu li {
  flex: 1 1 auto;
  position: relative;
}
#menu li:not(.active) {
  border-bottom: 2px solid #ccc;
}
#menu li.active {
  border-bottom: 2px solid red;
}
#menu li:not(:last-of-type) {} #menu li a {
  padding: .5em 0;
  box-sizing: border-box;
  display: block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  <ul id="menu">
    <li class="active"><a href="#">Item 1</a>
    </li>
    <li><a href="#">Item 2 with Text</a>
    </li>
    <li><a href="#">Item 3 with more Text</a>
    </li>
    <li><a href="#">Item 4</a>
    </li>
    <li><a href="#">Item 5 with Text</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;