0

I've got this nav bar almost right where I want it, but I just need to center it on the page. How can I center the navigation without changing anything else? No matter what I try, it will always align to the left of the page. I know it's probably because of the "float: left" properties, but changing those ruins the whole navbar layout.

.navbar {
  overflow: hidden;
  background-color: none;
  text-align: center;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: grey;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition-duration: 0.3s;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: grey;
  padding: 0px 0px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover,
.dropdown:hover .dropbtn {
  color: #FFF;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: none;
  margin-top: 45px;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: grey;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: none;
  padding: 12px 25px;
  color: #FFF;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.navbar .focus {
  color: #FFF;
}
<div class="navbar">
  <a href="#home" class="focus">HOME</a>
  <div class="dropdown"><a href="#">PRACTITIONERS
    <button class="dropbtn">
      <i class="fa fa-caret-down"></i> 
    </button></a>
    <div class="dropdown-content">
      <a href="#">Biochemistry</a>
      <a href="#">Biophysics</a>
      <a href="#">Nutrition</a>
    </div>
  </div>
  <a href="#news">CONSUMERS</a>
  <a href="#news">NETWORK</a>
</div>
Justin H
  • 11
  • 4
  • Please do not use `float`: https://stackoverflow.com/questions/9776840/is-float-for-layout-bad-what-should-be-used-in-its-place. There are way better methods of layout now, and you won't have to fight with clears and how other floats affect elements. – disinfor Oct 07 '22 at 01:52

3 Answers3

2

you can add display flex and justify content center like this

.navbar {
  display: flex;
  justify-content: center;
  overflow: hidden;
  background-color: none;
  text-align: center;
}
Eldo
  • 78
  • 3
  • While this may answer the question, add the entire code from OPs question so we can see it working. It makes for a much better answer for future visitors. – disinfor Oct 07 '22 at 02:00
-1
.navbar {
  overflow: hidden;
  background-color: none;
  text-align: center;
  margin-left: 25%;
}
Paramu
  • 613
  • 2
  • 10
  • 23
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Oct 08 '22 at 08:38
-1

Make navbar a Flexbox and with justify-content: center, align its children to the center of navbar.

body {
  background: lightgray;
}

.navbar {
  /* add the following two styles */
  display: flex;
  justify-content: center;
  overflow: hidden;
  background-color: none;
  text-align: center;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: grey;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition-duration: 0.3s;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: grey;
  padding: 0px 0px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover,
.dropdown:hover .dropbtn {
  color: #FFF;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: none;
  margin-top: 45px;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: grey;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: none;
  padding: 12px 25px;
  color: #FFF;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.navbar .focus {
  color: #FFF;
}
<div class="navbar">
  <a href="#home" class="focus">HOME</a>
  <div class="dropdown"><a href="#">PRACTITIONERS
    <button class="dropbtn">
      <i class="fa fa-caret-down"></i> 
    </button></a>
    <div class="dropdown-content">
      <a href="#">Biochemistry</a>
      <a href="#">Biophysiscs</a>
      <a href="#">Nutrition</a>
    </div>
  </div>
  <a href="#news">CONSUMERS</a>
  <a href="#news">NETWORK</a>
</div>
technophyle
  • 7,972
  • 6
  • 29
  • 50