skip to Main Content

I am building a navigation bar that has a lot of options and special sections.
I worked with Twitter Bootstrap, but it is difficult to develop.
The nav html tag has 3 sections grouped in 3 divs (left, center, right).
I am having difficulty in centring horizontally the text and logo of the company in left div, anchors with navigation items in the right div.
I need the height of navigation bar to be set in the CSS and not the calculate the height based of the child elements.

This is the html:

.navbar {
  overflow: hidden; /* Clips from content if it is bigger than the parent element */
  background-color: #333;
  position: fixed; /* Position of the navbar is fixed */
  top: 0;
  width: 100%;
  height: 50px;
}

.left-navbar {
  float: left;
  background: cadetblue;
  width: 230px;
  height: 100%;
  text-align: center;
}

.right-navbar {
   float: right;
  background: maroon;
  height: 100%; 
    /* float: right;
    position: absolute;
    top: 50%; right: 0%;
    transform: translate(-50%,-50%);
    background: gold;
    padding: 1.5rem; */
}


.center-navbar {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%,-50%);
    background: gold;
    padding: 1rem;
}

.left-navbar strong {
  color: red;
  padding: 10px 10px;
  display:inline-block;
  text-align: center;
  vertical-align: center;
}

.left-navbar img {
  width: 32px;
  height: 32px;
  padding: 10px 10px;
}

.navbar a {
  float: right; /* Orientation of the element in the parent element */
  display: block; /* All over top left right bottom it is a block - element has block/padding all over the embedded element */
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px; /* 14px top and bottom, 16px right and left */
  text-decoration: none; /* Could be underline, overline, line-through */
  font-size: 17px;
}

/* Apply only for anchors inside the navbar class */
.navbar a:hover {
  background: #ddd;
  color: black;
}

input[type="text"]{ padding: 5px 5px; line-height: 28px; }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<nav class="navbar">
  <div class="left-navbar">
    <strong>Company</strong>
    <img src="https://cdn0.iconfinder.com/data/icons/social-flat-rounded-rects/512/newsvine-512.png"></p>
  </div>
  <div class="center-navbar">
      <input type="text" id="name" name="name" required height="45px;"
       minlength="4" maxlength="40" size="40">
  </div>
  <div class="right-navbar">
    <a href="#home">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
  </div>
</nav>

Any working fiddle with best practices is ideal for me.

Thank you!

3

Answers


  1. Give .left-navbar – horizontal and vertical centering with display:flex;

     .left-navbar {
    display: flex;
    float: left;
    background: cadetblue;
    width: 230px;
    height: 100%;
    text-align: center;
    justify-content: center;
    align-items: center;
    

    }

    Also, how do you want the right part of the navbar?

    Login or Signup to reply.
  2. You can use flexbox to achieve this

    .right-navbar, .left-navbar{
      display: flex;
      justify-content: center;
      align-items: center;
    }
    

    Here you have a codepen, let me know if that help!

    Login or Signup to reply.
  3. Flex-box is what you’ll want to use here. Add display: flex to the .navbar and then add flex-grow: 1; to the center piece. This essentially says ‘make this element span the remaining space in the flex container. Also, your height: 100% were unnecessary, so I removed them.

    .navbar {
      overflow: hidden; /* Clips from content if it is bigger than the parent element */
      background-color: #333;
      position: fixed; /* Position of the navbar is fixed */
      top: 0;
      width: 100%;
      height: 50px;
      display: flex;
    }
    
    .left-navbar {
      background: cadetblue;
      width: 230px;
      text-align: center;
    }
    
    .right-navbar {
      background: maroon;
    }
    
    .center-navbar {
      background: gold;
      padding: 1rem;
      flex-grow: 1;
    }
    
    input[type="text"]{
      padding: 5px 5px;
      line-height: 28px;
      width: 100%;
      box-sizing: border-box;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search