I'm trying to place separators in a navbar
between the nav-items
, but I have no idea how to set padding on separators in equal range between the nav-item
.
My second problem is that, the separators have appeared in the toggle-navbar
(when the navbar
is collapsed) and I've tried to remove them, but unsuccessfully.
If someone knows a better way to create separators, I would be thankful.
Here is my code:
.navbar-nav > li > a::after {
content: "|";
padding: 0 5px
}
<div class="collapse navbar-collapse" id="navbarResponsive">
<!--
1) collapse - class dedicate to a given alement
2) navbar-collapse: show elements to bootstrap which belong to navbar, then use function - collapse
-->
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">O nas</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Cennik</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Usługi</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Rejestracja</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Porady</a>
</li>
</ul>
</div>
Maybe a border-right
CSS style on your nav-items
will do the work, and you can disable they on a specific break-point
(MD
is the chosen breakpoint on the next example).
.border-md-right {
border-right: 1px solid silver;
}
@media (max-width: 768px) {
.border-md-right {
border-right: none;
}
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active border-md-right">
<a class="nav-link" href="#">O nas</a>
</li>
<li class="nav-item active border-md-right">
<a class="nav-link" href="#">Cennik</a>
</li>
<li class="nav-item active border-md-right">
<a class="nav-link" href="#">Usługi</a>
</li>
<li class="nav-item active border-md-right">
<a class="nav-link" href="#">Rejestracja</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Porady</a>
</li>
</ul>
</div>
</nav>
@Shidersz solution should work. Another solution if you don't want to add an extra class in every li
. Also may be a bit more difficult if you have a dynamic menu and you have to add this class to every element except the last one!
.navbar-nav .nav-item:not(:last-child) {
border-right: 1px solid silver;
}
@media (max-width: 768px) {
.navbar-nav .nav-item:not(:last-child) {
border-right: none;
}
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">O nas</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Cennik</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Usługi</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Rejestracja</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Porady</a>
</li>
</ul>
</div>
</nav>
This worked for me using Bootstrap 4
.navbar-nav > li > a::after {
content:'|';
padding-left:10px;
@media (max-width: 767px) {
content: none;
padding-left:0;
}
}
.navbar-nav > li:last-child > a::after {
content:none;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With