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="<?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.

Read More

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!

Related posts

2 comments

  1. 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.

  2. The 3rd parameter accepted by wp_enqueue_script is $dependencies… aka what stylesheets must be loaded prior.

    function wpa_115637(){
        wp_enqueue_style(
            'wpa_custom',
            get_stylesheet_directory_uri() . '/responsive.css',
            'main_style' // use the handle that you used when you enqueued the main stylesheet 
        );
    }
    add_action( 'wp_enqueue_scripts', 'wpa_115637' );
    

Comments are closed.