How to Auto-Adjust Hero Section Spacing for Sticky Headers in Bricks Builder?

Learn how to automatically adjust the top spacing of your hero section to prevent content from being hidden when the sticky header is activated in Bricks Builder. This quick guide will help you implement the solution simply by adding a JavaScript snippet; without any hassle of manual configurations.

  • Published on: August 22, 2025
  • Updated on: August 25, 2025

Wasim Akram

Blog Author

Bricksism - Default Featured Image

If you’re frustrated with the hassle of manually adding extra top padding to the hero section when the sticky header is activated to prevent content from being obscured, today you can resolve that issue once and for all.

In this article, I’m providing a plug-and-play, secure, performance-optimized JavaScript code snippet that leverages only the native Bricks selectors to immediately fix the problem we discussed above.

If you don’t want to learn why I created it or how it works, just go ahead and add the script to your site by following the steps in the relevant section; you’ll be all set.

However, if you want to learn why I made it and how it works, feel free to read the entire article; it’s fun.

Disclaimer: This code snippet is completely vibe coded, with manual checks and testing done. If you’re skeptical, you should do your due diligence before using it on your site or simply choose not to use it at all. I’ve done my best to make it safe, secure, and performant, and I use it in my projects as well, but please use it at your own risk as I don’t provide any guarantees.

Why I Created This Script?

I have been using Bricks since its early days, and I don’t often use sticky headers on my sites, but sometimes clients want them, and I can’t say no.

Using a sticky header in Bricks Builder is such a hassle because whenever you activate it, the hero section ends up underneath the header.

If you don’t activate “sticky on scroll“, the header just sits right on top of the website body, specifically, the hero section.

I remember the last time I used a sticky header on a large client site with tons of pages.

I had to manually add extra headers to all the templates, like singles, archives, and custom-built pages, which was a huge pain.

Later, I started making a class to add extra padding on top of the hero section and assigned this class to all the templates and pages, which was a little more convenient.

But I wasn’t satisfied.

I needed a more robust, automated solution because I started using sticky headers on my agency and store websites recently, as I wanted to use the latest frosted glass or glassmorphism effect on the header, which is cool.

So, I thought, why not create a permanent solution with my vibe coding skills (which I learned while building my first full-fledged micro saas product) to fix this issue without digging into manual work at all?

I wanted a fully automated solution.

After two days of trial and error, lots of iterations, and implementing all my ideas, I ended up with a JavaScript snippet that does it all automatically, without even touching a single line of code; exactly as I wanted.

Now, I can simply plug this script into all my and my clients’ projects and forget about manually adjusting hero section padding altogether, which is truly a time saver.

How Does This Script Work?

So, here is how this script works when you activate it:

  1. At first, it checks if the sticky header without sticky on scroll is activated or not. If it finds that it’s activated, then it automatically runs.
  2. It dynamically calculates the current sticky header height to use as a token context in the next step.
  3. Then it automatically adds top padding exactly equal to the calculated header height on top of the existing hero padding; no more, no less.

Benefits of using this script

  • Unlike any other solutions available out there, it’s just a plug-and-play solution. It leverages the Bricks native selectors to work without any manual configurations.
  • You don’t need to manually update your header or hero section selectors in the this code snippet.
  • You also don’t need to add any extra CSS or assign any selectors to the dedicated sections to make it work.
  • Plus, you don’t need to change the top padding of the section manually to prevent the sticky header from obscuring your content; you can keep it as is.

Disadvantages of Using This Script

  • It completely depends on Bricks’ native selectors, like .brx-sticky to run, .on-scroll to stop running, and #brx-content > section:first-child to inject the top padding into the hero section. So if they change any selector in the future (which I hope they won’t), the script will stop working until you update the latest selectors.
  • On initial page load, when the script assigns top padding to the hero section, it’s delayed by a fraction of a second due to its dynamic nature. This causes the hero content to visibly shift layout from zero padding to header height padding.
    If you want to fix this, you can simply hide the hero section content momentarily to let the page load and layout shift happen behind the scenes, then show it after the shift is done. I also have a code snippet for that if you need it.

