How to Create a Sticky Header by using Custom JS

Why Use a Sticky Header?

Sticky headers, also known as fixed headers, stay at the top of the page as the user scrolls down. This allows for easy access to key navigation links without the need to scroll back up. By adding a background color and shadow effect, you can improve the header’s visibility against different page backgrounds, making your website look modern and professional. How to Create a Sticky Header by using Custom JS

Step 1: Identify Your Header Class or ID

To customize your header, you first need to find its class or ID. Most WordPress themes assign a unique identifier to the header section. You can find this by following these steps:

  1. Open your website in a browser.
  2. Right-click on the header and select Inspect (or press Ctrl + Shift + I on Windows, or Cmd + Option + I on Mac).
  3. In the Elements tab, look for a class or ID for your header element (commonly named .site-header or #header).
  4. On Elementor page builder select your header container and goto Advanced settings and find under Layout tab CSS ID

Step 2: Adding CSS for the Sticky Header

With your header’s class or ID identified, add custom CSS to style the header for sticky mode. In this example, we’ll make the header transparent initially, then apply a background color and shadow when it becomes sticky. How to Create a Sticky Header by using Custom JS

CSS Code:

/* Initial transparent header */
#yourHeaderID {
background-color: transparent;
transition: background-color 0.3s ease, box-shadow 0.3s ease;
box-shadow: none; /* No shadow initially */
}

/* Sticky header with background color and shadow */
#yourHeaderID.sticky {
background-color: #yourColorCode; /* Replace with your desired color */
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2); /* Bottom shadow effect */
}

 

Step 3: Making the Header Sticky with JavaScript (if needed)

Some WordPress themes don’t support sticky headers by default. You can add a simple JavaScript snippet to make your header sticky when the user scrolls down.

window.onscroll = function() { addSticky(); };

var header = document.getElementById(“yourHeaderID”);
var sticky = header.offsetTop;

function addSticky() {
if (window.pageYOffset > sticky) {
header.classList.add(“sticky”);
} else {
header.classList.remove(“sticky”);
}
}

 

Step 4: Adding the CSS Code to WordPress

To apply the CSS code:

  1. Go to your WordPress dashboard.
  2. Navigate to Appearance > Customize > Additional CSS.
  3. Paste the CSS code and publish the changes.

 

Step 5: Testing the Sticky Header

After applying the code, visit your website and scroll down to see the sticky header in action. You should notice a smooth background color transition and a shadow effect, enhancing your website’s look and navigation. For more articles : https://webfrik.com/blog/

 

Share to Social Media

Facebook
Twitter
LinkedIn
Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Retaled Post

Enter Key to Search

Book Free Consultations

Get Free Consultation