Bootstrap 4 Navigation Divider

AthenaBK picture AthenaBK · Jan 23, 2018 · Viewed 10k times · Source

I've been trying to combine two ul in one responsive navigation bar with a divider, without messing up with the seconds ul css. I've tried with mediaqueries but i haven't been able to make it work. To make it more clear, I need the first ul when it collapses to be centered, then have a divider and after that have the other ul centered beneath it.

HTML:

<nav class="navbar navbar-expand-md navbar-dark">
    <div class="container">
      <a href="#" class="navbar-brand"><img src="img/logo.png" alt="Akrotopi Apartments"></a>
      <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#main-nav">
        <span class="menu-icon-bar"></span>
        <span class="menu-icon-bar"></span>
        <span class="menu-icon-bar"></span>
      </button>
      <div id="main-nav" class="collapse navbar-collapse">
        <ul class="navbar-nav ml-auto">
          <li><a href="#" id="home" class="nav-item nav-link active">Αρχική</a></li>
          <li><a href="#" class="nav-item nav-link">Σχετικά</a></li>
          <li><a href="#" class="nav-item nav-link">Δωμάτια</a></li>
          <li><a href="#" class="nav-item nav-link">Επικοινωνία</a></li>
          <div class="dropdown-divider" ></div>
          <div id="lang">
          <ul>
            <li><a href="index.html" id="active">GR</a></li>
            <li><a href="lang.en.html">EN</a></li>
            </ul>
        </div>
        </ul>
      </div>

    </div>
  </nav>

CSS:

@media screen and (max-width: 768px) {
      .navbar-brand {
        margin-left: 20px;
       }

      .navbar-nav {
        text-align: center;
        padding: 0 20px;
        background-color: #f8f9fa00;
       }

     .navbar.fixed-top .navbar-nav {
       background: transparent;
       }
       }

      #lang ul{
       position:absolute;
       top:15px;
       right:10px;
       list-style:none;
       padding:0;
       margin:0;
      }
      }

Any ideas on how to fix it?

Thank you !!

Answer

dferenc picture dferenc · Jan 24, 2018

First thing first: the current way how you nesting the <ul> tags is invalid, as <ul> can only have <li> elements as their immediate children. So you either have to wrap the nested <ul> into a <li>, or have the two <ul> on the same level. In this case, I would suggest to go with the latter.

Also, you could apply the #lang id directly to the <ul> tag, without the wrapper div. The divider than could be shaped with a simple border top property applied on smaller devices.

Having said that, your markup and css could be something like as follows.

@media screen and (max-width: 768px) {
    .navbar-brand {
        margin-left: 20px;
    }

    .navbar-nav {
        text-align: center;
        padding: 0 20px;
        /*background-color: #f8f9fa00; invalid color value */
    }

    .navbar.fixed-top .navbar-nav {
        background: transparent;
    }

    #lang {
        border-top: 1px solid #f8f9fa;
    }
}

@media screen and (min-width: 767px) {
    #lang {
        position:absolute;
        top:15px;
        right:10px;
        list-style:none;
        padding:0;
        margin:0;
    }
}
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
    <div class="container">
        <a href="#" class="navbar-brand"><img src="img/logo.png" alt="Akrotopi Apartments"></a>
        
        <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#main-nav">
            <span class="menu-icon-bar"></span>
            <span class="menu-icon-bar"></span>
            <span class="menu-icon-bar"></span>
        </button>

        <div id="main-nav" class="collapse navbar-collapse">
            <ul class="navbar-nav ml-auto">
                <li><a href="#" id="home" class="nav-item nav-link active">Αρχική</a></li>
                <li><a href="#" class="nav-item nav-link">Σχετικά</a></li>
                <li><a href="#" class="nav-item nav-link">Δωμάτια</a></li>
                <li><a href="#" class="nav-item nav-link">Επικοινωνία</a></li>
            </ul>

            <ul class="navbar-nav" id="lang">
                <li><a href="index.html" id="active">GR</a></li>
                <li><a href="lang.en.html">EN</a></li>
            </ul>
        </div>
    </div>
</nav>


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>