How to create an animated sticky header, with CSS3 and jQuery

Trends come and trends go. The ones that stick around the longest do so because they solve a particular problem. A trend that’s popular right now for that very reason, is sticky elements; elements that behave normally until we scroll, and then maintain their presence on the page somehow.

The trend started with sidebars, but where it’s really grown in popularity is headers. Why? Because headers tend to contain navigation, and persistent navigation is popular with users.

In this tutorial we’ll create a header that sticks to the top of the viewport, but so that it doesn’t interfere with the content, we’re going to minimize it when the user scrolls down the page.

Here’s what it’s going to look like when we’re done:

If you’d like to follow along with the code, you can download it here.



The HTML for our example is really simple, all we need is an h1 inside a header. Below that we have an image to force the page to scroll so that we can test the effect.

<header><h1>Sticky Header</h1></header>
<img src="large-image.jpg" width="782" height="2000" alt="Big Image" />


The jQuery

CSS transitions are the best way of handling the animation portion of our sticky header. All we’re using jQuery for is detecting the scroll position of the window.

When the scroll position of the window is greater than 1—meaning that the user has scrolled downwards—then we want to add the class ‘sticky’ to the header; otherwise we want to remove it (if it’s there).

This means we’ll be able to style the header based on whether the ‘sticky’ class is applied.

