Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 Separators in navbar

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.

enter image description here

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.

enter image description here

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>
like image 920
Mr. Daniel Avatar asked Jan 20 '19 21:01

Mr. Daniel


3 Answers

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>
like image 82
Shidersz Avatar answered Oct 17 '22 16:10

Shidersz


@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>
like image 40
Αntonis Papadakis Avatar answered Oct 17 '22 17:10

Αntonis Papadakis


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;
}
like image 2
user4533280 Avatar answered Oct 17 '22 18:10

user4533280