• Resolved Halo Diehard

    (@halo-diehard)


    I’ve been playing around with WordPress for a couple years now, and am somewhat familiar with css, but a problem I am always encountering is that it can take me upwards of two hours to get a simple css customization to work, because it takes me that long to figure out how many and what div’s #’s .’s to put in there!

    I understand the more specific you can be when working down to an element, the better chance you have of your new css taking precedence and working, but sometimes I’ll work on down and it won’t work no matter what I try. Or often I’ve found it will only work if I use div.whatever instead of div#whatever (or vice versus), or sometimes I’ll have to remove a .whatever in the “chain” even though it seems like it’s an element that my element is inside of – – so confusing!!

    I don’t know why I haven’t finally come to some sort of understanding with this, but so much about the patterns eludes me. Of course it gets even more complicated when trying to tweak css in relation to plugins!

    So my (not so) general question, is whether or not in WordPress there is a simple set of rules to follow when listing elements from large (page) to smaller (the .div outside the .div outside the .whatever, outside the .whatever your .element is inside of) all the way down to your specific .element that you are attempting to change with your css, or if it’s mostly just mucking around until you figure it out?

    Any clarification appreciated!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    It all depends on the theme.

    Thread Starter Halo Diehard

    (@halo-diehard)

    Ha ha! lol, @sterndata I guess that’s some information. Well, why does it depend on the theme? I mean, if I check out the code by left-click, inspect (chrome), I should see the hierarchy (or whatever it’s called), right? I peek there for my element, then play with the css (in the chrome code thingy) until I get it to work on that page, which usually takes but a couple minutes, but then when I go to add that css to my child-theme, (which is supposed to take priority, right?) I can’t get it to work.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Themes are responsible for displaying your content. While some of the HTML markup comes from core, most comes from the theme.

    Thread Starter Halo Diehard

    (@halo-diehard)

    Yeah, but isn’t the point of child-themes to override their parent theme’s CSS? I thought WordPress was set up for child-theme’s CSS to take priority? I thought you could take any CSS from a parent theme, copy it into your child theme, and then tweak it, and it should override any parent theme CSS…

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Yes, if you do the CSS correctly in the child, it will override the parent.

    Thread Starter Halo Diehard

    (@halo-diehard)

    Which brings me back to my original question: is there a set of rules to follow to correctly override a parent theme’s CSS with the child theme’s CSS, or is it mostly just mucking around until you figure it out?

    I’m sure my not being familiar with the terminology isn’t helping me to clarify just what I am asking, but I will try.

    I’m looking for guidance on how to determine the hierarchy “path” to the specific element who’s CSS you are looking to change. For example, here is the class of your post just above this one:

    div#post-10918376.odd.bbp-parent-forum-21260.bbp-parent-topic-10918292.bbp-reply-position-5.user-id-7819546.post-10918376.reply.type-reply.status-publish.hentry.by-moderator.author-has-badge

    It’s inheriting CSS from:

    .bbpress #bbpress-forums li.bbp-body div.hentry

    That is the “path” I am referring to. div.hentry is inside of li.bbp-body which is inside of #bbpress-forums, which is inside of .bbpress. Each div inherits it’s parent’s CSS unless it has it’s own CSS that overrides it.

    My understanding is that in a CSS document, the CSS is read from top to bottom, with the rules being executed in that order, so rules beneath are higher priority in the hierarchy, this is why a child theme’s CSS is supposed to overrule a parent theme: because it is loaded after the parent theme’s. But herein lies the issue: the more specific a “path” to a CSS rule is, the more of a chance that it will have a higher priority in the heirarchy than a conflicting rule that is executed AFTER it.

    Because of this, if I want to change a more specific element than can be accomplished by a simple copy/paste of my parent theme, I may need to be more specific in the “path” to my element.

    For example, let’s say I wanted to change your moderator badge color, I could (theoretically) just paste this parent theme code into my child-theme’s CSS and change the color:

    .author-badge-moderator {
         background-color: #000000;
    }

    But let’s say I only want odd row moderator posts to have a black moderator badge background. There isn’t a CSS rule for that in the parent theme, so maybe I would do something like this:

    .odd .author-badge-moderator {
         background-color: #000000;
    }

    Problem is, that might not be specific enough of a “path” to overwrite any parent CSS that might conflict, so I might have to do:

    .bbpress #bbpress-forums li.bbp-body div.hentry .odd .author-badge-moderator {
         background-color: #000000;
    }

    Thing is, that might not work either, which brings us right back to my original question of is there a basic set of rules that helps me figure out how to determine what “path” I need to use to override parent theme CSS.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Which brings me back to my original question: is there a set of rules to follow to correctly override a parent theme’s CSS with the child theme’s CSS, or is it mostly just mucking around until you figure it out?

    Okay. Here’s the quick rundown on CSS Sanity.

    My understanding is that in a CSS document, the CSS is read from top to bottom, with the rules being executed in that order, so rules beneath are higher priority

    This is the first thing you have to unlearn: Order doesn’t matter. ALL CSS is active ALL the time. Doesn’t matter what order they are in the HTML. Doesn’t matter if it’s in a child theme or not. Yes, okay, order does actually matter in the way you say, but for the purpose of maintaining your sanity, stop thinking that way. Pretend that order quite literally does not matter, at all, ever.

    Once you have that down, then you can understand specificity.

    More specific rules override less specific ones. It’s not a guessing game. This has a defined hierarchy.

    Highest to lowest:

    1. Inline styles. An inline style is in the HTML itself, in the “style” part of the tag. h1 style=“color: #fff;”

    2. IDs. These are in the id=”” of the tag, and referred to in the CSS by hashtags: #element.

    3. Classes (also attributes and pseudo-classes). Anything in a “class”, gets referred to with the period, like .element. Attributes like [attr] and pseudo-classes like :hover, :focus have the same value.

    4. Elements and pseudo-elements. These are things like “div” or “ul” in CSS. Direct references to elements in the page. Pseudo-elements are at the same level. These would be elements that would be there if they were not pretend, like :before and :after.

    If you prefer to do it in actual math, you can calculate a value for any given rule with something like a points based system. 1000 points for the inline styles, 100 points for IDs, 10 points for classes, 1 point for elements. So a rule like this in the CSS body #content .data img:hover gets 122 points. One ID (content), 2 classes (data and hover), 2 elements (body and img). A rule scoring higher referring to the same thing in the HTML would win and have precedence.

    But, realistically, you just need to learn what is more specific. “body” refers to literally anything on the page. “body img” only refers to the images on the page. If you had both “body” and “img” as selectors in your CSS, then they have the same score, but “img” is more specific to actual images than the “body” is because, well, they’re using actual img tags directly.

    If you need to override a style rule, use a more specific selector. Doesn’t matter where it is in terms of order on the page, more specific always wins, because all selectors available are applied all the time. Order only matters when you use the same identical selectors for the same identical rules, which you probably shouldn’t do because that way lies madness.

    Also, “!important” beats non-“!important”. Avoid using this unless you have no other choice.

    But let’s say I only want odd row moderator posts to have a black moderator badge background. There isn’t a CSS rule for that in the parent theme, so maybe I would do something like this:

    Avoid adding things to the HTML like “odd” and “even” classes, when CSS has native support for such things to begin with. If you’re modifying the HTML just to add classes for CSS targeting, then you’re probably doing-it-wrong. CSS is powerful. Use it natively.

    .author-badge-moderator:nth-child(odd) { ... }

    Or something similar. Maybe you need the odd divs above it and then want to target the moderator badge inside them. So with html like:

    <div class="example">
    <span class="author-badge-moderator"></span>
    </div>
    ... repeating...
    

    Then something like this instead:

    .example:nth-child(odd) .author-badge-moderator { ... }

    Thread Starter Halo Diehard

    (@halo-diehard)

    @otto42 omg thank you so much for taking the time to throw all that my way. I knew most of it, but it was kind of a jumbled mess in my head. That point system really clarified it for me. Also, specificity! That is what I was referring to above when I was using the word “path”; I just didn’t know the correct terminology.

    Thanks for clarifying how order works in CSS as well.

    Your post is exactly what I was looking for to help me organize what I needed in my brain.

    RE “odd” and “even” in HTML, I pulled that class example straight from bbpress code, lol, that’s not me. I know even less about HTML than I do CSS.

    But yeah, great reference, man, I’m sure I’ll be referring back to your post in the future next time I get stuck!

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Just Google for “CSS specificity”. Plenty of tuts on the idea.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘General question on WordPress css hierarchy’ is closed to new replies.