Elementor Header Change Color On Scroll Dynamically

Elementor Header Change Color On Scroll Dynamically

Table of Contents

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.

Access tutorial

  • Gain Access to This TutorialiUnlock complete access to the current tutorial: Elementor Header Change Color On Scroll Dynamically
  • Future UpdatesiYou will get access to all future updates to this tutorial.
  • Enjoy Unlimited UsageiUse on as many of your own sites or your clients sites as you wish.

    Note that reselling or redistributing is not permitted.
$29* Purchase access

Access everything

  • Unlock every premium tutorial on Element.howiGet access to the entire library of premium tutorials on Element.how
    Preview premium tutorials
  • Get access to the CSS course for Elementor usersiAccess the complete 14 HTML chapters, 30 CSS chapters and 7 Elementor Projects.Learn more
  • Simple CSS Grid For ElementoriAn Elementor Addon to Create Awesome Grid Layouts in a Single Click for Containers, Galleries and Loop Grid. Learn more
  • free extra: ShapeDividers.com Premium AccessiLifetime Premium Access to ShapeDividers.comVisit ShapeDividers.com
  • 30 day money backiNo questions asked money back. Not what you expected? Get a refund.
  • One-time payment of only $299iNo hidden fees or subscriptions.

    Sales taxes added where applicable.
  • Limited supportiGet help when you need it. Support includes getting things working as intended.

    Support excludes customization work.
  • Lifetime access to everything Element.howiThe price reflects what is currently available on Element.how. All future updates are included, but none are promised. You pay for what is available now, and the rest is a sweet extra.

    I will say that it is definitely my intention to keep adding tutorials to Element.how.
  • Launch offer discounts!iThis all inclusive package is freshly released and to celebrate, there is a special discount available:

    10% for new customers with coupon code LAUNCH10

    20% for existing customers with coupon code LAUNCH20

    30% for CSS Course customers with coupon code LAUNCH30_CSS_STUDENT

    Available for a limited time only.
$299* Purchase All Access
* All prices are USD. Applicable taxes will be charged at checkout. Have a question? email me

Element.how also provides premium tutorials showing awesome advanced designs, check them out here.

Looking for something else? Search across 2445 Elements right here:

Checkout the Elementor Addon Finder directly


37 Responses

  1. 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.


    Any ideas on how to make it not glitch like that?

    1. 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)');


      let observedElements = document.querySelectorAll('.elementor-section-wrap > *, .elementor > *:not(.elementor-section-wrap, #navsection)');

  2. 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.

    1. 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!

      1. 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.

      2. It's on that element:

        /* Start custom CSS for social-icons, class: .elementor-element-40595bd */

      3. Sorry! I forgot I put it in one more place when testing. I've removed it now. Has it gone?

      4. 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.

      5. 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?


      6. 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 :

        add_action( 'wp_footer', function() {
        if ( ! defined( 'ELEMENTOR_VERSION' ) ) {
        window.addEventListener('elementor/frontend/init', function() {
        elementorFrontend.hooks.addFilter( 'frontend/handlers/menu_anchor/scroll_top_distance', function( scrollTop ) {
        return scrollTop + 90;
        } );
        } );
        } );

        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.

      7. 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!

      8. Oups really sorry about that. It didn't copy paste as expected.

        I updated my previous comment with the proper code, please try again.

      9. That has worked perfectly! You are a genius!

        Thank you so much for all your help!!

  3. 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,

    1. 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.

  4. 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

    1. 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?


    1. 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.

  5. 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.

    1. 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:

      body.alternate-theme #stickyContainer .my-element-class-name {
      background-color: #333;

      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


      1. 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

      2. 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)?

      3. 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;


  6. 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

  7. 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! 🙂

    1. 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!


  8. 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.

  9. 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.


Leave a Reply