Do you want your WordPress site to load with a smooth fade-in effect that enhances the user experience? Many optimization plugins delay or defer styles and scripts, which can interfere with simple visual enhancements, such as animations.
In this tutorial, I’ll show you exactly how to add a fade-in animation to your WordPress site in a way that’s resistant to caching or optimization plugins.
What We’re Building
We’ll apply a short fade effect when the page loads by animating the body and main container. Here’s a preview of the effect:
Step-by-Step Instructions
1. Create or Edit header-sb.php
Inside your child theme, create a file named header-sb.php
(or edit it if it already exists). Then add the animation style directly inside a <style>
tag:
<style>
body {
animation-duration: 0.5s;
animation-name: animate-fade;
animation-delay: 0.5s;
animation-fill-mode: backwards;
}
@keyframes animate-fade {
0% { opacity: 0; }
100% { opacity: 1; }
}
</style>
2. Inject the Custom Header from functions.php
To make sure our header-sb.php
is loaded correctly and early, add the following code to your child theme’s functions.php
file:
function sb_header() {
get_header("sb");
}
add_action('wp_head', 'sb_header', 0);
This hook injects the header-sb.php
file directly into the <head>
section before other plugins or styles load. The 0
Priority ensures it runs before most optimization tools can modify the DOM.
Why This Works (Even with Optimization Plugins)
Most WordPress performance plugins (like Autoptimize, WP Rocket, or LiteSpeed Cache) delay or combine CSS files to speed up page load. Unfortunately, that often delays important styles or breaks visual effects.
By inserting the style directly into the <head>
using a custom header and hooking it early via wp_head
, We ensure:
- The animation CSS loads immediately
- The effect kicks in reliably, even if other CSS is deferred
- There are no conflicts with the cache or modification
Final Result
After applying the steps above, your entire page (body and main container) will softly fade in after 0.5 seconds, creating a more polished and elegant loading experience for visitors.
Wrapping Up
This small enhancement can improve perceived performance and user experience with just a few lines of code. And by adding it early and locally inside the <head>
You’re protected from performance plugin interference.
Have questions or want to see more small WordPress UI tricks? Let me know in the comments!
Mohammad Dahamshi is a skilled Embedded Software Engineer and web developer. With experience in C/C++, Linux, WordPress, and DevOps tools, he helps businesses solve technical challenges and build reliable digital solutions. Fluent in Arabic, Hebrew, and English, he also runs Saratec, offering web design and digital marketing services.