Wordpress - Child themes CSS priority problems without using !important
Try enqueuing your child theme's CSS like so:
// Queue parent style followed by child/customized style
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', PHP_INT_MAX);
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/styles/child-style.css', array( 'parent-style' ) );
}
Notice a few things:
1) PHP_INT_MAX as priority so that this runs last
2) get_stylesheet_directory_uri() vs. get_template_directory_uri() which will point to the child theme's template folder instead of the parents.
I added a subfolder there, as well /styles/
because I keep my CSS in a subfolder of my theme normally.
3) followed by array( 'parent-style' )
to make the child CSS have the parent CSS as a dependency, which has the effect of putting the parent theme first in the head and the child theme CSS following it. Because of that, whatever also appears in the child theme that is already in the parent theme will overwrite the parent theme's version.
TLDR answer: first parameter of each wp_enqueue_style()
should not be left as 'parent-style' and 'child-style'. They should be renamed to match the name of the parent theme and its child.
Problem
If you don't rename the parameters, you will get the child theme enqueued a second time can result in rules appearing twice in Firebug and changing values in the wrong one having no apparent effect, which may trick you into thinking your child rules don't override the parent.
The expectation
The Codex page on Child Themes correctly says that if you do nothing, the child CSS is linked automatically. It does, but only that. CSS workflow is a bit different: you want to override, not replace. It's logical (it works like the other theme files) but they could have had a note.
Solution
Rename the parameters. I do it like below to get (a little) more control, note that you should replace twentysixteen and twentysixteen-child with the names of your theme and child theme:
function theme_enqueue_scripts() {
//FIRST
wp_enqueue_style( 'twentysixteen-style', get_template_directory_uri() . '/style.css' );
//...custom queueing of .js and .css for Javascript plugins and such here
//LAST
wp_enqueue_style( 'twentysixteen-child-style', get_stylesheet_directory_uri() . '/style.css', array( 'twentysixteen-style' ) );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );
(Also note that you don't get control of the link order of some (all?) WP plugins in this action. They get linked after.)
Happy selector-hunting ;)