How to Add This Script to Your Website?

Here is the full code snippet you need to add to your website.

Please make sure to read the prerequisites just below the snippet before using it.

(() => {
  /**
   * @author Wasim Akram
   * @link https://bricksism.com/auto-adjust-hero-section-spacing-for-sticky-headers/
   * @version 1.0.0
   * Description: Automatically adjusts hero section spacing to accommodate sticky headers in Bricks Builder.
   * Usage: Plug and play - no manual configuration needed.
   */

  // Bricks native selectors (do not change):
  const sw_headerSelector = '.brx-sticky'; // Sticky header (Bricks default)
  const sw_heroSelector = '#brx-content > section:first-child'; // Hero/first section (Bricks default)

  let sw_header = null;
  let sw_hero = null;
  let sw_originalPaddingTop = null;

  /**
   * Initializes header and hero references and stores hero's original padding.
   * This prevents accumulating multiple paddings and ensures safe repeated calls.
   */
  function sw_initElements() {
    sw_header = document.querySelector(sw_headerSelector);
    sw_hero = document.querySelector(sw_heroSelector);

    if (!sw_header || !sw_hero) {
      // Warn for missing Bricks native structure (shouldn't happen unless template missing)
      console.warn('Sticky header or hero section not found.');
      return false;
    }

    if (sw_originalPaddingTop === null) {
      // Get the initial padding-top value of the hero section only once
      const computedStyle = window.getComputedStyle(sw_hero);
      sw_originalPaddingTop = parseFloat(computedStyle.paddingTop) || 0;
    }
    return true;
  }

  /**
   * Updates the top padding of the hero section to clear the sticky header.
   * - Adds header height to original hero padding.
   * - Resets to original if the header is in "on-scroll" state.
   */
  function sw_updatePadding() {
    if (!sw_header || !sw_hero) return;

    // When .on-scroll is present, revert to original padding
    if (sw_header.classList.contains('on-scroll')) {
      sw_hero.style.paddingTop = `${sw_originalPaddingTop}px`;
      return;
    }

    // Otherwise add sticky header height to original padding
    const headerHeight = sw_header.offsetHeight;
    sw_hero.style.paddingTop = `${sw_originalPaddingTop + headerHeight}px`;
  }

  /**
   * Main handler: ensures elements exist and updates padding as needed.
   */
  function sw_onUpdate() {
    if (sw_initElements()) {
      sw_updatePadding();
    }
  }

  /**
   * Throttled handler for scroll/resize to avoid layout jank.
   */
  let sw_ticking = false;
  function sw_throttledUpdate() {
    if (!sw_ticking) {
      window.requestAnimationFrame(() => {
        sw_onUpdate();
        sw_ticking = false;
      });
      sw_ticking = true;
    }
  }

  // Run on page load, scroll, and resize:
  window.addEventListener('load', sw_onUpdate);
  window.addEventListener('resize', sw_throttledUpdate, { passive: true });
  window.addEventListener('scroll', sw_throttledUpdate, { passive: true });
})();

Code Snippet Changelog:

## Changelog for Sticky Header Hero Padding Adjuster

### [1.0.1] – 2025-08-25
#### Fixed
- Renamed the header `.sticky` class to `.brx-sticky` to maintain compatibility with Bricks 2.0.2.

### [1.0.0] – 2025-08-22
#### Added
- Released: Sticky Header Hero Padding Adjuster.

Prerequisites Before Using This Script:

Before adding this script to your site, you need to do two things.

1. Sticky Header Activated: Make sure your sticky header is activated and properly set up in the Bricks header template settings.

Activate Sticky Header in Bricks Builder - Bricksism

