Search code examples
htmlcsstwitter-bootstrapsidebar

Stuck/Sticky side Navigation


How do I get my sidemenu to remain stuck so the navigation tabs always remain on top and don't scroll with the page

How do I get the buttons to change background color on hover? The issue is that only the text changes background color or only a small portion

THE CODEPEN

    <head>
    <title>Århus Gastronomi</title>

    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="frontpage" class="content">
    <div id="sidebar">
        <ul id="navbar">
            <li class="navitem"><a href="#">Kat</a></li>
            <li class="navitem"><a href="#">Kat</a></li>
            <li class="navitem"><a href="#">Kat</a></li>
            <li class="navitem"><a href="#">Kat</a></li>
        </ul>
    </div>

    <p>Another item</p>
</div>
<div id="page2" class="content">


    <p>This is page 2</p>

</div>
</body>

Solution

  • Add position: fixed; to your #sidebar

    You may also want to add this:

    .content {margin-left: 10%;}
    

    And restructure your html like so, with the nav outside of the first content section::

    <body>
    <div id="sidebar">
        <ul id="navbar">
            <li class="navitem"><a href="#">Kat</a></li>
            <li class="navitem"><a href="#">Kat</a></li>
            <li class="navitem"><a href="#">Kat</a></li>
            <li class="navitem"><a href="#">Kat</a></li>
        </ul>
    </div>
    <div id="frontpage" class="content">.......
    

    This will make it so that your content isn't behind the nav you have.