Search code examples
htmlcssflexboxstylus

Aligning navbar unordered list with flexbox


I am using flexbox to create a navbar - A Codepen here. I want a logo on the left and the links on the right of the navbar.

However I cannot workout how to use flex-end to good effect. My links in the unordered list do not appear to be affected by flexbox. Is there a special way to bind flexbox to a navbar?

My HTML:

<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />
<nav>
    <!-- Primary Logo -->
    <div id="logo">
        <img src="http://www.shinetown.com.sg/shinetown/wp-content/uploads/2016/04/logo-dummy.png" alt="NewCircle">
    </div>
    <!-- Primary Navigation -->
    <div id="navigation">
        <ul>
            <li class="current"><a href="#">Home</a>
            <li><a href="#">Products</a>
            <li><a href="#">Pricing Plans</a>
            <li><a href="#">Help</a>
        </ul>
    </div>
</nav>

and my Stylus code:

nav-height = 100px
primary-color = #FF5B4E
primary-text-color = #444
secondary-border-color = #EEE

body {
  font-family: Raleway
}

nav, nav *
  display: flex
  align-items: center
  width: 100%

nav
  height: nav-height
  border-bottom: 5px solid secondary-border-color

  #logo
    width: 300px
    height: nav-height
    border-right: 1px solid secondary-border-color
    padding: 0 1em 0 1em

    img
      max-width: 100%


  #navigation
    height: 100%
    justify-content: flex-end

    ul
      list-style-type: none
      padding: 0
      height: 100%
      margin: 0

      li
        height: 100%
        a
          height: 100%
          transition: color 0.4s ease
          font-weight: bold
          font-size: 13px
          letter-spacing: 1px
          text-transform: uppercase
          text-align: center
          text-decoration: none
          color: primary-text-color
          padding: 0 18px 0 18px

        a:hover
          color: primary-color

      .current
        > a
          color: primary-color

Solution

  • flex-end won't solve it, margin-left: auto will.

    nav, nav *
      display: flex
      align-items: center
    
      #navigation
        height: 100%
        margin-left: auto
    

    Also, in the rule you have nav *, which will make all nav's descendants to have 100% width, and the #navigation can't align right being full width.

    Removing width: 100% from your nav, nav * rule will fix that.

    Updated codepen

    Stack snippet

    body {
      font-family: Raleway;
    }
    nav,
    nav * {
      display: flex;
      align-items: center;
    }
    nav {
      height: 100px;
      border-bottom: 5px solid #eee;
    }
    nav #logo {
      width: 300px;
      height: 100px;
      border-right: 1px solid #eee;
      padding: 0 1em 0 1em;
    }
    nav #logo img {
      max-width: 100%;
    }
    nav #navigation {
      height: 100%;
      margin-left: auto;
    }
    nav #navigation ul {
      list-style-type: none;
      padding: 0;
      height: 100%;
      margin: 0;
    }
    nav #navigation ul li {
      height: 100%;
    }
    nav #navigation ul li a {
      height: 100%;
      transition: color 0.4s ease;
      font-weight: bold;
      font-size: 13px;
      letter-spacing: 1px;
      text-transform: uppercase;
      text-align: center;
      text-decoration: none;
      color: #444;
      padding: 0 18px 0 18px;
    }
    nav #navigation ul li a:hover {
      color: #ff5b4e;
    }
    nav #navigation ul .current > a {
      color: #ff5b4e;
    }
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />
    <nav>
        <!-- Primary Logo -->
        <div id="logo">
            <img src="http://www.shinetown.com.sg/shinetown/wp-content/uploads/2016/04/logo-dummy.png" alt="NewCircle">
        </div>
        <!-- Primary Navigation -->
        <div id="navigation">
            <ul>
                <li class="current"><a href="#">Home</a>
                <li><a href="#">Products</a>
                <li><a href="#">Pricing Plans</a>
                <li><a href="#">Help</a>
            </ul>
        </div>
    </nav>


    Note, the justify-content: flex-end should be used on the flex container, not on flex items. When one want to set it on a flex item, use align-self: flex-end.

    In this case the flex has row direction, so the align-self won't work as it applies on the cross axis, hence margin-left: auto will do the trick.