-1

I am actually trying to design the navbar for website and I am facing issue , I have one logo to show on the left and UL to right side on nav , I have created both and now I want them both to be center-vertical in their div container , unfortunately I am not able to center-vertical UL tag , need some help in css or html !

Here I am attaching the html

header {
  height: 100vh;
  background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(../../img/static/cover.jpg);
  background-size: cover;
  background-repeat: no-repeat;
}

.div_nav {
  overflow: auto;
  width: 80%;
  margin: 0px auto;
  padding: 16px 32px;
  background-color: rgba(139, 0, 139, 0.459);
}

.nav_logo {
  background-color: darkorange;
  height: 72px;
  width: auto;
}

.nav_ul {
  background-color: darkslateblue;
  float: right;
  margin: auto 0px;
  list-style: none;
}

.nav_ul li {
  margin-left: 16px;
  display: inline-block;
}

.nav_ul li a {
  color: #fff;
  text-decoration: none;
  text-transform: uppercase;
  font-family: 'Montserrat', sans-serif;
}
<header>
  <nav>
    <div class="div_nav">
      <img src="././img/static/logo.png" alt="Bbooster_Logo" class="nav_logo">
      <ul class="nav_ul">
        <li><a href="#">Para</a></li>
        <li><a href="#">Sagro</a></li>
        <li><a href="#">Miasa</a></li>
        <li><a href="#">Varga</a></li>
      </ul>
    </div>
  </nav>
</header>

here I am attaching the image of output which I am currently getting enter image description here

Ritu
  • 724
  • 4
  • 14
Nisarg Jani
  • 157
  • 10

2 Answers2

0

add flex rules for the .div_nav class and remove float: right from the .nav_ul class, since justify-content: space-between is used

header {
    height: 100vh;
    background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(../../img/static/cover.jpg);
    background-size: cover;
    background-repeat: no-repeat;
}

.div_nav {
    display: flex;
    align-items: center;
    justify-content: space-between;

    overflow: auto;
    width: 80%;
    margin: 0px auto;
    padding: 16px 32px;
    background-color: rgba(139, 0, 139, 0.459);
}

.nav_logo {
    background-color: darkorange;
    height: 72px;
    width: auto;
}

.nav_ul {
    background-color: darkslateblue;
    /*float: right;*/
    margin: auto 0px;
    list-style: none;
}

.nav_ul li {
    margin-left: 16px;
    display: inline-block;
}

.nav_ul li a {
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
    font-family: 'Montserrat', sans-serif;
}
<header>
    <nav>
        <div class="div_nav">
            <img src="././img/static/logo.png" alt="Bbooster_Logo" class="nav_logo">
            <ul class="nav_ul">
                <li><a href="#">Para</a></li>
                <li><a href="#">Sagro</a></li>
                <li><a href="#">Miasa</a></li>
                <li><a href="#">Varga</a></li>
            </ul>
        </div>
    </nav>
</header>
s.kuznetsov
  • 13,325
  • 3
  • 8
  • 21
  • ok let me try then – Nisarg Jani Aug 23 '20 at 17:40
  • Yeah It worked , but can explain a bit so new learner like me can understand what exactly I did wrong ? – Nisarg Jani Aug 23 '20 at 17:43
  • I can explain. it's all about the lack of a display parameter in the div_nav class. And in my example, I applied the align-items: center rule, and you do not need to use this parameter, since you have a margin: 0px auto written which will already center your menu horizontally. And by the way, in the example I wrote justify-content: space-between, which scatters elements in different corners, which means that float: right is no longer relevant in your code. As a beginner, use flex and grid. – s.kuznetsov Aug 23 '20 at 17:54
0

Well, you want to say horizontal UL. You can use flex for this..

.div_nav {
  width: 80%;
  margin: 0px auto;
  padding: 16px 32px;
  background-color: rgba(139, 0, 139, 0.459);
  /* Edit */
  display: flex;
  align-items: center;
  /* This centers the elements consisting inside a flexible row div */
  justify-content: space-between;
  /*to keep the maximum space between consisting childs */
}

Remove the float from nav_ul

Ritu
  • 724
  • 4
  • 14
KR Tirtho
  • 183
  • 10