$(window).scroll(function() {
if ($(this).scrollTop() > 1){  

The important thing to note is that using jQuery in this way degrades gracefully; if JavaScript is disabled, the navigation will still work, the header will simply be styled in the non-sticky default state.



Our CSS is used to style the two different states, the default state, and the ‘sticky’ state; and to transition between the two states.

To start with, let’s add some simple styles that improve the look of the header:

  position: fixed;
  width: 100%;
  text-align: center;
  font-size: 72px;
  line-height: 108px;
  height: 108px;
  background: #335C7D;
  color: #fff;
  font-family: 'PT Sans', sans-serif;

Now for the fun part: when the user scrolls down, the ‘sticky’ class will be applied, and we can now style the header differently to reflect that new priority on the page. We also set the position to fixed, so that we’re not changing positioning mid-scroll.

There are several things we want to do: first, we want to change the size so that it uses up less screen space; we also want to change the color and align to the left so that visually it doesn’t interfere too much:

header.sticky {
  font-size: 24px;
  line-height: 48px;
  height: 48px;
  background: #efc47D;
  text-align: left;
  padding-left: 20px;

Naturally, what you do here will depend on the design you’re trying to achieve. You can do just about anything you like.

If you test this now, you’ll see that the header changes as soon as we scroll down.

Now, to animate the change, all we need to do is set a transition on the header, like so:

transition: all 0.4s ease;



Creating this animated header with CSS3 properties and toggling the class with jQuery is extremely simple and adds a ton of UX goodness to your site design.

What’s more, the code degrades gracefully, so there really is no downside to the implementation.

  • indianwebdesigner

    not Campatible with IE8 :(

    • Web Design Essex

      Retire IE8

      • Krik Batner

        There is no other option. I’ll never understand the thought process behind “Provide the exact same user experience in IE8.” I’m all for making sure that my website works, looks consistent, and is fully functional in IE8, however if I don’t have my transitions or rounded corners I’ll be able to make it.

        Everyone’s entitled to their own opinion, though, but if you want fancy stuff you should upgrade your browser. I’m going to give you a working copy for IE8, but if you want the extra frills you’re gonna need to upgrade.

        Now that Windows XP has officially been retired there’s no excuse to not have anything less than IE9 now.

      • indianwebdesigner

        Yes I agree with you krik Batner, we always do that same stuff from years. yes i have an option for old clients. Recently we made that fixed header with some little bit jquery and css3 Transitions CSS3 not for compatibility.
        Thanks & Regards
        Team Zanetine

    • Erwin Heiser

      Degrades nicely in IE8 so I fail to see the issue. Besides, there’s no excuse anymore to keep considering parity for IE8 – Win XP is officially retired.

    • Jasper

      who cares? Upgrade your browser and remove yourself from 2003.

    • djovic

      people like you are killing the web.

  • Anubhav

    The content jumps suddenly when (after scrolling 1px) the header acquires a ‘fixed’ position.

    • Fixed

      Yeah, definitely no reason to only apply fixed on the scroll. Have it there always, apple a margin or a “top” to the content that matches the size of the bar and shrink it gracefully.

      • Benjie — WebdesignerDepot

        Thanks for pointing that out, we’ve corrected it in the code.

  • tjedison

    How could this approach be modified to make a table header row sticky? Or a div that could act as a table header row?

    • EzequielBruni

      That’s actually a really good idea… especially where very vertically-long tables are concerned.

    • Jason McGovern

      What reasoning might be behind using a div as a table header row over using a caption or th?

      • tjedison

        None at all. I just left it out there in case there was no way to do it with a thead or th, which would be preferable.

        So far, I can only do this for Safari with position:sticky

    • Benjie — WebdesignerDepot

      I haven’t tested it, but you should be able to modify the th’s display property to remove it from the DOM hierarchy, and then you can position it just as you would a div.

  • Thiago Dutra

    var top = 0;
    $(window).scroll(function() {
    if ($(this).scrollTop() > 1 && top != 1){
    top = 1;
    else if($(this).scrollTop() <= 1 && top != 0){
    top = 0;

    • Ionut Costica

      Though you should first check the “top” variable in the if conditions, so it fails early, without going into the scrollTop function if not necessary

  • Dylan

    You’re doing a lookup of “header” once every time the scroll event is fired. You should assign the element an ID, do the lookup once, then assign it to a variable.

  • Hết Giấy


  • MindLogics

    Amazing idea, good stuff but some compatibility issues exists.

  • Roshane Ingram


  • Dmitry Valuev

    very cool

  • Ardream – FreeWebTrends

    jQuery and CSS3 are awesome. But the problem is the browser support for these technologies and you can’t decide the browsers of your visitors. By the way, this is a nice feature.

  • BrendonBrown

    Lovely and simple, thanks!

  • BrendonBrown

    You probably want to use background images, and have your new one applied (or previous one made to fit) with the sticky class.

  • designcouch

    use a jquery if statement that also gets delayed by .4 seconds (the time the animation in the above-defined CSS takes to complete)

  • designcouch

    Changing alignment is kind of dumb – and it visually interrupts an otherwise smooth animation. Otherwise, this is pretty decent (if a little basic).

  • john

    not sure where i’m supposed to be adding all these different lines of code. I’m using a wordpress theme and am relatively new..
    any help?

  • Arturo

    Thanks very helpful

  • Rudderless

    This is a great tutorial. However, I can’t work out how to make it work for a div id rather than an HTML5 element – for example $(“div id = ‘logo'”).addClass(“sticky”); instead of $(‘header’).addClass(“sticky”); – any ideas anyone>

    • Chris


  • Barton Grennan

    Hi Sara,

    Loving playing around with this.

    I’m a complete jQuery novice though.

    Could you by any chance explain to mt how I would prevent your code conflicting with other jQuery?

  • RocaPress

    Great tutorial! Thanks a lot :-)

  • Joz

    great reply sirji. best not to take offense.

  • Chris

    I can’t get this to work with https in Chrome. The browser tells me that the site is running a script from an unauthenticated source. I have to disable https to get the jquery to work. Thoughts?

  • Ev

    simple to learn! thanks

  • Rex

    lol easier than i thought. Thanks

  • Kamal

    Really a nice article!!

  • Kareem Hackett

    Where does the transition go? Under header or header.sticky?

    • Tommy Oddo

      I had a tough time finding it too… look under the demo near the top of this page; you’ll see a link to download the code (“…download it here.”). Open the html file, and you will see that it is assigned to the “header” element in the css.

  • Axl Roze

    thanks a lot

  • krut


  • Ashish Phadale

    Thanks a lot SARA. Really helpfull. Cheers

  • Daryl W Baker

    Thanks so much for this, sooo much easier than dealing with bootstrap or any of the other fixed top navs and way more control in css!

  • Daryl W Baker

    If they run IE8, then they don’t get modern web design or any things like this just static and old as expected like their web browser.. that’s not saying you cannot try but to expect support on a browser like that is unreal.

    I do however feel bad for you I share the pain as I had to design a site for IE5.5, client didn’t want to understand the face they need to upgrade!

    Now i just force browser happy addon to make them upgrade :)

  • Fe Raio

    Hey guys, I’m not familiar with javascript and I’m using this onepage-scroll ( on my project.
    There’s some way to combine this? I’ve tried to put but it’s not working :(

  • sancoLgates

    Great stuff…

  • adjj

    There is a huge problem with this process. I have faced the problem while creating my website. I have 5-6 div elements in the header. Now if I zoom the page to around 200% then half the header is visible. the rest of the body can be scrolled sideways to view the whole content but the header does not scroll sideways. so if someone zooms the page, he can see only half the header or maybe even less if someone zooms to 300%.

  • Jorge Diaz

    Hey everyone here, I know this is an old post but I’m having trouble with a website, I see that in this example the change is from the start but the web I’m doing it you need to do like 2 scrolls for the header to change, is it possible to change this?