2. Code Snippet Plugin Installed: You should have a code snippet plugin (e.g., FluentSnippets or Scripts Organizer) installed and activated. This is where you will safely add the JavaScript snippet without modifying core theme files.

In this tutorial, I’ll use FluentSnippets because it’s free, although I personally use Scripts Organizer, which is a paid plugin.

Step-by-Step Process:

Add Bricks Sticky Header Hero Padding Adjuster Script in the FluentSnippets - Bricksism

Step 2: Access the FluentSnippets

Open your WordPress Dashboard and click on the “FluentSnippets” menu from the admin panel.

Step 2: Create a New Snippet

Click the “New Snippet” button to add the script provided in this article, then select “Scripts” from the available radio options.

Step 3: Enter Snippet Name and Description

Add a name (e.g., Sticky Header Hero Padding Adjuster) and description (if needed) to remember the purpose of this snippet.

Step 4: Copy and Paste the Code

Paste the JavaScript code snippet you copied from the above section.

Step 5: Assign the Location to Inject the Script

In the “Where to Run?” section, select “Site Wide Footer” to inject it before the closing body and click the “Create Snippet” button.

Step 5: Activate the Script

Once it’s ready, just hit the “Activate” button to run the script on your site.

That’s all!

Now, your sticky headers won’t sit on top of your hero section because this script dynamically adds the necessary spacing to keep everything looking normal.

Conclusion

In this article, I provided you with a plug-and-play code snippet to automatically adjust the spacing of the hero section when the sticky header is enabled, and also showed you how to add that script to your site step-by-step.

If you have any suggestions or want to contribute to making this snippet more robust, feel free to comment below.

Also, if you want me to create custom features or fixes for your Bricks-specific sites, feel free to get in touch with me either through my agency website or via Facebook Messenger to discuss further.

Lastly, if you find this post valuable, consider sharing it with your fellow Bricks users. And if you have any questions or concerns, please drop a comment in the section below.

4 6 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Posts You’ll Love

Keep exploring and sharpen your Bricks Builder skills with more expert guides, tips, and inspiration tailored just for you. Each post is carefully crafted to help you build faster, solve real-world challenges, and get the most from Bricks.

Explore More Posts
Bricksism - Default Featured Image

My Top Concerns About the Bricks Builder Ecosystem as a Committed Member

A quick look at what really holds the Bricks Builder ecosystem together, and where things could use some work. Whether you’re new or experienced, understanding this helps us all move…

Read Now
Bricksism - Default Featured Image

What is the Best Theme to Use with Bricks Builder?

Many people search for the perfect theme to pair with Bricks Builder, but the real answer might not be what you expect. The best fit could be hiding in plain…

Read Now
Bricksism - Default Featured Image

How to Display Total Post Count for Any Post Type in Bricks Builder?

Learn how to display the total published post count for any post type in Bricks Builder using a custom-made, optimized PHP function. Works with native posts, WooCommerce products, or any…

Read Now

Connect. Learn. Build Together.

Become part of the Bricksism family; a vibrant, supportive community where freelancers, designers, and agencies come together to share insights, ask questions, and celebrate wins. Here, you’ll find encouragement, real-world tips, and a network of passionate Bricks Builder users ready to help you grow.

Join Facebook Community
  • Connect with passionate Bricks Builder users who share your goals and challenges.
  • Access practical tips, insider tricks, and real-world solutions tailored for all skill levels.
  • Share your progress, get feedback, and celebrate your wins with a supportive network.
  • Stay updated on the latest Bricks features, tutorials, and community events.
  • Experience friendly mentorship and no-fluff guidance, making web building easier and more fun.
Bricksism - Default Featured Image

👨‍💻 Need Help Adding This Feature to Your Site?

If you want everything done for you without touching a thing, I can set up this hero section auto-spacing feature or create similar custom JavaScript or PHP solutions for your (or a client’s) Bricks-powered site, fully implemented, secure, and tailored to your needs.

Request Your Setup