0

How can I horizontally and vertically center nav listed in a ul list on a page?

Below is the Html and CSS code

.nav{
    width: 100%;
    height: 100%;
    position: fixed;
    background-color: white;
    overflow: hidden;

}

.menu a{
    padding: 30px;
    color: black;
    text-align: center;
    left: 0;
    margin:  0 auto;
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
}
<nav class="nav">
            <ul class="menu">
                <li><a href="#">Google Store</a></li>
                <li><a href="#">Google Search</a> </li>
                <li><a href="#">Spotify</a></li>
                <li><a href="#">Spotify Blog</a></li>
                <li><a href="#">Squarespace</a></li>
                <li><a href="#">Other work</a></li>
            </ul>
        </nav>

I tried aligning the items by using the CSS flex method.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
iamamruta
  • 9
  • 2

1 Answers1

1

You've got to apply the flexbox properties to the container that holds the items, not the items themselves.

.nav {
    width: 100%;
    height: 100%;
    position: fixed;
    background-color: white;
    display: flex;
    align-items: center; 
    justify-content: center;  
}

.menu {
    list-style-type: none;
    padding: 0;
}

.menu a {
    padding: 30px;
    color: black;
    text-align: center;
    text-decoration: none;
}
<nav class="nav">
            <ul class="menu">
                <li><a href="#">Google Store</a></li>
                <li><a href="#">Google Search</a> </li>
                <li><a href="#">Spotify</a></li>
                <li><a href="#">Spotify Blog</a></li>
                <li><a href="#">Squarespace</a></li>
                <li><a href="#">Other work</a></li>
            </ul>
        </nav>

This should center your ul both horizontally and vertically within your .nav container.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
QueryKiller
  • 209
  • 6