Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS style for parent instead of child?

Tags:

css

I am using jquery to insert html I get from the server into a div. Based on the div contents i'd like to adjust the syle of the container. I wrote up a simple html test. How do I tell css to apply this site when it has X child? After googling I tried :has and :contains with neither working. I don't want to keep my styles in JS as css makes more sense.

Demo: http://jsfiddle.net/wd9fk/

html

<div id="a"><div id="b">B</div></div>
<div id="a"><div id="c">C</div></div>

css

#a { height: 400px; border: 1px solid red;  }
#b { height: 200px; }
#a :has #b { height: 300px; border: 1px solid blue; }
like image 347
BruteCode Avatar asked Dec 27 '22 11:12

BruteCode


1 Answers

You simply cannot traverse up the DOM in CSS. You will need to use JavaScript.

Here is an article explaining why: http://snook.ca/archives/html_and_css/css-parent-selectors

Long story short, it's due to the way CSS is read by the browser, and by introducing it, it would increase the performance hit by a factor of ten (at least!), because it would need to read every single node multiple times to see whether or not it fits the profile.

It's a nice thought, but it's simply not viable.

like image 149
Nix Avatar answered Jan 13 '23 12:01

Nix