Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRY up CSS - multiple parents to one child

I have several hierarchies separately listed out below where first selector is the parent div, second is the image item within the div. But could I combine these somehow?

.outdoors .how-to-image {
  cursor: pointer;
}

.snowsports .how-to-image {
  cursor: pointer;
}

.stripe .how-to-image {
  cursor: pointer;
}

.twilio .how-to-image {
  cursor: pointer;
}

.rental_requests .how-to-image {
  cursor: pointer;
}
like image 241
james Avatar asked Feb 11 '15 22:02

james


People also ask

How do I not include my first child in CSS?

This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element. Explanation: The above example shows that a <div> is a container Tag.

How do I specify parents in CSS?

There is currently no way to select the parent of an element in CSS in a way that works across all browsers. That said, the Selectors Level 4 Working Draft includes a :has() pseudo-class that will provide this capability. It will be similar to the jQuery implementation.

Is there a CSS parent selector?

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.

How do I skip the last child in CSS?

:not(:last-child) Combining these two above selector to excludes the last children (inner-div) of every parent div from the selection.


1 Answers

If the parent element doesn't matter, then you could simply use:

.how-to-image {
  cursor: pointer;
}

Otherwise, you would have to combine the selectors. The simplest form would be:

.outdoors .how-to-image,
.snowsports .how-to-image,
.stripe .how-to-image,
.twilio .how-to-image,
.rental_requests .how-to-image {
  cursor: pointer;
}

Aside from that, there is no other way to simplify it. Even if you were to use LESS, the same would be outputted.

.outdoors, .snowsports, .stripe, .twilio, .rental_requests {
  .how-to-image {
    cursor: pointer;
  }
}

Output:

.outdoors .how-to-image,
.snowsports .how-to-image,
.stripe .how-to-image,
.twilio .how-to-image,
.rental_requests .how-to-image {
  cursor: pointer;
}
like image 176
Josh Crozier Avatar answered Oct 27 '22 01:10

Josh Crozier