Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply style if element exists

Tags:

css

I need to use CSS to apply style to class article ONLY if class subject is present inside .container

<div class="container">
    <div class="subject">
       ...
    </div>
    <div class="article">
       ...
    </div>    
</div>

I was trying to use sibling selector, but it does not seem to work. What am I missing?

.container + .subject .article { ... }
like image 688
santa Avatar asked Jan 10 '23 14:01

santa


2 Answers

The adjacent sibling combinator is meant for sibling elements. Your selector wasn't working because .container and .subject are not siblings, .subject is a child of .container.

.container .subject + .article

The elements .subject and .article are siblings, therefore it should work.

Example Here

like image 92
Josh Crozier Avatar answered Jan 23 '23 13:01

Josh Crozier


You can use the sibling selector:

.container .subject + .article { // Your CSS rules

The rule you have now grabs the .container, checks for the sibling .subject, and then grabs the child .article, so it's just in the wrong order.

like image 43
Igor Avatar answered Jan 23 '23 12:01

Igor