Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Apply class to parent if child div contains class

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #39789

    Is there any way that anyone knows of to pull this off:

    I have:

    What I want is to change the background of body to blue IF the div inside it has a class of blue. Same principle if it’s red, green, yellow, etc.

    Does anyone know of a way to accomplish that?

    #109828
    Kitty Giraudel
    Participant

    No way in CSS. We can’t target a parent with pure CSS yet.

    #109831

    Is there a way with javascript or jquery?

    #109833

    I’m doing this in a WordPress Theme if that makes a difference…

    #109834
    TheDoc
    Member

    No problem with jQuery. Would look something like this:

    if( $(‘#target’).hasClass(‘blue’) ) {
    $(document.body).addClass(‘yellow’);
    }

    Then in your CSS you would have your styles for `.blue` and `.yellow`.

    #109837

    I have got to be doing something wrong. Here’s what I have:

    In Header.php inside the head tag I put:

    In my page.php, I have:

    #109838

    well – apparently I can’t figure out how to do code blocks in the new forum either… one of those days – I have my if statement wrapped in script tags, and it removed them.

    #109847

    That didn’t work either… hmmm…

    #109851
    theacefes
    Member

    http://codepen.io/katbella5/pen/pxutd

    No need for periods. The jQuery docs say they aren’t needed.

    Make sure you are calling the jQuery library properly. I think that is likely your issue.

    -Kat

    P.S.: In other exciting news, that was my first CodePen! Yay.

    #109862
    TheDoc
    Member

    Also, it’s generally best practice to make sure you IDs and classes are lowercase. There is *some* preference to using camelCase out there, but I think the industry standard is lowercase with hyphens.

    So it your example:

    `#ContentRow` would become `#content-row` and `.Blue` would be `.blue`, etc.

    You don’t need to add periods when using `.hasClass()`. It also doesn’t play very nicely with multiple class names, they have to be in the proper order. So if you do `.hasClass(‘first second’)` but the element on the page is actually `class=”second first”` then hasClass() will return `false`.

    Here’s an example for you: http://jsfiddle.net/e6kE3/

    #109863
    TheDoc
    Member

    Having said all of that, @theacefes is probably correct – you might be trying use jQuery before the library is included. You might also want to wrap your code in something like this:

    $(document).ready(function() {
    // your code here
    });

    #109864
    theacefes
    Member

    I work for a company that uses .NET so we’re all about Camel Case. I started off using underscores though.

    https://css-tricks.com/poll-results-hyphens-underscores-or-camelcase/

    It’s from a while ago, but still interesting.

    #109865
    TheDoc
    Member

    camelCase certainly has its place! I think it’s all about what language you are using.

    JS/PHP – camelCase
    HTML/CSS – hyphens / underscores

    #109906
    theacefes
    Member

    Well for us it’s about maintaining a consistent standard across the board I think. On a personal level, whatever makes one comfortable I think. :)

    #110095
    Taufik Nurrohman
    Participant
    $(document).ready(function() {
    $('div').each(function() {
    var foo = this.className; // Get the element class...
    if ($(this).hasClass(foo)) {
    $('body').addClass(foo);
    }
    });
    });
Viewing 15 posts - 1 through 15 (of 30 total)
  • The forum ‘CSS’ is closed to new topics and replies.