In this premium tutorial you are going to learn how to change the styling of the header dynamically, based on the class names of the sections / containers encountered while scrolling down the page.
In other words, we are going to create a whole new CSS styling for the header, and it will be enabled just when we are over sections with a specific class name.
Here is a rough version of what we are creating.
Here is a production website where I implemented this same code snippet, with great results. (In collaboration with the amazing somethingfamiliar agency)
Features include:
- Have a different logo image show for the alternate heading
- Different color for the links and pointers from the Nav Menu element
- Different color for the Nav Menu hamburger icon on mobile
- Different background color and box shadow for the heading section
- Different colors for the social icons element as well as the button element
- Option to have a specific hover style always in place (as in the demos I shared above)
- Works with either sections or containers
Let's get started!
To start with, let's add this code to our website
This is a premium tutorial. Purchase access to unlock the full tutorial.
41 Responses
Hi again Maxime!
I've implemented this on a site and it works great. The only problem I'm having is when I tried creating a hamburger menu (which also works great) when I click on the hamburger it glitches and changes to the default header and doesn't turn back.
I originally tried your tutorial: https://element.how/elementor-full-screen-split-navigation/
It worked well but again, it glitched to the default header so changed colour on the wrong background.
Then I tried using JetBlocks hamburger panel, same problem.
https://ianf58.sg-host.com/
Any ideas on how to make it not glitch like that?
Hey Millie!
Yes I see the problem indeed.
Try this. Change this line:
let observedElements = document.querySelectorAll('.elementor-section-wrap > *, .elementor > *:not(.elementor-section-wrap)');
to
let observedElements = document.querySelectorAll('.elementor-section-wrap > *, .elementor > *:not(.elementor-section-wrap, #navsection)');
Also, I am using menu anchors to click through to the next section using the arrows at the bottom of each section. However, the sticky header seems to be confusing it and adds the header above where the menu anchor is so it appears high. Any idea of how I could resolve this?
So grateful for all your help.
The anchors offset is caused by this CSS I found active on your site :
html, body {
scroll-padding-top: 75px;
}
Not certain where exactly you added it though. Hopefully you will know!
I was trying out your solution here: https://element.how/elementor-scroll-offset-menu-anchors-table-content/
But unfortunately, it still didn't work. I've removed it now. Any ideas?
Hey Millie!
I still see the CSS applied on the page... I'm not certain if it's really the cause of the problem or not, but first let's try to remove it.
It's on that element:
/* Start custom CSS for social-icons, class: .elementor-element-40595bd */
Sorry! I forgot I put it in one more place when testing. I've removed it now. Has it gone?
Yes now it has... the issue is still there. Could you please confirm that if you remove my custom code, the issue goes away? Just trying to confirm that it's related to my code, and not just an Elementor issue.
Hi Maxime,
The issue is still there, so I guess it is an issue with elementor?
Out of interest, is adding 3 headings possible via the way in this tutorial?
Thanks,
Millie
Hey Millie!
Yes seems like an Elementor issue. You can still try to use the code from that tutorial here : https://element.how/elementor-scroll-offset-menu-anchors-table-content/
Try this, in a code snippet :
Yes, you can add as many headers as you want, it will work. Just keep this in mind: You should have only one container with the ID 'header-section'.
So you would need to add all your different header containers IN that 'header-section' ID container. You will also need to rework the CSS to work as intended for each header container.
Thank you. I've added the code snippet to a new snippet. I'm getting this error.
The code snippet you are trying to save produced a fatal error on line 14:
syntax error, unexpected end of file
Thanks for all your help, sorry for being a pain!
Oups really sorry about that. It didn't copy paste as expected.
I updated my previous comment with the proper code, please try again.
That has worked perfectly! You are a genius!
Thank you so much for all your help!!
Hi Maxime,
I tried adding the HTML code to elementor >> custom code, but I got css errors in the root part of the code. Can you help me what am I doing wrong here?
Thank you,
Paveen
Or could you please provide the video through the process? That's would help me a lot.
Thank you.
Hey Paveen!
Elementor is using an old CSS validator, and it's inaccurate. It will flag errors when there are none.
The code works. If it's not working for you, maybe you added the PHP version in there? That one only works for with a php code snippet in the Code Snippet plugin. It won't work with Elementor custom code.
Really, I suggest you use the first code snippet (php version), and add it in a code snippet from the Code Snippet plugin. That's ideal.
Hi Maxime,
The PHP version works perfectly!
Thank you very much.
Hi Maxime,
is there any update for the code and flex containers, as this one seems not to be working with the new Elementor versions?
Thank you in advance
Jovan
Hey Jovan!
The code is tested working on the latest version of Elementor. Could you share your page with me so that I have a look why it might not be working as it should?
Cheers!
Hey Maxime,
The header switch is working on the way down, but not when you scroll up. Do you know why that would be?
http://whatnot.mysites.io/slide-test/
Hey Caleb!
It looks like it might be because your very first section on your page is set to 'sticky'. Try having it be normal, see if that fixes it.
Yep, that was it. Thanks!
Is it possible to apply this CSS, but to another block/section/container that is not exclusive to HEADER?
I subscribed thinking I could apply it to a container I have on the site that follows during the user's scroll (like a sticky index box), but I saw that the code contains HEADER specifications.
Greetings!
Yes it is certainly possible, as the class gets added to the <body> element.
You will need some CSS knowledge though... I don't provide CSS directly for this scenario, as you could literally have any element sticky, and the CSS would be different in every case.
Essentially, you write CSS for the alternate styling this way. Give you sticky container the ID "stickyContainer", then write like this:
that will change the background color of your element with the class 'my-element-class-name' to #333 when the alternate styling is active.
Hope this helps
Cheers!
thank you for the quick response.
I will try it on a HEADER to understand the system, and then try to apply it to the container I want.
as my CSS knowledge is limited, let's see if I can do it
Hello again 🙂
I managed to apply the tutorial to a sticky section, as was my initial idea.
I don't know if it's working exactly like the header, but it does the trick.
However, because it's a very vertical section the transition from black to white, and white to black seems too "rough".
I tried applying the transition to the CSS, but it only worked on one transition.
Is it possible to add to the CSS directly?
Or would this transition have to be applied directly in the script (which is beyond my knowledge)?
Yes you should be able to add the transition to the CSS directly. That's where it goes... the JavaScript here only changes class names on the body element. It does not directly change any styling.
I don't know what you have used exactly here, but you need something like
.element {
transition: color 0.4s, border 0.4s;
}
Or try just this, in the Advanced > Custom CSS field:
selector * {
transition: color 0.4s, border 0.4s;
}
Cheers!
Here is the link https://gifyu.com/image/SIK27
Hi Maxime.
I have a problem that I've been trying to fix for days now, but I can't find a solution.
I have a header that contains three seperat nav menus.
All three of them are black as default.
When I scroll down the page they change color from black to white when scrolling over the first container as they should, but when I scroll back to the top they keep being white. If I refresh the page they are black again. Any idea how to resolve this?
Best regards
Hey Artisan!
Could you please share your URL with me where you have this?
Thanks!
Hi Maxime! When the "alternate theme" elements are set to hidden on mobile (through the responsive panel in the elementor advanced tab), they still show up - is this because of this part here:
body.alternate-theme .alternate-theme-only {
display: block;
}
overrides the 'elementor-hidden-mobile' class? If so, can you please suggest some code to fix this?
I tried asking chatGPT and it gave me this below, but that didn't work lol:
const element = document.querySelector('#your-element-id');
if (element && !element.classList.contains('elementor-hidden-mobile')) {
element.style.display = 'block';
}
Thanks for any help you can provide! 🙂
Hey Charlene!
Try this. Below this part of the CSS:
body.alternate-theme .alternate-theme-only {
display: block;
}
Add this:
@media (max-width:767px){
body.alternate-theme .alternate-theme-only.elementor-hidden-mobile {
display: none;
}
}
Let me know if that works!
Cheers!
Thanks so much that totally works!
Unfortunately though, I just realized the styling on the drop down mega menu breaks once I add all this code in. 🙁 If I remove everything (like the whole thing - javascript, html, etc) the styling returns to normal. I've added it via my functions.php theme file, and also tried just adding it in an html widget through elementor, but the styling breaks either way.
Please send me your URL with the broken styles in place, I will have a look what might be going on here...
Hello, is there a way to modify the code to have 3 header options (on the same page) instead of 2? I though I saw a reference to tag in earlier comments, but didn't really see (or understand) how.
Thanks
Hello Maureen!
It's possible, however when / how the third header should show up?
Hi Maxime! This has been working amazing, up until today when I updated the elementor pro plugin to 3.15.1. The dropdown arrows/icons have reverted to the theme's default "red", but the text and logo and icons still color switch perfectly.
Would you happen to know how i can fix this? I've tried a few things but haven't been successful. The website is live at https://www.justforyoudayspa.com
Thanks so much!
Hey Charlene!
That's an Elementor bug... there is some CSS missing. There is no color styles applied at all to that icon, unless it's on hover.
They will probably fix it, but for now you can add that CSS:
.elementor-widget-n-menu .e-n-menu-item-title:not(.e-current) i {
color: var(--n-menu-item-title-color-normal,var(--n-menu-item-title-hover-color-fallback));
}
Cheers!
You are a fricking wizard lol! Thank you SO much for providing a quick fix in the interim! Worked perfect. Many thanks!!
Awesome Charlene, glad to know it's all working fine!