Html 在html导航栏中的链接之间放一条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29446595/
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-08-29 03:47:03  来源:igfitidea点击:

Put a line between links in html navigation bar

htmlcsshyperlinknavbar

提问by mohsen.nour

In the below html and css code how I can add separator or line between two link tag not after the link
the line should be in the middle of two link tag not after element tag

在下面的 html 和 css 代码中,我如何在两个链接标签之间添加分隔符或行,而不是在链接之后,
该行应该在两个链接标签的中间,而不是在元素标签之后

<style>
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

li {
    float: left;
}

a:link, a:visited {
    display: block;
    width: 120px;
    font-weight: bold;
    color: #FFFFFF;
    background-color: #98bf21;
    text-align: center;
    padding: 4px;
    text-decoration: none;
    text-transform: uppercase;
}

a:hover, a:active {
    background-color: #7A991A;
}
</style>
 <!DOCTYPE html>
<html>
<head>
</head>
<body>

<ul>
  <li><a href="#home">Home |</a></li>
  <li><a href="#news">News |</a></li>
  <li><a href="#contact">Contact |</a></li>
  <li><a href="#about">About |</a></li>
</ul>

</body>
</html>

回答by Manwal

I have added just one line in your css of li:

我在你的 li css 中只添加了一行:

border-right:solid 1px #fff;

DEMOThis is better way of added separator line between navbar links. Better way then added |manually after any tag.

演示这是在导航栏链接之间添加分隔线的更好方法。更好的方法是|在任何标签之后手动添加。

When your Nav bar will cover complete web page width you should avoid Right-Border to last element. Like:

当您的导航栏将覆盖整个网页宽度时,您应该避免右边框到最后一个元素。喜欢:

li:not(:last-child) { 
    border-right:solid 1px #fff;/* This will not give border to last li element */
}

回答by Logz

Try this ,

尝试这个 ,

border-right:1px solid #FFF

Demo

演示

回答by Michelangelo

If you really want a symbol you can go for something like this which requires a bit more code: fiddle

如果你真的想要一个符号,你可以选择这样的东西,这需要更多的代码:fiddle

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

li {
    float: left;
    position:relative;
}

.divider {
    position: absolute;
    right:-1px;
    top:2px;
}


a:link, a:visited {
    display: block;
    width: 120px;
    font-weight: bold;
    color: #FFFFFF;
    background-color: #98bf21;
    text-align: center;
    padding: 4px;
    text-decoration: none;
    text-transform: uppercase;
}

a:hover, a:active {
    background-color: #7A991A;
}

回答by khv2online

<html>

<head>
    <style>
    .yourNav {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }

    .yourNav li {
        float: left;
    }

    .yourNav a{
        position: relative;
    }

    .yourNav a:after{
        position: absolute;
        right: -1px;
        top: 3px;
        bottom: 3px;
        width: 3px;
        background: #fff;
        content: "";
    }

    .yourNav li:last-child a:after{
        display: none;
    }

    .yourNav a:link,
    .yourNav a:visited {
        display: block;
        width: 120px;
        font-weight: bold;
        color: #FFFFFF;
        background-color: #98bf21;
        text-align: center;
        padding: 4px;
        text-decoration: none;
        text-transform: uppercase;
    }

    a:hover,
    a:active {
        background-color: #7A991A;
    }
    </style>
</head>

<body>
    <ul class="yourNav">
        <li><a href="#home">Home</a></li>
        <li><a href="#news">News</a></li>
        <li><a href="#contact">Contact</a></li>
        <li><a href="#about">About</a></li>
    </ul>
    <script type="text/javascript">
    </script>
</body>

</html>