Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding transition to a different property

I have an element with two classes:

<div class="class1 class2"></div>

.class1 {transition: background-color 0.4s;}
.class2 {transition: top 1s;}

The problem is that the transition of class2 overwrites the transition of class1.

I can't use .class2 {transition: all 1s} because the transition duration must be different.

I also don't want to duplicate the code from class1 to class2 because class2 can be applied to other elements as well.

Is there a way to add transitions to an element without overwriting existing ones?

like image 215
Tzach Avatar asked May 12 '14 15:05

Tzach


People also ask

How do you transition multiple properties?

Transitioning two or more properties You can transition two (or more) CSS properties by separating them with a comma in your transition or transition-property property. You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.

What is Property transition?

The transition-property property specifies the name of the CSS property the transition effect is for (the transition effect will start when the specified CSS property changes). Tip: A transition effect could typically occur when a user hover over an element.

Are transition properties inherited?

CSS3 Transition Properties Are Not Inherited (In AngularJS)


1 Answers

Unfortunately it's not possible to "extend" a property in another rule in CSS, no matter if it's a transition or another property. Some alternatives may be:

Creating a rule for the combination of the classes

.class1.class2 {transition:background-color 0.4s, top 1s;}

The cons are that you have to create such rule for each of the relevant combinations. This also means code duplication.

Changing the html to eliminate the need to combine the rules

Find a proper way of representing the html objects so you won't need to extend the rules. In my case it's:

<div class="class2">
  <div class="class1">
</div>

The cons are a bit more complicated html. On the other hand you avoid duplicate code, and make your css more reusable.

like image 194
Tzach Avatar answered Nov 29 '22 17:11

Tzach