Creating a Fixed Slide-In Header and Navigation in Divi

aniaromanska

Written by Ania Romańska

Published on . Last edited on .

Fixed slide-in header and navigation in Divi

TL;DR

Divi 5 and Divi 4 have a built-in fixed header option: section settings ➜ Advanced ➜ Scroll Effects ➜ Sticky Position ➜ Stick to Top.

The slide-in header is a second section with the CSS class dl-fixed-header, hidden with position: fixed and transform: translateY(-100%).

A jQuery scroll function adds the class dl-scrolled after 150px of scrolling, and translateY(0) slides the section in with a 0.3s transition.

In Divi 5 you can skip the jQuery: two Interactions on the big header (Viewport Exit ➜ Add Attribute class dl-scrolled, Viewport Enter ➜ Remove Attribute) do the same.

The selector body:not(.et-fb) keeps the header in place inside the Visual Builder, and extra top values (32px, 46px on screens up to 782px) make room for the WordPress admin bar.

Updated for Divi 5: The slide-in header from this tutorial still works in Divi 5: I tested the CSS and the jQuery on new Divi 5 sections and on the converted free layouts. Divi 5 also lets you do it without any JavaScript, using Interactions, and both Divi 5 and Divi 4 now have a built-in Sticky Position setting for a simple fixed header.

Does Divi have a fixed header setting?

Yes. When I first wrote this post, the brand new Divi Theme Builder let us design website headers with the Divi Builder, but there was no setting (as of yet) to turn the Menu or Fullwidth Menu module into a fixed bar that always stays visible on top of the page when you scroll down. Since then Divi got a native one: open the section settings and go to Advanced tab ➜ Scroll Effects ➜ Sticky Position ➜ Stick to Top (same place in Divi 5 and Divi 4).

If you're a visual learner, here's the video version of this tutorial:

The video was recorded in Divi 4. The steps and the code are the same in Divi 5; only the place where you add a CSS class has moved (Advanced tab ➜ Attributes, see Step 1).

Making the header fixed at the top of the page

The simplest (though not really ideal) code solution is to use this bit of CSS for your header section:

position:fixed;
top:0;
width:100%;
z-index:99;

You can add this code in the Advanced tab of the section settings, in the Main Element CSS box. In Divi 5: Advanced tab ➜ CSS ➜ Module Elements ➜ Main Element; in Divi 4: Advanced tab ➜ Custom CSS ➜ Main Element. It will make your section "stick" to the top of the page. Unfortunately this only works well if your header is narrow. If your header area is big, making it fixed may not work well, as it might cover too much of the browser window, especially on smaller screens. The same goes for the Sticky Position setting.

Why use a separate slide-in section for the fixed header?

Because a big header that stays fixed covers too much of the screen. A separate, slim section that slides in only after the visitor scrolls lets you keep a large "normal" header and show a compact fixed bar with a different design.

This is what I mean:

A slim fixed header slides in from the top after scrolling past the big Divi header

Read on for the step-by-step instructions on how to recreate this effect, or download my pre-designed header layouts with a sleek slide-in navigation (at the end of this post), which you can simply import and use on your projects! You can see them in action here: Style 1, Style 2 and Style 3.

How to create a fixed slide-in header in Divi

You need two header sections, the CSS class dl-fixed-header on the second one, a few lines of CSS that hide it above the screen, and a short script that adds the class dl-scrolled after the page has been scrolled, which slides the section in.

Step 1. Create two header sections

This approach allows us to design a completely different look for each header state. Let's say you have two different logo images: a big one, used as the main logo in the centered navigation, and a horizontal logo version, which can be used for a slide-in fixed nav. Or, a different scenario, you don't need the full navigation in the fixed nav, maybe you'd just like to keep a call to action button to draw your visitor's attention. With two separate header sections the possibilities are endless!

Create each section using the Divi Builder. If you want the header on every page, build both sections in your Theme Builder header (Divi ➜ Theme Builder ➜ Add Global Header ➜ Build Global Header). Then add a custom CSS class of dl-fixed-header to the second section:

  • Divi 5: section settings ➜ Advanced tab ➜ Attributes ➜ Add Attribute ➜ CSS Class, value dl-fixed-header
  • Divi 4: section settings ➜ Advanced tab ➜ CSS ID & Classes ➜ CSS Class

We are going to hide this section with CSS and then use a simple JavaScript function to show it after the page has been scrolled by a certain number of pixels.

Step 2. Add the CSS code

Add this CSS code to Divi ➜ Theme Options ➜ General ➜ Custom CSS or to your child theme stylesheet. Note that we are setting the top position to 0. Instead of moving the section outside of the viewport with top:-100%, we are going to use the transform property with translateY, which is smoother to animate with a transition (performance-wise).

body:not(.et-fb) .et_pb_section.dl-fixed-header {
	position:fixed;
	top:0;
	width:100%;
	transform: translateY(-100%);
	transition: 0.3s transform ease-in-out;
	z-index:99;
}

The :not(.et-fb) selector makes sure our header is fixed only on the frontend and not when the Divi Builder is enabled (Divi 5 still adds the et-fb class to the body in the Visual Builder), otherwise it might be a bit hard to access the section settings and other Builder controls.

The CSS code below makes sure the fixed section is displayed correctly for logged-in users (when the WordPress admin bar is visible):

