Trying to load different syles for 404.php page

I’ve created a 404.php page and my theme correctly loads that page when an error is made. My question is – how do I style it? I notice that the body tag like this:

<body class="error404">

So should I call that in an enqueue to load some css, perhaps something like this:

          // Error 404
      if ( is_page(404)):
        // Load Newsletter Main CSS
        wp_enqueue_style( '404 css', get_template_directory_uri() . '/css/404.css', array(), null);
      endif;

My question is – how should I associate the 404.php page with a css stylesheet?

Thanks

1 Answer
1

You have 2 options here.

Use Built-in Classes

As you already mentioned, WordPress automatically adds classes to body, based on the current page. You can use it to style your elements differently, or even use your own different classes in your 404.php:

.error404 p {
    // Some different classes here
}

Enqueue Your Styles Only for 404 Page

You can check whether the page is a 404 error or not, and then enqueue your styles:

add_action('wp_enqueue_scripts','enqueue_my_script');
function enqueue_my_script(){
    if( is_page_template('404.php') ){
        // Enqueue your style here
    }
}

Leave a Comment