Question

I am trying to center my navigation. The CSS text-align: center; is not working. It doesn't seem to affect the nav in anyway, shape, or form.

here's my html:

<nav id="user_nav"><ul>
       <li><a href="#">Friends</a></li>
       <li><a href="#">Followers</a></li>
       <li><a href="#">Photos</a></li>
       <li><a href="#">Interests</a></li>
</ul></nav>

here's my CSS:

#user_nav {
    height: 60px;
    border-bottom: 1px solid black;
    width: 100%;
}

#user_nav ul {
    margin: 0 auto;
    width: 100%;
}

#user_nav ul li {
    list-style: none;
    display: inline;
    text-align: center;
}

#user_nav ul li a {
    padding: 15px 25px 0 0;
    font-size: 18px;
    float: left;
}

Please note: I am trying to make the links centered horizontally as well as vertically.

If there is a better way to do it than how I am doing it now, I am open to suggestions.

Please help me.

Was it helpful?

Solution

Fiddle

CSS

 #user_nav {
    height: 60px;
    border-bottom: 1px solid black;
    width: 100%;
}

#user_nav ul {
    margin: 0 auto;
    width: 100%;
    text-align: center;
}

#user_nav ul li {
    list-style: none;
    display: inline-block;    
}

#user_nav ul li a {
    vertical-align: middle;
    padding: 15px 25px 0 0;
    font-size: 18px;
    display: block;
}

HTML

<nav id="user_nav"><ul>
       <li><a href="#">Friends</a></li>
       <li><a href="#">Followers</a></li>
       <li><a href="#">Photos</a></li>
       <li><a href="#">Interests</a></li>
</ul></nav>

OTHER TIPS

You may be interested in this: Live demo (click).

#user_nav {
  height: 60px;
  border-bottom: 1px solid black;
  width: 100%;
}

#user_nav ul {
  width: 100%;
  display: table;
}

#user_nav ul li {
  list-style: none;
  display: table-cell;
  font-size: 18px;
}

If you use inline-block you'll need to space the elements out manually by setting the width: Live demo (click).

#user_nav {
  height: 60px;
  border-bottom: 1px solid black;
  width: 100%;
}

#user_nav ul {
  width: 100%;
}

#user_nav ul li {
  list-style: none;
  display: inline-block;
  width: 24%;
  font-size: 18px;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top