Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fixed menu not working

Tags:

html

css

I want my menu to stay on its place while I scroll, but when I add the following in my CSSposition:fixed; the whole thing goes out of the place from where I want it to be.

In this jsfiddle you can see how it is now and how I want it to be. The only thing wrong with it, is that if I change the position to fixed it doesn't stay where I want it to be anymore.

<!DOCTYPE html>
<html>

<head>
    <link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
    <title>random</title>
  <!--   <link rel="stylesheet" type="text/css" href="styles/style.css"> -->

</head>

<body>
    <div id="container">
        <div id="headcontainer">
        </div>
               <div id="menu">
                <div id="logo">
                    <p>
                        Hier komt een logo
                    </p>
                </div>
                <ul>
                    <li>
                        Home
                    </li>
                    <li>
                        Over
                    </li>
                    <li>
                        Contact
                    </li>
                    <li>
                        Producte
                    </li>
                </ul>
            </div>

<div id="content">
<div class="text-box">


    </div>
    <div class="text-box">


    </div>
</div>

    </div>
</body>

</html>

<style>
    /*Global*/

    * {
        margin: 0px;
    }

    body {}

    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {}

    h1 {}

    h2 {}

    h3 {}

    h4 {}

    a {}

    img {}

    #container {
        margin-left: auto;
        margin-right: auto;
        width: 100%;

    }

    #headcontainer {
        width: 100%;
        height: 100vh;
        background-color: pink;

    }
    /* navigation */

    #menu {
        height: 100px;
        width: 100%;
        background-color: rgba(0, 0, 255, 0.1);
        max-height: 100px;
        border: 1px solid black;
        border-top: none;


    }

    #menu li {
        display: inline-block;
        text-decoration: none;
        padding-left: 20px;
        position: relative;
        padding-right: 20px;
    }

    #menu ul {
        float:right;
        height:100%;
        width: auto;
        line-height: 100px;
        margin-right:25px;
    }

    #menu ul li {

    }

    #menu ul li:hover {
        cursor:pointer;
        color: white;
    }

    #logo {
        height: 50px;
        width: auto;
        background-color: red;
        float: left;
        margin-top: 0px;
        margin-top: 30px;
        margin-left: 60px;


    }

    /*content*/
    #content {
    width:100%;
    height:1000px;
    min-height:500px;
    margin-left: auto;
    margin-right: auto;


    }

    .text-box {
    width:40%;
    height:auto;
    background-color:blue;
    min-height:100px;
    float:left;
margin-left:5%;
margin-right:5%;
margin-top:50px;
    }

    </style>

EDIT: I'm trying to achieve something like this:

http://themes.lucky-roo.com/resume-cv/berg-v1.7/HTML_Template/home-1-static-image.html

like image 258
Kevin Avatar asked Jun 03 '16 09:06

Kevin


People also ask

How to fix a menu in css?

Step 2) Add CSS: 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 to make fixed header in css?

Answer: Use CSS fixed positioning You can easily create sticky or fixed header and footer using the CSS fixed positioning. Simply apply the CSS position property with the value fixed in combination with the top and bottom property to place the element on the top or bottom of the viewport accordingly.

What is position sticky CSS?

position: sticky;A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).


6 Answers

I create a jsfiddle.

I just add those lines in #menu and it works for me:

    position : fixed;
    top: 0;

The menu doesn't move.

like image 96
Naugrim. Avatar answered Oct 07 '22 03:10

Naugrim.


Use menu inside the headercontainor and give position: fixed; fom #menu

 <!DOCTYPE html>
    <html>
    
    <head>
        <link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
        <title>random</title>
      <!--   <link rel="stylesheet" type="text/css" href="styles/style.css"> -->
    
    </head>
    
    <body>
        <div id="container">
            <div id="headcontainer">
    		
    		<div id="menu">
                    <div id="logo">
                        <p>
                            Hier komt een logo
                        </p>
                    </div>
                    <ul>
                        <li>
                            Home
                        </li>
                        <li>
                            Over
                        </li>
                        <li>
                            Contact
                        </li>
                        <li>
                            Producte
                        </li>
                    </ul>
                </div>
    			
            </div>
                   
    
    <div id="content">
    <div class="text-box">
    
    
        </div>
        <div class="text-box">
    
    
        </div>
    </div>
    
        </div>
    </body>
    
    </html>
    
    <style>
        /*Global*/
    
        * {
            margin: 0px;
        }
    
        body {}
    
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {}
    
        h1 {}
    
        h2 {}
    
        h3 {}
    
        h4 {}
    
        a {}
    
        img {}
    
        #container {
            margin-left: auto;
            margin-right: auto;
            width: 100%;
    
        }
    
        #headcontainer {
            width: 100%;
            height: 100vh;
            background-color: pink;
    
        }
        /* navigation */
    
        #menu {
                position: fixed;
            height: 100px;
            width: 100%;
            background-color: rgba(0, 0, 255, 0.1);
            max-height: 100px;
            border: 1px solid black;
            border-top: none;
    
    
        }
    
        #menu li {
            display: inline-block;
            text-decoration: none;
            padding-left: 20px;
            position: relative;
            padding-right: 20px;
        }
    
        #menu ul {
            float:right;
            height:100%;
            width: auto;
            line-height: 100px;
            margin-right:25px;
        }
    
        #menu ul li {
    
        }
    
        #menu ul li:hover {
            cursor:pointer;
            color: white;
        }
    
        #logo {
            height: 50px;
            width: auto;
            background-color: red;
            float: left;
            margin-top: 0px;
            margin-top: 30px;
            margin-left: 60px;
    
    
        }
    
        /*content*/
        #content {
        width:100%;
        height:1000px;
        min-height:500px;
        margin-left: auto;
        margin-right: auto;
    
    
        }
    
        .text-box {
        width:40%;
        height:auto;
        background-color:blue;
        min-height:100px;
        float:left;
    margin-left:5%;
    margin-right:5%;
    margin-top:50px;
        }
    
        </style>
