David Wahlstrom
David Wahlstrom

Reputation: 29

CSS3 Nav not aligning

I've researched this topic and noticed I'm not the only one with this issue; however the answers found on others with the same question hasn't solved anything. I set my links specific to nav, have set both nav and 'a nav' to position relative, margin to 0, text-align to center, veritcal align to middle but can't seem to get it centered.

nav {
  margin: auto;
  margin-bottom: 10px;
  max-width: 35%;
  height: 45px;
}
nav a {
  position: relative;
  padding: 0px 15px;
  margin: 4px 0px;
  font-size: 25px;
  text-decoration: none;
  color: #ECF0F1;
  border: 1px solid black;
}
<nav>
  <a href="#">Home</a><a href="#">Team</a><a href="#">About</a><a href="#">Files</a><a href="#">Forum</a>
</nav>

As a slight edit; why does the hover not cover the entire length of the nav? Do I just need to adjust the top and bottom padding?

Layout

Hover Result

Upvotes: 0

Views: 44

Answers (3)

Sebastian Brosch
Sebastian Brosch

Reputation: 43564

Solution #1 (using flexbox / newer browser):

nav {
  display:flex;
  flex-direction:row;
  margin: auto;
  margin-bottom: 10px;
  height: 45px;
  justify-content:center;
}
nav a {
  position: relative;
  padding:0px 15px;
  margin: 4px 0px;
  font-size: 25px;
  text-decoration: none;
  color: #ECF0F1;
}
<nav>
  <a href="#">Home</a>
  <a href="#">Team</a>
  <a href="#">About</a>
  <a href="#">Files</a>
  <a href="#">Forum</a>
</nav>

Solution #2 (for older browser too):

nav {
  margin-bottom:10px;
  height:45px;
  text-align:center;
}
nav a {
  display:inline;
  padding:0px 15px;
  margin: 4px 0px;
  font-size: 25px;
  text-decoration:none;
  color:#ECF0F1;
}
<nav>
  <a href="#">Home</a>
  <a href="#">Team</a>
  <a href="#">About</a>
  <a href="#">Files</a>
  <a href="#">Forum</a>
</nav>

Upvotes: 1

sQer
sQer

Reputation: 1151

You can use lists as the markup for navigation

HTML:

<nav>
  <ul>
    <li>
      <a href="#">Home</a>
    </li>
    <li><a href="#">Team</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Files</a></li>
    <li><a href="#">Forum</a></li>
  </ul>
</nav>

CSS:

nav {
  vertical-align: middle;
  margin: auto;
  margin-bottom: 10px;
  max-width: 35%;
  height: 45px;
}

nav li {
  display: inline-block;
  position: relative;
  padding: 0px 15px;
  margin: 4px 0px;
}

nav li a {
  font-size: 25px;
  text-decoration: none;
  color: #ECF0F1;
}

Upvotes: 0

Duc Nguyen
Duc Nguyen

Reputation: 474

Edit your code

nav{
    margin: auto;
    margin-bottom: 10px;
    text-align:center;
    height: 45px;
}

Upvotes: 0

Related Questions