body.admin-bar:not(.et-fb) .et_pb_section.dl-fixed-header {top:32px;}

@media (max-width:782px) {
	body.admin-bar:not(.et-fb) .et_pb_section.dl-fixed-header {top:46px;}
}

@media (max-width:600px) {
	body.admin-bar:not(.et-fb) .et_pb_section.dl-fixed-header {top:0;}
}

And the final bit of code: when the header gets the CSS class dl-scrolled (which we are going to add dynamically with jQuery in a moment), this moves it back to its default position:

body:not(.et-fb) .et_pb_section.dl-fixed-header.dl-scrolled {
	transform: translateY(0);
}

Step 3. Add the JavaScript

Add this jQuery code to a Code module in your header (or on the page), or to Divi ➜ Theme Options ➜ Integration ➜ Add code to the < head > of your blog. You can also enqueue a custom JS file via your child theme. I wrote a separate blog post with detailed instructions on adding JavaScript code to Divi.

You can change the 150 value to one that matches your design. It is the number of pixels the page needs to be scrolled before the fixed header slides down.

<script>
jQuery(document).ready(function($){
	$(window).on('scroll', function(){
		var topOffset = $(window).scrollTop();
		var fixedSection = $('.dl-fixed-header');

		if (topOffset >= 150) {
			fixedSection.addClass('dl-scrolled');
		}
		else {
			fixedSection.removeClass('dl-scrolled');
		}
	}).trigger('scroll');
});
</script>

Divi 5 loads jQuery deferred (in the footer) by default, and the code is wrapped in jQuery(document).ready(), so it works in Divi 5 wherever you add it. I also added .trigger('scroll') at the end, so the fixed header shows up straight away when someone reloads the page halfway down.

And that's it! This is all the code you need to create a slide-in fixed section. Hope you'll find it useful. I'd love to see how you use it on your websites, share your headers in my Divi Lovers Facebook group!

How to show the fixed header without jQuery in Divi 5

In Divi 5 you can replace the jQuery from Step 3 with two Interactions on the big header section: Viewport Exit adds the class dl-scrolled to the fixed section, and Viewport Enter removes it. Keep the CSS from Step 2, it does the actual sliding.

  1. Open the settings of your first (big) header section and go to Advanced tab ➜ Interactions ➜ Add Interaction.
  2. Set the trigger to Viewport Exit and the effect to Add Attribute. Choose the fixed header section as the target, then enter the attribute name class and the value dl-scrolled.
  3. Add a second interaction: trigger Viewport Enter, effect Remove Attribute, the same target, name class and value dl-scrolled.
  4. Save and test it on the frontend.

The difference: the fixed header slides in when the big header has completely left the screen, instead of after a set number of pixels. For most designs that's exactly the right moment, and there's no script to maintain.

Prefer a single header that just stays on top? Use the Sticky Position setting mentioned above, or check my guide to a transparent fixed (sticky) header in Divi.

Grab the free slide-in fixed header layouts!

The free download includes three ready-made header designs, each with a standard and a fixed header section, the CSS and the jQuery, and they work in Divi 5 and Divi 4.

I created three different header designs, which you can download and use on any Divi website! Each layout includes a standard and a fixed header section with some custom styling. Check out the demo page and feel free to download the JSON files!

Free download

Fixed Slide-In Header Layouts

Three header designs with a big default header and a slim fixed header that slides in on scroll, CSS and jQuery included. Works in Divi 5 and Divi 4.

Get the free download

After the import, pick your own menu in the Menu modules and replace the sample logos. In Divi 5 the layouts are converted automatically when you import them; I tested all three, and the slide-in and the mobile menus work.

This article has been tagged with: Divi CSS, Menus & Headers

Tags

Fixed slide-in header and navigation in Divi

Table of Contents

Popular questions

How do I make the header sticky in Divi without code?

Open the header section settings and go to Advanced ➜ Scroll Effects ➜ Sticky Position, then choose Stick to Top. It works in Divi 5 and Divi 4 (since 4.6). For a different, slimmer header that appears only after scrolling, use the slide-in section from this tutorial.

Does the fixed slide-in header code work in Divi 5?

Yes. The CSS and the jQuery work in Divi 5 on new and on converted sections, because Divi 5 keeps the et_pb_section class, adds your custom class to the section, and runs code wrapped in jQuery(document).ready() even though jQuery is deferred. In Divi 5 the class goes in Advanced ➜ Attributes instead of CSS ID & Classes.

Why does my fixed header not show up in the Divi Builder?

That's on purpose. The selector body:not(.et-fb) applies the fixed position only on the frontend, so in the Visual Builder the section stays in place and you can edit it. Check the effect on the live page.

How do I change when the fixed header slides in?

Change the 150 in the jQuery code to the number of pixels the page should be scrolled first. With the Divi 5 Interactions method the header slides in when the big header has fully scrolled out of view.

About the Author

Ania Romańska

I'm a Divi expert from Poland, with over 10 years building the templates and courses that make up Divi Lover. I'm also a mom of five, which taught me a thing or two about explaining things clearly the first time.

Hey there!Sorry to interrupt,but this one's important!

Divi Lover is turning 10!
Let’s celebrate with 40% off everything.

Ten years of Divi templates, plugins and tips, and a brand-new website to celebrate it. To say thank you, every product in the shop is 40% off until Cyber Monday.

Your code

Paste it at checkout. Valid until November 30.