like image 30
Mani Avatar answered Oct 07 '22 03:10

Mani


Since you are not using JQuery, You can achieve it through the simple javascript

var header             = document.querySelector('div'),
     title              = header.querySelector('ul'),
    fix_class          = 'fixnav';

function stickyScroll(e) {

  if( window.pageYOffset > 310 ) {
    title.classList.add(fix_class);
  }

  if( window.pageYOffset < 310 ) {
    title.classList.remove(fix_class);
  }
}

// Scroll handler to toggle classes.
window.addEventListener('scroll', stickyScroll, false);
<style>
    /*Global*/
    
    * {
        margin: 0px;
    }
    
    body {}
    
    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {}
    
    h1 {}

    h2 {}
    
    h3 {}
    
    h4 {}
    
    a {}
    
    img {}
    
    #container {
        margin-left: auto;
        margin-right: auto;
        width: 100%;
      
    }
    
    #headcontainer {
        width: 100%;
        height: 100vh;
        background-color: pink;

    }
    /* navigation */
    .fixnav{
      position:fixed;
      z-index:9999;
      top:0;
    }
    #menu {
        height: 100px;
        width: 100%;
        background-color: rgba(0, 0, 255, 0.1);
        max-height: 100px;
        border: 1px solid black;
        border-top: none;
        transition: 1s all;
    }
    
    #menu li {
        display: inline-block;
        text-decoration: none;
        padding-left: 20px;
        position: relative;
        padding-right: 20px;
    }
    
    #menu ul {
        float:right;
        height:100%;
        width: auto;
        line-height: 100px;
        margin-right:25px;
    }
    
    #menu ul li {

    }
    
    #menu ul li:hover {
        cursor:pointer;
        color: white;
    }
    
    #logo {
        height: 50px;
        width: auto;
        background-color: red;
        float: left;
        margin-top: 0px;
        margin-top: 30px;
        margin-left: 60px;
        

    }

    /*content*/
    #content {
	width:100%;
	height:1000px;
	min-height:500px;
	margin-left: auto;
    margin-right: auto;
    

    }

    .text-box {
	width:40%;
	height:auto;
	background-color:blue;
	min-height:100px;
	float:left;
margin-left:5%;
margin-right:5%;
margin-top:50px;
    }

    </style>
<!DOCTYPE html>
<html>

<head>
    <link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
    <title>random</title>
  <!--   <link rel="stylesheet" type="text/css" href="styles/style.css"> -->
    
</head>

<body>
    <div id="container">
        <div id="headcontainer">
        </div>
               <div id="menu">
                <div id="logo">
                    <p>
                        Hier komt een logo
                    </p>
                </div>
                <ul>
                    <li>
                        Home
                    </li>
                    <li>
                        Over
                    </li>
                    <li>
                        Contact
                    </li>
                    <li>
                        Producte
                    </li>
                </ul>
            </div>
    
<div id="content">
<div class="text-box">


	</div>
	<div class="text-box">


	</div>
</div>

    </div>
</body>

</html>

Fiddle: https://jsfiddle.net/sank8893/mvmbtcby/3/

like image 22
Sankar Avatar answered Oct 07 '22 01:10

Sankar


JS:

$(document).ready(function() {

$(window).scroll(function () {
  //if you hard code, then use console
  //.log to determine when you want the 
  //nav bar to stick.  
  console.log($(window).scrollTop())
if ($(window).scrollTop() > 100) {
  $('#nav_bar').addClass('navbar-fixed');
}
if ($(window).scrollTop() < 100) {
  $('#nav_bar').removeClass('navbar-fixed');
}
 });
});

Css:

.navbar-fixed{
  position:fixed;
    z-index:auto;
    Top:0px;
}

Since you need this css to be appended only when you have scrolled a certain height in your webpage, Using this js code you can append navbar-fixed class to menu div after scrolling a certain amount of height.

like image 34
Pushpendra Avatar answered Oct 07 '22 01:10

Pushpendra


Add the following script it will work fine

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
    $(window).scroll(function () {
        if ($(window).scrollTop() > 650) {
            $('#menu').css('position', 'fixed').css('top', '0');
        } else {
            $('#menu').css('position', 'static');
        }
    });
</script>
like image 34
Naved Ansari Avatar answered Oct 07 '22 03:10

Naved Ansari


Add these styles to your menu in css .

position: fixed
top:0

The combination of these 2 will make sure the menu stays where it has to stay : always at the top.

As @Error404 said:

the fixed elements goes out of the normal flow of the document and this is why you can position it on the top of the screen.

like image 40
jelleB Avatar answered Oct 07 '22 03:10

jelleB