在导航栏中创建虚线分隔符

时间:2015-01-29 15:21:31

标签: html css

我尝试为导航栏创建一个水平分隔符,类似于:

enter image description here

我已经在HTML中创建了一个简单的不公开导航栏,并创建了一个透明的GIF并将其应用到后台。虽然效果是我想要达到的效果,但我觉得间距有点偏差,我似乎无法纠正它。

继承我的CSS代码:

.on {
    display: inline; 
    background-image:url(images/seperator.gif);
    background-position: center right;
    background-repeat: no-repeat;
}

.last {
    background-image: none;
    padding-left: 0;

}

ul {
    width: 800px;
    padding: 5px;
    margin: 0px auto 0px auto;
    text-align: left;
}

li {    
    display: inline;    
    margin: 0px 3px;
}

a {
    color: #000000;
    text-transform: uppercase;
    text-decoration: none;
    padding: 6px 20px 5px 10px;
}

以下是导航栏的HTML:

<ul id="nav">
    <li><a href="#" class="on">Home</a></li>
    <li><a href="#" class="on">Shop</a></li>
    <li><a href="#" class="on">Contact</a></li>
    <li><a href="#" class="on last">Login</a></li>
</ul>

2 个答案:

答案 0 :(得分:2)

使用li:not(:last-child)选择器排除上一个li并添加点border-right

&#13;
&#13;
.on {
  display: inline;
  background-image: url(images/seperator.gif);
  background-position: center right;
  background-repeat: no-repeat;
}
.last {
  background-image: none;
  padding-left: 0;
}
ul {
  width: 800px;
  padding: 5px;
  margin: 0px auto 0px auto;
  text-align: left;
}
li {
  display: inline;
  margin: 0px 3px;
}
a {
  color: #000000;
  text-transform: uppercase;
  text-decoration: none;
  padding: 6px 20px 5px 10px;
}
li:not(:last-child) {
  border-right: 1px dotted black;
}
&#13;
<ul id="nav">
  <li><a href="#" class="on">Home</a></li>
  <li><a href="#" class="on">Shop</a></li>
  <li><a href="#" class="on">Contact</a></li>
  <li><a href="#" class="on last">Login</a></li>
</ul>
&#13;
&#13;
&#13;


如果要更改分隔符height,可以使用:伪元素代替。

&#13;
&#13;
.on {
  display: inline;
  background-image: url(images/seperator.gif);
  background-position: center right;
  background-repeat: no-repeat;
}
.last {
  background-image: none;
  padding-left: 0;
}
ul {
  width: 800px;
  padding: 5px;
  margin: 0px auto 0px auto;
  text-align: left;
}
li {
  position: relative;
  display: inline;
  margin: 0 3px;
}
a {
  color: #000000;
  text-transform: uppercase;
  text-decoration: none;
  padding: 6px 20px 5px 10px;
}
li:not(:last-child):after {
  content: '';
  position: absolute;
  width: 100%;
  height: 150%;
  top: -25%;
  left: 0;
  background: transparent;
  z-index: -1;
  border-right: 1px dotted black;
}
&#13;
<ul id="nav">
  <li><a href="#" class="on">Home</a></li>
  <li><a href="#" class="on">Shop</a></li>
  <li><a href="#" class="on">Contact</a></li>
  <li><a href="#" class="on last">Login</a></li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个:

li{
    display: block;
    border-right: dashed black;
}