Change every post background based on category

The body_class() is what controls those classes, and does not include the category in single posts. That is why the CSS doesn’t work once you click through to a post.

I did find a function that may help, though. Try placing this in your functions.php. This will add the current post’s category back to the body class. You should be able to style your pages easier once the class is available. Double the classes on the body tag if it’s still not working, and make sure it matches your CSS.

add_filter('body_class','add_category_to_single');
function add_category_to_single($classes) {
    if (is_single() ) {
        global $post;
        foreach((get_the_category($post->ID)) as $category) {
            $classes[] = 'category-'.$category->slug;
        }
    }
    return $classes;
}

References:

  1. https://codex.wordpress.org/Function_Reference/body_class