Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping css selectors

Is it possible to group two selectors so that an element is matched only if both selectors are satisfied? For example, if I want the element to satisfy both the selectors .a>.b~.c and .d~.e, is it possible to write a selector that matches the intersection of these selectors?

like image 816
ccy Avatar asked Jan 21 '11 22:01

ccy


People also ask

How do you group the CSS selectors?

To group selectors, separate each selector with a comma.

How do I group multiple selectors in CSS?

Grouping selectors - CSS TutorialYou can group multiple selectors into one declaration block, separating each selector with a comma. This will apply the styles to all of the matched elements in the list. You can also mix different types of selectors in the same list. The spaces are optional, but the comma is not.

What is grouped selector?

The CSS grouping selector is used to select multiple elements and style them together. This reduces the code and extra effort to declare common styles for each element. To group selectors, each selector is separated by a space.

Can you group things in CSS?

You can write the CSS styles to a specific element or group elements together with a specific style. Grouping CSS styles saves page space and allows you to apply particular styles to multiple tags.


2 Answers

If you're using class selectors as in your example, you can chain classes like this:

.a > .b.d ~ .c.e

Selects an element that has both class="c e"
which is a sibling of (i.e. comes, directly or not, after) an element that has both class="b d"
which is a child of some element of class="a"

Or, like this, if you want .c.e to occur after an element that has either class b or d:

.a > .b ~ .c.e, .a > .d ~ .c.e

Selects an element that has both class="c e"
which is a sibling of an element with either class="b" or class="d" (or both)
which is a child of some element of class="a"

Both selectors imply that .b, .d and .c.e are all children of .a. I should also think it gives you the class selector intersection that you're looking for.

like image 143
BoltClock Avatar answered Oct 06 '22 01:10

BoltClock


No, you cannot do set intersection in CSS selectors. Sometimes you can find a way to combine two selectors into one that solves your specific problem, but I don't think there's a way to combine your two sample selectors.

like image 45
Jacob Avatar answered Oct 05 '22 23:10

Jacob