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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Trending

  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  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
DZone Core CORE ·
Feb. 06, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.2K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!