Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - design parent class if child has a specific class [duplicate]

Tags:

html

css

web

Possible Duplicate:
Complex CSS selector for parent of active child
Is there a CSS parent selector?

Is there a way to design parent class based on if its child elements has a specific class?

<div class="container">
   <div class="content1">
      text
   </div>
</div>

.

<div class="container">
   <div class="content2">
      text
   </div>
</div>

I want to design the first .container differently based on if the child class is content1 or content2. It must be pure css solution, without javascript.

like image 767
Alvarez Avatar asked Oct 16 '12 18:10

Alvarez


3 Answers

No, you can't do that. CSS can't select elements based on their children, except to check whether or not the element is empty.

like image 59
tuff Avatar answered Nov 15 '22 15:11

tuff


Why not use container1 + content and container2 + content?

<div class="container1">
    <div class="content">
      text
    </div>
</div>

<div class="container2">
    <div class="content">
        text
    </div>
</div>

And then write CSS like so:

.container1 .content {
    /* Container 1 styles here */
}

.container2 .content {
    /* Container 2 styles here */
}
like image 36
Kevin Boucher Avatar answered Nov 15 '22 14:11

Kevin Boucher


What you're asking for is the the mythical CSS parent selector. Perhaps some day.

like image 23
Asad Saeeduddin Avatar answered Nov 15 '22 14:11

Asad Saeeduddin