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

Default avatar.
May 08, 2014
How to create an animated sticky header, with CSS3 and jQuery.

thumbnailTrends 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

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){ 
 $('header').addClass("sticky");
 }
 else{
 $('header').removeClass("sticky");
 }
});

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.

The CSS

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:

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;

Conclusion

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.

Sara Vieira

Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website.

Read Next

3 Essential Design Trends, November 2024

Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…

20 Best New Websites, October 2024

Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…

Exciting New Tools for Designers, October 2024

We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…

15 Best New Fonts, September 2024

Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…

3 Essential Design Trends, October 2024

This article is brought to you by Constantino, a renowned company offering premium and affordable website design You…

A Beginner’s Guide to Using BlueSky for Business Success

In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…

The Importance of Title Tags: Tips and Tricks to Optimize for SEO

When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…

20 Best New Websites, September 2024

We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…

Exciting New Tools for Designers, September 2024

This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…

3 Essential Design Trends, September 2024

September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…

Crafting Personalized Experiences with AI

Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…

15 Best New Fonts, August 2024

Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…