0

I'm buidling my site and I'm trying to horizontal centering the following code with no luck: http://codepen.io/anon/pen/LZZJrK

If I'm not wrong I must add a css code for nav ul so I added the following code but it's not working:

nav ul li {
  display:block;
  text-align:center;
}

Can someone please tell me how can I center the menu so I can have the following output? Thanks in advance. enter image description here

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
Avión
  • 7,963
  • 11
  • 64
  • 105

5 Answers5

2

Add display: table; and margin: 0 auto; to your nav like this:

nav {
    display: table;
    margin: 0 auto;
}

CodePen

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
1

Try with this:

        ul{
text-align:center;}
        li{
display: inline-block;
        float:none;
        text-align:center;
        }
Alejandro Garrido
  • 381
  • 1
  • 3
  • 15
  • Sorry, but it's not working (I tried using the link of the post). I added the code you post, and also changing the `li` to `nav li` but it's not working either. – Avión Jun 16 '16 at 19:24
  • you have 2 classes who have float left, and wrong displays (line 116 and 123). The forked codepen http://codepen.io/capitanflamingo/pen/mEEGgr – Alejandro Garrido Jun 16 '16 at 19:25
1
  1. Remove the float: left property from nav ul li and nav li
  2. Change display:block to display: inline-block for li
  3. Add the following to your css:

#nav ul {
  text-align:center;
}

CodePen: http://codepen.io/anon/pen/XKKPGO

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
1

I think all you really need is text-align:center on the parent element of the items/links/buttons

#container{
  text-align:center;
}
<div id="container">
  <a href="#">One</a>
  <a href="#">Two</a>
</div>
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
1

You could do it like this:

nav {
  text-align: center;
}

nav ul {
  display: inline-block;
}

Because the ul tag is a block level element it will stretch the full width of the parent element

More info: here