Fixed headers are a popular trend in web design, and adding one to your site can be as easy as adding a position: fixed line to your CSS, but often it’s a little more complicated than that. Some other styling factors you’ll need to consider are top, width, and z-index. It’s important that you set top to 0 if you want the header to be fixed to the very top of the page. Additionally, you’ll probably want to make the width of the header 100% if it isn’t already — the fixed header might look a little strange as you scroll down the page if it doesn’t take up with entire width of the screen. You should also definitely add a high z-index to ensure that the header scrolls over all of the content below it, not underneath it.
Here’s an example of what the typical CSS code might look like to make a header appear to be fixed.
- position: fixed;
- top: 0;
- width: 100%;
- z-index: 10000;
- var n = $("nav");
- var height = $('#top').height();