Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content div going over fixed navbar

Tags:

I've got a stylesheet where the intention is to have a fixed navbar which stays at the top of the screen no matter how far you scroll down. For this I've just used position:fixed; - but when I actually scroll down, the #content div overrides it and goes straight over the top (so the navbar stays at the top of the page but is underneath the content div.)

I haven't done any serious CSS coding in years, so I'm a bit rusty - it's probably a very simple solution, so apologies for being so trivial!

style.css

body {
    margin:0;
    background:#eeeeee;
}

#navbar {
    background-color:#990000;
    position:fixed;
    width:100%;
    height:50px;
    text-align:center;
    vertical-align:middle;
    line-height:50px;
    top:0px;
}

#navbar a {
    color:#fff;
}

#content {
    background:#eeeeee;
    margin-top:50px;
    width:100%;
}

#feed {
    background: #fff;
    position:absolute;
    left:22%;
    width:776px;
}

Pages are structured like this:

<body>
    <div id="navbar"><?php include core/navbar.php; ?></div>
    <div id="content">
        <div id="feed">
            CONTENT
        </div>
    </div>
</body>  
like image 843
CuriousCabbage Avatar asked Dec 30 '13 13:12

CuriousCabbage


People also ask

How do I fix a navbar top to fixed?

To create a fixed top menu, use position:fixed and top:0 . Note that the fixed menu will overlay your other content. To fix this, add a margin-top (to the content) that is equal or larger than the height of your menu.

How do I add content to my navigation bar?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How do I get navbar under header?

Locate the section of the CSS file controlling the navigation bar. Add the following line of code to this section: clear:both; This moves the element below any floated object above it, ensuring that the navigation bar will appear below, and not to the side, of your header.


2 Answers

In order to fix this you need the property z-indexdefined by W3 that specify the level of the element. Try this:

#navbar {
  background-color:#990000;
  position:fixed;  
  z-index:1; /*Add this*/
  width:100%;
  height:50px;
  text-align:center;
  vertical-align:middle;
  line-height:50px;
  top:0px;
}
like image 51
DaniP Avatar answered Sep 27 '22 21:09

DaniP


If you're using Bootstrap 4, the .navbar background is clear and can be covered by some elements. Setting the z-index won't work. Use a background color, e.g., .bg-white.

<nav class="navbar fixed-top navbar-expand-sm navbar-light bg-white">
like image 32
LWRMS Avatar answered Sep 25 '22 21:09

LWRMS