0
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'twentytwentyone-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('twentytwentyone-style')
    );
}

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

When I add styling changes to the child theme style.css file, I can see them listed in chrome dev tools, but crossed out and overridden by parent styling. I've only been able to override parent styling by adding !important to the child styling changes. How can make styling changes without !important, or is this the only way to make styling changes?

5
  • Have you tried a more specific selector? E.g. parent used .main-content .mydiv you use body .main-content .mydiv or .main-content .someclass .mydiv.
    – kero
    Jan 19, 2022 at 18:11
  • Yes that did fix it, so I have to do this for all changes? Why is that the case?
    – BeccaN
    Jan 19, 2022 at 18:15
  • 1
    Depends on your specific case. It should work with same specifity (since you define the parent -> it should be loaded before). But more specific will always work, see this article on MDN for more info.
    – kero
    Jan 19, 2022 at 18:17
  • Ah okay, thank you for your help!
    – BeccaN
    Jan 19, 2022 at 18:21
  • @kero can you post that as the answer rather than a comment?
    – Tom J Nowell
    Jan 19, 2022 at 18:58

1 Answer 1

0

From the looks you've enqueued everything correct: Set the parent style as dependency, this way WordPress should first load the parent style and then the child style.

However, it can still happen that your rules are not applied, this is usually happening due to a rule being more specific. See this MDN article for more information.

As a solution, you can make your rules be more specific:

// parent
.main-content .mydiv

// child
body .main-content .mydiv
// or
.main-content .someclass .mydiv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.