How can I make new .css file in child theme override styles in child theme’s style.css

I want to make a separate responsive.css file in my child theme but having trouble making the media queries override the styles in the child theme’s default style.css.

I’ve tried putting <link rel="stylesheet" href="https://wordpress.stackexchange.com/questions/115637/<?php echo get_stylesheet_directory_uri(); ?>/responsive.css" type="text/css" media="screen"/> after the <?php wp_head(); ?> code, which works, but I’ve read that this is not good practice.

I do not really want to add the media queries to the end of style.css as it’s already large and getting quite confusing!

2 Answers
2

You can enqueue your own stylesheet by adding this to your child theme’s functions.php file:

function wpa_custom_css(){
    wp_enqueue_style(
        'wpa_custom',
        get_stylesheet_directory_uri() . '/responsive.css'
    );
}
add_action( 'wp_enqueue_scripts', 'wpa_custom_css', 999 );

This will load the file responsive.css located in your child theme directory. The priority is set very low (999) so it is more likely to appear after other styles which may be enqueued on the same action.

Leave a Comment