Intro
Content is king when it comes to website traffic, however if your site doesn’t show well then you could be losing some of your audience or even business prospects. When I visit a website and see a fixed item done correctly it usually stands out and my impression meter of the site automatically goes up even before I read the content. Are you doing something to standout?
In this post we will run through how you can add a css fixed header to your site and gain a visual distinction. With less that 10% of all internet sites using fixed assets correctly, you can set yourself apart and allow your sites to have a different more elegant flow.
Video Walkthrough
Step 1: Locate Elements to Fix
The first thing to do is locate your target element(s) you want to set as fixed. This is very easy with a DOM inspector like Google Chrome or Firebug. To see how this is done visit the post Fast Website Layouts. Once you have located the items place them inside a div element with an id such as:
//Create a holding element <div id="my-menus">...</div>
Step 2: Set CSS Styles
Once you have your items set as fixed you will need to most likely do some manual adjustments to the content inside the fixed div element. Remember that fixed items work on an absolute layout so top, left, right and bottom are going to be very useful for getting the items positioned correctly. If your header is large you can add an opacity (transparent) layer that will allow the content to bleed through the back. This can be done by using the opacity setting in your CSS file.
//Map the correct css style to create the fixed position div#header-fixed {position:fixed; top:0px; margin:auto; z-index:100000; width:100%;} div#my-menus {background:#000; height:75px; opacity:0.8}
Step 3: Validate Assets
Once you have your header fixed in position correctly you will want to make sure that other assets like videos and other fixed elements are accounted for when scrolling happens. In order to clean up videos from YouTube you can add the following script:
Script Courtesy of MaxMorganDesing.com
//Set Videos below header $("iframe").each(function(){ var ifr_source = $(this).attr('src'); var wmode = "wmode=transparent"; if(ifr_source.indexOf('?') != -1) { var getQString = ifr_source.split('?'); var oldString = getQString[1]; var newString = getQString[0]; $(this).attr('src',newString+'?'+wmode+'&'+oldString); } else $(this).attr('src',ifr_source+'?'+wmode); });
It is also important to work with other fixed assets to make sure that they don’t collide with your new fixed header. Every site is going to be different so you will have to evaluate any other fixed assets you might have. Below is a script to adjust a WordPress plug-in called ShareBar. This script keeps my header from colliding with the ShareBar as the user scrolls. It also has additional styling that gives the bar more of a 3-D feel.
The sharebar on this site has been removed since this video…
//Fix ShareBar var $sharebar = jQuery('#sharebar'); var $sharebar_elms = jQuery('#sharebar li iframe'); var $sharebar_facebook = $sharebar_elms.get(0); jQuery($sharebar_facebook).css({"width":"60px", "height":"62px", "margin-left" : "7px" }); jQuery($sharebar).css({"margin-top" : "110px", "border-radius" : "5px", "box-shadow" : "0 8px 6px -6px #555, inset 0 0 10px #D3D3D3", "border" : "1px solid #D3D3D3"} );
Step 4: Slide Away
In order to get a portion of the header to slide away and give us a more stream-lined navigation bar there is some additional logic that I added after creating the video. This logic basically gives a whoosh effect to the site logo and social links and hides them out of site. Below is the code that makes that happen. Basically we set an event to listen for the window scroll and when its past a height of 80 we just hide the elements we don’t want to see anymore…
$(window).scroll(function () { if ($(this).scrollTop() > 80) { $("#top").addClass("grid-hide-header"); $("#header_content").hide(300); $("#search-trigger").hide(); } else { $("#top").removeClass("grid-hide-header"); $("#search-trigger").show(); $("#header_content").show(300); } });
Wrap-Up
So with a few simple adjustments of your header you can create a fixed layout that navigates easier and leaves a unique impression to those visiting your website! Hope you enjoyed this article on setting up the fixed header.
What do you think?
Have you used fixed assets before?
95 thoughts on “Simple CSS Fixed Header”
Comments are closed.
Nice. I primary use the fixed position bar at the bottom to display my latest special and affiliate links.
Good to hear from you! ;)
Yep, I think more people are realizing that proper placement on a site really gives them the edge they need keep important content in the users view.
Thanks for touching base via the comments Terence!
I love this tutorial, thank you very much.
Excellent! Thanks for touching base Juan… I hope do some more like this in the future.
Hi Cory,
Really amazed.
How can this type of fixed header affixed in blogger or wordpress site like my site (perfectnewgadgets.blogspot.com)
I’m a new comer with just simple idea. So, please give me some specific knowhow, if possible.
And, please, don’t laugh at my small and new site. It needs many scrutiny and refurbishment.
Hi Cory,
Really amazed.
How can this type of fixed header affixed in blogger or wordpress site like my site?
I’m a new comer with just simple idea. So, please give me some specific knowhow, if possible.
And, please, don’t laugh at my small and new site. It needs many scrutiny and refurbishment and many many qualitative contents also like yours.
Thanks in advance.
Hey Jewel,
Thanks much! With Blogger or WordPress.com I’m not 100% sure if they expose access to your templates and PHP files. If they do then it should be pretty simple to add some css to those files. However if they don’t then you may not get the customization’s your looking for… You can easily take the source on this article and add it to your site, if they expose file access…
Hope that help!
Thanks Cory,
Blogger permits to add CSS to any blogs template. Unfortunately I’m unable to add any CSS to my blog. Maybe it needs assistance of a pro or maybe someday I’ll do it myself with proper knowledge.
Thanks again for everything.
Hey Cory, BTW my site is now using this little trick you posted here, thanks a ton bro.
Very nice Juan, I like it! The opacity looks really smooth. The forums area of the site is especially going to benefit when someone comes across a large thread. Excellent…
So easy! Such a good example! You’re AWESOME!
Really informative video and related text tutorial! Very helpful.
I’m not sure where to trouble shoot my issue: body elements partially hidden under the newly fixed header. Is there a way to “bump” content down so that they are clear of any fixed (in my case on top of page) elements?
Thanks!
Gregg
Hey Gregg,
Thxs for the feedback! Not 100% sure if I understand the issue, but I’ll give it a shot. On the body tag you should be able to set style=’margin-top:30px’ or something like that, you could also apply this style to a div tag that comes right after the body tag. This would help push any content initially below the fix element.
Hope that helps!
Hey I’m new to html- I tried using the tutorial, but could only get the header to stay fixed, and then the would keep moving. Any idea what I can do?
Thanks,
Mat
Hey Mat,
I checked out your site, it looks like the header is staying fixed correctly, at least in Chrome…
hey ya i got it to work after some fiddling-thanks
video is flows the fixed header.How to apply the above javascript
For video overlays you can check out the video in the post source code, also you might have to do a google search for wmode=transparent to double check the various settings for the video player your using…
Hey Cory,
1st: Very cool tip eh
2nd: I’ve been trying to set this up on my mobile site to fix the header section. Worked but the rest of the content [div’s] moved up behind the header logo. I see your javascript (above) says iframe, so assuming here (don’t like to assume eh lol) it is for video iframes.. and not content.. Would you know how I could fix it and push the page back down.. I really like the idea to keep my logo in front of the user.
Thanks eh
Take care,
Al
Hey Cory,
I read though the above again and must of missed it the first time but tried the suggestion you gave Gregg (style=’margin-top:30px’) and wouldn’t work so I tried padding-top:184px and that worked. Still needs a bit of tweaking and probably won’t use it on my mobile site (logo’s a bit big at 184px) but I will use it on a couple demo sites..
Thanks again.
Take care,
Al
Hey Al,
Thanks for touching base! I see you got it going that’s great! As far as your original question on the iframe that is only for the video as the majority of most embeds (at least from the big guys, youtube, vimeo) mainly use the iframe. If your doing a responsive design site, you might check out one of these tools as they can help you with multi-platforms…
Cheers!
Hey!
Landed here while googling a fix for my problem. My site uses the method shown, but it breaks on the iPhone—specifically, if you try to zoom in, the header doesn’t zoom and everything gets all wonky.
Any ideas?
Thanks!
Hey Shawn,
Thanks for stopping by…
You might try this workaround. You could also maybe attach an event to the window zoom and monitor any css changes to the items in question when a zoom occurs…
Hope that gives you some ideas…
Cheers!
Excellent! Thanks so much!
Love the tutorial I have used it for my new design over at:
http://www.listacker.com – Social Bookmarking!
P.S Your shareoholic social bar does not appear to be working?
Thanks
Very nice walk-through! Noticed a typo. In step 3 (Validate Assets), it says
Script Courtesy of MaxMorganDesing.com
Should be design, not desing. And that link has a double http://.
Thanks for the great tutorial!
Very nice walk-through! Noticed a typo. In step 3 (Validate Assets), it says
Script Courtesy of MaxMorganDesing.com
Should be design, not desing. And that link has a double http://.
Thanks for the great tutorial!
Hrm. This comment form had an error on submittal, and said try again. Then said duplicate comment. Not sure if it went through. Sorry if it did.
It’s so easy to integrate. I thank you for this. I just improved my design.
Hey Kaiser!
Thanks for dropping by… Glad you got thinks going!
i’m trying this effect and my problem is that the fixed div overlaps with the window scrollbars in IE. i works ok with firefox and chrome though
how can i remedy this ?
here is the page i am working on: http://www.cjmagowan.com/home
Hey CJ,
Thanks for stopping by! Nice site BTW… I’m not sure if I see any issues with your site, maybe shoot me a screen shoot of what issue your seeing?
Cheers!
hello Cory,
It’s working fine now.. the problem that is cause by the css position property of the to “absolute”. now it’s fixed and following your tutorial is great..
thanks
Hello!
Your tutorial was good, but it didn’t solve my problem. All of my content that’s supposed to be BELOW my header and nav bar automatically jumps up behind it, so half of my content is hidden.
Hey Samantha,
Thanks for stopping by the site! You might have to do some manual adjustments, the code won’t be a one size fits all in some cases. You might want to double check the parent containers position and the z-index. Sometimes it takes a bit of fiddling to get it just right… Hope that helps a bit…
Cheers!
Thanks for the great tutorial. I applied it to TheWeeklyRoundup.com
Quick Question Thought.
Where do we insert the Javascript in order to have videos hide behind the header.
I.e. which file do I modify is it header.php or the stylesheet?
Hey Miguel,
For the JavaScript it will need to go into the header.php file.
Hope that helps!
Nice blog here! Additionally your website a lot up fast! What host are you the use of? Can I get your associate hyperlink on your host? I wish my site loaded up as fast as yours lol
Thanks. How do redirect all links (e.g. from top navigation menu) to target the content area?
Fantastic article and video my friend, I needed to get it implemented for a client of mine and the way you did it is just beautiful! Just a fantastic job!
Hey Michael,
Thanks for stopping by! Glad you enjoyed the article :)
hi there,
nice.
how does this work on ipad? older ios version?
does the header remain as fixed was not working
as a supported then?
thanks
bo
Hey Bo,
Yes I just tried on my ipad gen 1 and it looks to be working well :)
Cheers~
Good work, Cory! I am going to implement this for my new blog.
hello!
thanks for the tutorial!
i have a problem with my fixed header.. the code not preview the actual size of the image. i want the header stay over along the top of the website, but it turns to a short header, no matter how long i have resize the header’s image, it keep stay short.
please check my blog’s header at agrassflower.blogspot.com if you can’t understand my bad english >. <
sorry but please help.
thankyou!
Try this instead:
< div style="display:scroll;position:fixed;z-index:2000; top: 0; margin:auto; overflow: hidden;width:1100px;"...
Cheers~
Hey Cory,
Nice and easy tutorial mate. For those on WordPress that don’t want to fiddle with the code, there’s a free WordPress plugin called Sticky Header http://wordpress.org/plugins/sticky-header/
Try it out, tell me what you think.
Hey Dragan,
Thanks for stopping by… Nice plugin! I’ll give it a try :)
thanks. lapping of a div over fixed header in my blog SOLVED !!
Awesome Hadeji! Glad you got it going…
Hi! I’ve tried this whole process probably three times now (and I’ve searched other websites) but I can’t seem to integrate this into my html. Is there any way you can help me?
Sure shoot me the URL of your website…
when i am using position fixed other div elements are going under it and i can’t use top for other elements as website doesn’t stays responsive
Hey Nishant,
It looks like the theme your using will dynamically add a class called .tc-header when the browser is a specific width using a css query rule. You will have to located that class and play around with the media queries to validate you get the desired results with a fixed header…
Hi Cory,
Thanks for the tutorial on fixing the header. I have used your suggestion to fix the header and it works.Thanks ! … the header is fixed…. but unfortunately, the top of my contents page is then “hidden” under the fixed header. (I.e when I go to my web page – I can’t see parts/top of my contents page)
I then googled for answers to fix this — some of which suggested that to put either a top margin or top padding for the contents page.
I used top padding but the minute I do this I loose my fixed header … I can only use one of the otehr but not both.
I am new in this … hope I explained correctly/clearly enough.
Thanks for your help in advance.
Cheers,
Ying Chun
Hey Ying,
Thanks for stopping by… If you want to send me your url I’ll have a look at the issue and see if there is a solution…
Hi Cory,
Many thanks for your help ! It worked !!!
I was using the margin-top and padding-top codes, but as I mentioned, once I put the CSS codes for margin-top /padding-top, the header becomes “unfixed” – and vice versa. Did one at a time as I wanted to see effects of each set of codes … ;-)
So, I finally added both the codes (for fixing top header and the margin-top) at the same – and bingo ! I got what I wanted !!!
Probably the sequence of which code gets added first shouldn’t matter ? — but in my case, it did ! I am new and I can’t explain ! But it worked !!! Thanks for your help, Cory !!!
And … THANKS again for the codes !!!!
With much appreciation !
Ying Chun
Hi Cory,
Great work with your tutorial and snippets! This looks like my site could use this fixed header, but I can’t find the correct place to implement your idea in to my page-default-template.php and also my main.css files. Can you help?
For instance – on the page-default-template.php it looks like the code would nested outside of this code…
And for the main.css file I am not sure how to integrate with the current code…
Appreciated!
Hey Matt,
Honestly it is pretty tricky to get it going and does take quite a bit of playing around. I would user “border: 1px solid red” around each of your items to locate them on the page. Then its a matter of playing with the location elements, “top right bottom left” and the box elements “margin padding”, once you get those located and formatted its a matter of moving the elements around until they fit.
Hope that helps!
I’ll keep playing with the placements. This site is not a WP site and the code is a mesh of languages (and about 8 years old), so it is a bit tougher to work with.
Thanks for the border idea though.
Regards,
Matt
Whoops! Looks like the page-default-template.php code is missing.
Let’s try again…
<?/**/?>
I tryed fixing a text on top of page, it works in chrome on desktop and android tablet, BUT
it wont work on android webview (android 2.3.3)
is there a work around that ?
I tryed fixing a text on top of page, it works in chrome on desktop and android tablet, BUT
it wont work on android webview (android 2.3.3) or in the standard browser.
is there a work around that ?
it works on a phone with newer version of android.
I am generating the grid using div and span tags but i want to set the fixed header for my grid how can i do it. my grid values will be generating dynamically.
Hey Ganesh,
I’m not 100% sure how to go about it with that exact approach…
Hi Corey– in Chrome you lose your tooltip-like search box as you scroll down; just thought you’d like to know :)
Hey Veer,
Thanks for the headsup! I’ll get a fix for it… Cheers~
Hey Corey
Could you take a look at my test page for fixing the header on my site by chance ? http://Www.dvdorchard.com.au/comedy-dvds.asp is the page in question. The div will not be on the page if it’s being viewed from a mobile device.
I have a div of header-fixed around the header, not hard to tell what this on the page.
I have css for the div as per your suggestions, but no fixed header….
Hoping you can help. Ideally I’ d like to do a similar thing to your site, fix the entire header with opacity of 0.8 or similar but have my search bar, the red bit, not opaque.
Waiting with baited breath, have tried a few solutions to date, none if which have worked, very frustrating.
Thanks :)
Hi again,
OK, so it’s working on that page now, BUT
My issue is with page widths. I have a min width of 990px and max width of 1320px sitewide.
Fixing the header appears to throw out the min and max widths,the header fixes itself to a smaller width than the body.
Does anyone have any ideas to help please ?
Hi Richard,
Sorry for the delayed response. It looks like it works quite well… I’m not 100% sure what issue visually your trying to fix, when re-sizing the window it looks like it works, can you provide a bit more detail?
Cheers~
What a great start! Just enough to get us going and exploring. Thanks!
Now, when are you going to show us the less-simple technique that whisks away the upper part of your header and scoots the menu bar to the top?
Hey Bob,
I forgot about the whisker :) I’ll have to dig up the code and update the post… Thanks for the reminder!
Hello cory
Great tutorial
As i can see now in your site only navigation stays on top and the logo disappear it would be great if u could tell how to do that
Thamks
Hi Arpit,
I went ahead and added Step 4 to the post, it should have the whoosh away logic :)
Hi, I was wondering if there was a way to adapt what you are using here so that it would only remain fixed in certain instances. In my case I want it to revert back to a non fixed position if the web page is scaled under a certain pixel size. I am using a responsive theme and when viewed on a phone for instance the header is taking up about 50% of the screen
Hey James,
You can! Just use CSS media queries with the min-width setting and you can toggle how a CSS style class is rendered based on the viewport. Here is a quick overview…
Cheers~
Hi Cory,
Thanks for the nice simple script it worked nicely, just not sure how to implement step 4. im using this for templated website, ive already got the fixed-header done correctly in header.tpl and style.css, but Not too sure about were to implement step4 if you can help out thanks.
Hey Eric,
No Problem. Basically take the entire source in step 4 and place it in your script tag that is in your head. Then in the first part of the if statement you will need to pin-point the items you want to hide via a DOM inspector. In the second part of the if statement you will pretty much use the same elements that you hid and just show them them using the show() method… The > 80 is used to trigger how far you have scrolled…
Cheers~
i’ve create a plugin just for this purpose, you can customize it in any way you want, take a look here on my github https://github.com/oussamaelgoumri/fixed_header
i willl apreciate any feedback
Looks Good! Thxs for the github resource…
Cheers~
thanks for this code,
nice,
thanks for code
The code is great, thanks!
Do you by any chance know how to stop your content from moving under the header? I have added a margin to the top of my content wrapper which works when you first open the site up however when scrolling everything moves underneath. This is mainly causing problems as it is a one page site with page anchors, when a link is clicked its all moving under my header!
Remove position: fixed;
Very helping and thank you so much for best tutorials.
This is what, I was looking for hours, got it now!
what type of hour and for what?
I like the valuable info you provide to your articles.
I will bookmark your blog and test once more right here regularly.
I am slightly sure I will be told lots of new stuff right right here!
Best of luck for the next!
Thanks Huey~
Worked like a charm, thanks for taking away my headache!
no problem
I got ths website frm my friend who topld me about this web page and at the moment
this time I am browsing ths web site and reading very informative articles orr revieas at ths time.
That’s really great work. I am trying to implement your method into my website but can we make it purely CSS like this one http://codeconvey.com/create-simple-pur-css-sticky-header/ and also add some animation so it gets resize when scroll. Thanks
If you are interested in topic: how to earn online xanax reviews – you should read
about Bucksflooder first
Thanks..s