DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Death of the CSS Selector: Architecting Resilient, AI-Powered Web Scrapers
  • A Guide to Parallax and Scroll-Based Animations
  • Building a Card Layout Using CSS Subgrid
  • CSS Hide and Seek: Choosing Between display: none and visibility: hidden

Trending

  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  • Hallucination Has Real Consequences — Lessons From Building AI Systems
  • How to Prevent Data Loss in C#
  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes
  1. DZone
  2. Coding
  3. Languages
  4. CSS Transitions Generator

CSS Transitions Generator

Let's take an in-depth look at how CSS transitions work, along with experimenting with a CSS transition generator to generate any transition you want.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Feb. 06, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

CSS transitions give us the ability to smoothly transition from one set of styles to another. Without them, your hover, click and transform effects can look janky and sudden. 

To illustrate a CSS transition, below are two emojis. Click on them to see the difference:

CSS Transition Generator

As part of this tutorial, I've created a CSS transition generator that will let you play around with a transition and see how it works. Use this tool to generate your own custom transitions. Timing in transitions is controlled either by keywords, such as ease-in, or by the cubic-bezier function, which you can manipulate below. 

If you want to learn more, read the rest of this article!

When Are CSS Transitions Fired?

When a user initiates a new state, i.e. focuses or hovers over an element with :focus or :hover, styles will change. Similarly, if you add a new class with Javascript to an HTML element that adds new styles, the style will similarly change.

In both of these situations, CSS transitions will be fired. Essentially, then, a transition is fired when a CSS element changes, whether through an event or by adding a class.

How To Use CSS Transitions

CSS Transitions can be added using the transition CSS property. Typically, we just use this shorthand property, which can be summarized as:

transition: [transition-property] [transition-duration] [transition-timing-function] [transition-delay]

Let's think about what each of these means:

  1. Transition-property - this is the property we want to transition. It can be set to all or any CSS property, i.e. background or padding
  2. Transition-duration - this is the length of the transition. It can be set to a time, usually in seconds, i.e. 5s.
  3. Transition-timing function - this is the timing function. It is usually a keyword, which can be ease, linear, ease-in, ease-out, or ease-in-out. There are others, but we'll look at those later.
  4. Transition-delay - this is the delay at the start. If a user hovers, and we have this set to 0.5s, then the hover effect will only start 0.5s after the hover begins.

By default, we only really need to give the first two properties, i.e the following assumes an ease timing function, and no delay:

 
div {
    transition: background 2s;
}


As discussed, the transition is a shorthand, the following shows both short form and long form of the transition property:

 
div {
    transition: background 3s ease-in 0.5s;
    /* Equivalent to.. */
    transition-property: background;
    transition-duration: 3s;
    transition-timing-function: ease-in;
    transition-delay: 0.5s;
}


An Example of a CSS Transition

Let's take a look at a hover transition effect. If you hover over the div below, you'll see the slow transition to the new styles:

 
div {
    color: white;
    text-decoration: underline;
    font-size: 1.25rem;
    background: transparent;
    padding: 0.25rem;
    box-shadow: none;
    cursor: pointer;
    /* The transition property: */
    transition: all 0.5s ease-out;
}
div:hover {
    padding: 0.5rem;
    background: white;
    color: black;
    box-shadow: 0 2px 25px rgba(255,255,255,0.5);
}


Other Easing Functions

We've spoken a little bit about easing functions, and the keywords we can use, but those aren't the only ones we can use. There are also a few other easing functions that can be quite useful for creating varied transition effects.

Cubic Bezier Curves

One of the most used transition timing functions is a cubic bezier curve. An example of that is shown below:

 
div {
    transition: all 0.5s cubic-bezier(.17,.67,1,-0.52);
}


This allows us to have transitions go back and forwards based on the curve shape. The above transition can be seen below:


If you want to generate your own cubic bezier curves, you can do it through various tools found online, or via the Google Chrome Dev tools when editing a transition property.

Steps

Less used, but equally useful. It breaks the transition into a set of evenly spaced steps, giving a sort of jumpy effect when transitioning between two states.

Steps are defined as a number of steps and a keyword. The keyword determines whether the jump starts at the beginning of the CSS change (jump-start) or if it aligns with the end instead (jump-end), or both (jump-both).

 
div {
    transition: all 2s step(6, jump-start);
}


This allows us to have transitions go back and forwards based on the curve shape. The above transition can be seen below:

Conclusion

I hope you've enjoyed this guide on CSS transitions. You can use our CSS Transition generator above to generate your own custom cubic-bezier transitions, and test them out. Check out our other tutorials and guides for more. 

CSS

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Death of the CSS Selector: Architecting Resilient, AI-Powered Web Scrapers
  • A Guide to Parallax and Scroll-Based Animations
  • Building a Card Layout Using CSS Subgrid
  • CSS Hide and Seek: Choosing Between display: none and visibility: hidden

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook