4

Is it possible to dynamically assign a class to an existing class? I am trying to allocate a style to the first child like this:

.active {
  color: red;
}

item:first-child .item_text {
  background: rgba(255, 255, 255, 1);
  /* make it active class */
}
0

1 Answer 1

3

No, this is simply not possible with just CSS.

The best you could do:

.active,
item:first-child .item_text {
  color: red;
}

item:first-child .item_text {
  background: rgba(255, 255, 255, 1);
}

It is possible if you used a CSS preprocessor like LESS or SASS, which among others extend CSS with functionality like including/extending classes.

In LESS

item:first-child .item_text {
  background: rgba(255, 255, 255, 1);
  .active;
}

This would literally replace that classname with the color: red; line.

In SASS (from version 3, which uses the "SCSS" syntax):

item:first-child .item_text {
  background: rgba(255, 255, 255, 1);
  @extend .active;
}

This would render the same output as my CSS example above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.