Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Multiple selection based on parent

For the below code:

<div id="main">
   <div class="one"></div>
   <div class="two"></div>
   <div class="three"></div>
</div>

I want to select all of the 3 divs inside by class name and parent like this:

div#main div.one div.two div.three{/*apply common properties for all 3*/}

^ Doesn't work.

This is an abstract example so I need a solution similar to the one above, things like:

div#main>div{/*apply common properties for all 3*/}

Would not work in the real-case scenario.

Thank you.

like image 357
Francisc Avatar asked May 13 '26 20:05

Francisc


2 Answers

I don't think you have any other choice than this syntax :

div#main div.one,
div#main div.two,
div#main div.three
{/*apply common properties for all 3*/}
like image 56
greg0ire Avatar answered May 15 '26 09:05

greg0ire


I'm not entirely sure what you are looking for because the example is fairly abstract, however, you could try to give each of the child divs a second class after their numbering and selecting by that. This assumes you have control over the server-side/html generation. For example:

<div id="main>
    <div class="one other-style"></div>
    <div class="two other-style"></div>
    <div class="three other-style"></div>
</div>

div#main div.other-style {
    font-family: 'Comic Sans MS';
}

Except don't actually use Comic Sans.

This answer may be a bit simplistic, but without knowing a bit more that's all I could think of!

like image 29
marta.joed Avatar answered May 15 '26 08:05

marta.joed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!