CSS Center nav bar links

22,922

Use flex to it get more flexible:

.navmenu { margin: 0 auto }
.navlink { display: flex; justify-content: center }
Share:
22,922
STiGYFishh
Author by

STiGYFishh

Updated on July 09, 2022

Comments

  • STiGYFishh
    STiGYFishh almost 2 years

    Having trouble centering some text links on my nav bar, margin left and right auto aren't working and i've tried setting display types to block/inline-block to no avail. The solution is probably staring me in the face I know but I really can't find it.

    CSS:

    .navmenu {
        background-color: #1e1e1e;
        background-image: url("../images/NavBar.gif");
        background-position: top;
        border-top: 1px solid #383838;
        margin-bottom: 5px;
        z-index:105;
    }
    .navlink {
        margin-left:auto;
        margin-right: auto;
        width: 100%;
        font-size: 28px;
        text-transform: uppercase;
    }
    

    HTML:

    <div class="navmenu">
        <div class="navlink">
            <a href="index.php">Home</a>
            <a href="http://twitch.tv/" target="_blank">Twitch</a>
            <a href="http://youtube.com/" target="_blank">YouTube</a>
            <a href="http://twitter.com/" target="_blank">Twitter</a>
            <a href="http://facebook.com/" target="_blank">Facebook</a>
            <a href="teamspeak.php">Teamspeak</a>
            <a href="/forum/">Forum</a>
        </div>
    </div>
    
  • STiGYFishh
    STiGYFishh over 9 years
    Cheers, knew it would be something stupidly simple, always overthink everything.
  • STiGYFishh
    STiGYFishh over 9 years
    hmm I fixed it with text-align center but i'll keep this in mind cheers.
  • James Korden
    James Korden over 9 years
    While this will work, be aware that it may look odd when you shrink the page too thin.
  • Glenn
    Glenn over 9 years
    this seems like the right solution based on the problem to me. the page should hold together much better.
  • George
    George over 9 years
    I dont know why this was voted down. The nav bar has an image that has a fixed width and this cannot be scaled down.