How to enqueue style before style.css

How do I enqueue a .css file before style.css is loaded? Or make the default style.css dependant on another .css file?

I’m trying to load a .css reset, which style.css would overwrite.

Here’s what I have:

add_action('wp_enqueue_scripts', 'load_css_files');

function load_css_files() {
    wp_register_style( 'normalize', get_template_directory_uri() . '/css/normalize.css');
    wp_enqueue_style( 'normalize' );
}

However this is loaded after style.css.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Enqueue the style.css too, and set normalize as dependency:

if ( ! is_admin() )
{
    // Register early, so no on else can reserve that handle
    add_action( 'wp_loaded', function()
    {
        wp_register_style(
            'normalize',
            // parent theme
            get_template_directory_uri() . '/css/normalize.css'
        );
        wp_register_style(
            'theme_name',
            // current theme, might be the child theme
            get_stylesheet_uri(), [ 'normalize' ]
        );
    });
    add_action( 'wp_enqueue_scripts', function()
    {
        wp_enqueue_style( 'theme_name' );
    });
}

WordPress will load the dependencies now first automatically when theme_name is printed.


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x