twitter-bootstrap 用导航栏画一条小的垂直线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41141360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:26:23  来源:igfitidea点击:

Draw a small vertical line with navbar

csstwitter-bootstrap

提问by uneeb meer

I need to draw a small vertical linethat should be appear after every navbar li.

我需要画一条小的垂直线,应该出现在每个导航栏 li 之后。

Buut the issue is whenever I try to draw something, navbar gets messed up and the navbar goes into second lines. I cant really draw a vertical line either.here's the code

但是问题是每当我尝试绘制某些东西时,导航栏都会弄乱并且导航栏进入第二行。我也不能画一条垂直线。这是代码

html

html

<!DOCTYPE HTML>
<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Double Double</title>
    <link rel="shortcut icon"  href="favicon.ico">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700%7CRoboto+Slab:400,100' rel='stylesheet' type='text/css' />
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/double.css" rel="stylesheet">
</head>

<body>
<nav class="navbar" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div style="margin: auto;display: table;">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="#">Home</a></li>
                    <hr class="vertical"/>
                    <li><a href="#">Deals</a></li>
                    <li><a href="#">Pickup Deals</a></li>
                    <li><a href="#">Menu</a></li>
                    <li><a href="#">About Us</a></li>
                    <li><a href="#">Contact Us</a></li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div>
    </div>
</nav>
</body>
</html>

css

css

/*navbar properties */
.navbar .brand {
    max-height: 40px;
    overflow: visible;
    padding-top: 0;
    padding-bottom: 0;
}
.navbar a.navbar-brand {
    padding: 9px 15px 8px;
}
.navbar{
    font-family: young;
    clear: both;
}
.navbar a{
    color: #515254;
}
.navbar-nav{
    font-size: 20px;
    padding-top: 20px;
}
#bs-example-navbar-collapse-1{
    float: left;
}
.nav > li > a:hover, .nav > li > a:focus{
    color: #007f3d;
    background: none;
}
.navbar-toggle .icon-bar {
    background-color: black;
}

.navbar-toggle {
    border: 1px solid #000;
}
.verticalLine {
    border-left: thick solid #ff0000;
}
hr.vertical
{
    color: black;
    width: 0px;
    height: 100%; /* or height in PX */
}
/*navbar properties */

JSFIDDLE

JSFIDDLE

回答by Taniya

Use border-right for each menu li instead of using hr.

为每个菜单 li 使用 border-right 而不是使用 hr。

.navbar-nav>li{
  border-right: 1px solid #000;
}

and remove the last element border.

并删除最后一个元素边框。

.navbar-nav>li:last-child{
   border: none;
}

回答by Banzay

You may not add <hr>element. just write this:

您不能添加<hr>元素。只写这个:

.nav > li:not(:last-child) {
    border-right: 1px solid red;
    box-sizing: border-box;
}

box-sizing: border-box;is for width of liwouldn't exceed its original width

box-sizing: border-box;用于宽度li不会超过其原始宽度

回答by Shivkumar kondi

May be this is what you want..

可能这就是你想要的..

.navbar-default {
  background: #005986;
}
.navbar {
  border: 0;
  border-radius: 0;
}
ul.nav {
  list-style: none;
  border-right: 1px solid #84B6D0;
}
ul.nav li {
  padding: 20px 0;
  display: inline-block;
}
ul.nav li a {
  padding: 20px 10px;
  color: #fff;
  position: relative;
}
ul.nav li a:after {
  position: absolute;
  content: "";
  width: 2px;
  height: 60%;
  right: 0;
  background: red;
  top: 50%;
  transform: translate(0, -50%);
}
ul.nav li:first-child a:before {
  position: absolute;
  content: "";
  width: 2px;
  height: 60%;
  left: 0;
  background: white;
  top: 50%;
  transform: translate(0, -50%);
}
.navbar-default .navbar-nav>li>a,
.navbar-default .navbar-nav>li>a:focus,
.navbar-default .navbar-nav>li>a:hover {
  color: blue;
}
.navbar-default .navbar-nav>li>a:hover {
  background: #022E44;
}
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <ul class="nav navbar-nav">
      <li><a href="#">Link 1</a>
      </li>
      <li><a href="#">Link 2</a>
      </li>
      <li><a href="#">Link 3</a>
      </li>
      <li><a href="#">Link 4</a>
      </li>
    </ul>
  </div>
</nav>

回答by Veiko J??ger

Box-shadow does not create new elements. Try adding:

Box-shadow 不会创建新元素。尝试添加:

@media (min-width: 768px)
    .navbar-nav > li {
        ...
        box-shadow: inset -1px 0 0 0 black; // high element or..
        box-shadow: 16px 0 0 -15px black;  // .. closer to text height
    }
    .navbar-nav > li:last-of-type {
        ...
        box-shadow: none;
    }

回答by T K

You should take a look at this css selector ~.

你应该看看这个 css selector ~

http://www.w3schools.com/cssref/css_selectors.asp

http://www.w3schools.com/cssref/css_selectors.asp

ul li {
  display: inline-block;
  padding: 0 10px;
}
ul li ~ li {
  border-left: 1px solid #333;
}
<ul>
  <li>
    <span>Item</span>
  </li>
  <li>
    <span>Item</span>
  </li>
  <li>
    <span>Item</span>
  </li>
</ul>

回答by Rounak Khan

You can add the following lines in your css file. the problem will be solved.

您可以在 css 文件中添加以下几行。问题将得到解决。

ul.navbar-nav li{
  float: left;
  list-style: none;
  padding: 0px 6px;
  border-right: 1px solid #000;
}
ul.navbar-nav li a{
  text-decoration: none;
}
ul.navbar-nav li:last-child{  
  border-right: none;
}

or instead of using border-right: 1px solid #000; in li you can use an image here as a background.

或者不使用 border-right: 1px solid #000; 在 li 中,您可以使用此处的图像作为背景。

here is my codepen link: http://codepen.io/rounak/pen/dOgLBx

这是我的 codepen 链接:http://codepen.io/rounak/pen/dOgLBx

回答by kelvinM

ul li {
  display: inline-block;
  padding: 0 10px;
}
ul li ~ li {
  border-bottom: 1px solid #333;
}
<ul>
  <li>
    <span>Item</span>
  </li>
  <li>
    <span>Item</span>
  </li>
  <li>
    <span>Item</span>
  </li>
</ul>