Search code examples
cssmenunavigationfixed

Centered and fixed navigation


I have the css below, which makes my navigation bar be centered. However, i want to add the following line "position:fixed", but if i do so, the navigation bar will no longer be centered, and will no longer have the border lines stretched all across the screen.

Any suggestions ? (Thanks !)

.navbar{
    border:1px solid #ccc;
    border-width:1px 0;
    list-style:none;
    margin:0;
    padding:0;
    text-align:center; } 

.navbar li{
    display:inline; } 

.navbar a{
    display:inline-block;
    padding:10px; }

body {
    padding-left: 0em;  padding-bottom: 0em;
    color: white;
    background-color: #708090; 
    font-family: Georgia, Times, serif}

Solution

  • HTML

    <nav>
      <ul>
        <li><a href="#">First Nav Item</a></li>
        <li><a href="#">Second Nav Item</a></li>
        <li><a href="#">Third Nav Item</a></li>
      </ul>
    </nav>
    

    CSS

    nav { 
      position: fixed;
      top:0; left:0;
      width:100%; height:40px;
      line-height:40px;
      text-align: center; }
    
    nav li { display: inline; }