search

Home  >  Q&A  >  body text

How to center navigation on page using ul list?

<p>如何在页面上水平和垂直居中ul列表中的导航?</p> <p>以下是HTML和CSS代码</p> <p><br /></p> <pre class="brush:css;toolbar:false;">.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; }</pre> <pre class="brush:html;toolbar:false;"><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></pre> <p><br /></p> <p>我尝试使用CSS的flex方法来对齐项目。</p>
P粉052686710P粉052686710502 days ago536

reply all(1)I'll reply

  • P粉642436282

    P粉6424362822023-08-14 00:40:06

    You must apply flexbox properties to the container containing the item, not the item itself.

    .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 will center your ul horizontally and vertically in the .nav container.

    reply
    0
  • Cancelreply