Add CSS animation as Preloader to WordPress

The question:

I found a CSS animation snippet on Codepen, and would like to add it in my WordPress site for preloader animation, but I couldn’t find any related help or plugin that would allow me to add a Preloader with custom CSS animation.

I tried googling, but all I could find was plugins that accepts “GIF animation” for preloader animation. But, I want to use “CSS animation” instead of “GIF animation”.

Any suggestions?

P.S. I only have a moderate knowledge of WordPress.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You can accomplish this by setting a class on the body and removing it with JS when the page is loaded. This is just a basic example but it’ll work out of the box.

 // Add specific CSS class by filter
add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
    // add 'class-name' to the $classes array
    $classes[] = 'preloader-visible';
    // return the $classes array
    return $classes;

// Add preloader style
add_action('wp_head', function(){ ?>
    /** let's every child  of body know there is a loader visible */
    body.preloader-visible {

    /** by default loader is hidden */
    body > .loader {

    /** when loader is active the loader will show */
    body.preloader-visible > .loader {

// Remove preloader when document is ready
add_action('wp_footer', function(){ ?>

      $(function () {




All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment