The Wayback Machine - https://web.archive.org/web/20240922124402/https://www.geeksforgeeks.org/css-transitions/
Open In App

CSS Transitions

Last Updated : 23 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS transitions enable web developers to control the smooth transition between two states of an element. For instance, when a user hovers over a button, the background color of the element can change seamlessly using CSS selectors and pseudo-classes.

Transitions are not limited to color changes; they can animate various properties, enhancing user experience and interactivity by making changes visually appealing. This article demonstrates how to animate transitions between CSS properties using four key transition properties: transition-property, transition-duration, transition-timing-function, and transition-delay.

Key CSS Transition Properties

To create effective transitions, you should use at least two of the four key properties: transition-property and transition-duration. Here’s a detailed look at each property:

1. transition-property:

This property allows you to select the CSS properties that you want to animate during the transition(change). 

Syntax:

transition-property: none | all | property | property1,
property2, ..., propertyN;
  • Values:
    • none is used to specify that no property should be selected.
    • all is used to specify all the properties to be selected, though not all properties are animate-able, only the properties which are animate-able will be influenced.
    • We can specify a single property or a set of comma-separated properties property1, property2, …, propertyN.

2. transition-duration:

This property allows you to determine how long it will take to complete the transition from one CSS property to the other. 

Syntax:

transition-duration: time;
  • Here, time can be in seconds(s) or milliseconds(ms), you should use ‘s’ or ‘ms’ after the number (without quotes).

3. transition-timing-function:

This property allows you to determine the speed of change and the manner of change, during the transition. Like, the change should be fast at the beginning and slow at the end, etc. 

Syntax:

transition-timing-function: ease|ease-in|ease-out|ease-in-out|linear|
step-start|step-end;
  • Note, there are other values that this transition-timing-function can take, only the most frequent and simple are mentioned here.

4. transition-delay:

This property allows you to determine the amount of time to wait before the transition actually starts to take place. 

Syntax:

transition-delay: time;
  • Here, again, time can be in seconds(s) or milliseconds(ms), and you should use ‘s’ or ‘ms’ after the number (without quotes).

Shorthand Property

You can combine all four transition properties into a single shorthand property, which simplifies the code and ensures readability.

Syntax:

transition: (property name) | (duration) | (timing function) | (delay);

The value is taken by are same as mentioned above. This property must be placed with other CSS properties, if any, of the initial state. You should use at least, property name and duration to get any animate-able effect. Also, the ordering of the values matters. The first value is of the property name, second for the duration and so on, as listed above. So, if only one number is mentioned, it will be taken up as duration, and not as a delay. 

Example: Changing property without using transitions. 

html
<!DOCTYPE html>
<html>
<head>
    <title>CSS Transition</title>

    <style>
        h1 {
            color: green;
            text-align: center;
        }

        div.one {
            height: 150px;
            width: 150px;
            border: 1px dashed black;
            margin: 0 auto;
            background: #FFEBEE;
        }

        div.one:hover {
            height: 300px;
            width: 300px;
            background: #BBDEFB;
        }
    </style>

</head>

<body>
    <h1>GeeksForGeeks</h1>

    <div class="one">
    </div>

</body>
</html>

Output: change-without-transition 

Practical Examples

Example: Changing property using transitions. 

html
<!DOCTYPE html>
<html>
<head>
    <title>CSS Transition</title>

    <style>
        h1 {
            color: green;
            text-align: center;
        }

        div.one {
            height: 150px;
            width: 150px;
            border: 1px dashed black;
            margin: 0 auto;
            background: #FFEBEE;
            transition: height 2s, width 2s, background 2s;
        }

        div.one:hover {
            height: 300px;
            width: 300px;
            background: #BBDEFB;
        }
    </style>

</head>

<body>
    <h1>GeeksForGeeks</h1>
    <div class="one">
    </div>

</body>
</html>

Output: change-with-transition

CSS transitions are a powerful tool for creating smooth and visually appealing transitions between different states of an element. By understanding and utilizing the four key properties—transition-property, transition-duration, transition-timing-function, and transition-delay—developers can enhance user interactivity and experience. The shorthand property simplifies the process, making it easier to manage and apply transitions. Experiment with the provided examples to see the impact of transitions on your web design projects and elevate the overall user experience

Supported browsers:

The browsers supported by Transition are listed below:

  • Google Chrome 26
  • Firefox 16
  • Opera 12.1
  • Safari 9
  • Edge 12

CSS Transitions – FAQs

What are CSS transitions?

CSS transitions allow you to change property values smoothly (over a given duration) when a specified event (such as hovering over an element) occurs.

How do I create a basic CSS transition?

To create a basic CSS transition, you specify the CSS property you want to animate, the duration of the transition, and optionally the timing function and delay.

What properties can be transitioned using CSS transitions?

Most CSS properties that have numeric values or colors can be transitioned. Examples include width, height, margin, padding, background-color, opacity, and transform.

How do I make a transition run infinitely?

Transitions cannot run infinitely by themselves, but you can use the animation property for continuous animations.

What are some common uses of CSS transitions?

  • Creating hover effects on buttons and links
  • Smoothly changing the size or position of elements
  • Fading in or out elements
  • Creating accordion or toggle effects
  • Enhancing user interactions with visual feedback


Previous Article
Next Article

Similar Reads

Foundation CSS Motion UI Built-in Transitions
Foundation CSS is one of the best responsive frameworks. The CSS framework adapts to any device, screen, and is accessible widely. We can make beautiful responsive websites with the help of foundation CSS. It is a highly customizable framework. It is an open-source framework built by the ZURB foundation. Foundation CSS Motion UI is a library for cr
3 min read
Foundation CSS Off-canvas Transitions
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
3 min read
Why are CSS animations and transitions blocked by JavaScript ?
When JavaScript is used to alter the CSS properties on which the animations and transitions are based, CSS animations and transitions may be prevented by JavaScript. The animation or transition may stop suddenly or fail to show altogether if the properties are altered before it has finished. This happens because JavaScript is changing the page too
6 min read
How to Apply Transitions to the Display Property in CSS ?
In CSS, transitions allow you to change property values smoothly over a specified duration. However, the display property is not animatable, which means you cannot directly apply transitions to it like you can with properties such as opacity or transform. This limitation is because the display property does not support values between its states (e.
3 min read
How to make a Realistic Motion Blur with CSS Transitions ?
In this article, we are going to create a realistic Motion Blur effect with CSS Transitions. Motion Blur is a special kind of effect that we can see in various online videos, movies, or animated clips. When an object moves from one place to another place with some fair speed then it gets blurred which makes the user infer that the object is moving
5 min read
How to create multiple transitions on an element using CSS ?
In this article, we are going to learn how we can have multiple transitions on an element. Generally, we apply or create more than one transition to create some effects in our design. In CSS there are certain properties that can be transitioned. Approach: To have multiple transitions on an element, basically we have two ways. One is specifying the
3 min read
Tailwind CSS Transitions and Animation Complete Reference
Tailwind CSS is a highly customizable, low-level CSS framework that gives you all of the buildings blocks that you need. Tailwind CSS Transitions utilities control the transition and animation of elements. it provides transition classes like transition Property, duration, timing function, delay to manage elements perfectly. Tailwind CSS Class Descr
2 min read
How to create a Transitions popup using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating a transition popup button using jQuery Mobile. Approach: Add jQuery mobile scripts needed for your project. &lt;link rel=”stylesheet” href=”http://code.jquery.com/mobile/1.4.5/j
2 min read
Vue.js Dynamic List Transitions
Dynamic List TransitionDynamic List Transitions defines as the animated transitions that occur when the elements are added, remove, or rearranged in a list. This process wraps the list in transition components by adding CSS for transition and adding animations to the transition components. Implementing Dynamic List TransitionAdding Transitions: The
4 min read
React MUI Transitions Util
React MUI transition component enables you to define a change from one component state to another across time. Although it's most frequently used for animation component mounting and unmounting, it can also be utilized to depict in-place transition states. It's up to you to give those states purpose and impact. Components available in React MUI tra
6 min read
D3.js Transitions API Complete Reference
The D3 is an abbreviation of Data-Driven Documents, and D3.js is a resource JavaScript library for managing documents based on data. D3 is one of the most effective frameworks to work on data visualization. Shapes Transitions: D3.js selection.transaction() FunctionD3.js selection.interrupt() FunctionD3.js interrupt() FunctionD3.js transition() Func
1 min read
Vue.js List Entering &amp; Leaving Transitions
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. The Entering and Leaving Transitions are used to perform the animation on lists, whether ordered or unordered lists. The animati
3 min read
React Chakra UI Other Transitions
React Chakra UI has a Transition component which has types like Fade, ScaleFade, SlideFade, and Collapse. All these types provide animation effects and smooth transitions in the application. By using the transition types in the application, the appearance of the elements is enhanced and the application becomes more engaging. In this article, we wil
3 min read
Animation and Transitions using React Hooks
Animations allows to control the elements by changing their motions or display. Animation is a technique to change the appearance and behaviour of various elements in web pages. Transitions in CSS allow us to control the way in which transition takes place between the two states of the element. We can use the transitions to animate the changes and
6 min read
Vue.js Reusable List Transitions
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance.  We can create Single Page Applications as well as Full Stack applications. Vue.js Reusable List transitions allow the developers to create one animation and then use it across all the components. In the
4 min read
Vue.js Staggering List Transitions
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. Staggering List Transitions can be performed in the lists by communicating between the javascript transition and data attributes
2 min read
Next.js Built-in Loader Feature for Smooth Page Transitions
Next.js offers a built-in loader feature that enhances user experience by providing smooth page transitions. This feature helps to inform users that the application is loading, reducing perceived waiting times and improving overall usability. In this article, we'll explore the Next.js Loader feature by creating a "loading.js" component that will be
3 min read
Vue.js List Move Transitions
Vue.js is a progressive JavaScript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. The Vue.js List Move Transitions smooths the transition when an element is added or removed from the list. In entering and leavi
3 min read
Difference between bootstrap.css and bootstrap-theme.css
Bootstrap is an HTML, CSS and JS front-end-framework used for developing responsive and mobile-first projects on the web. It is the framework that comes up with predefined terms, thus giving you the ability to use these codes instead of having to create the codes from scratch. It is a collection of HTML, CSS and JS code that is designed to help bui
5 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
CSS Cheat Sheet - A Basic Guide to CSS
What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is a CSS Cheat Sheet? CSS Cheat Sheet provides you wi
13 min read
How to identify unused CSS definitions from multiple CSS files in a project ?
In this article, we will learn how to identify unused CSS definitions from multiple CSS files in a project. What are unused CSS definitions? Unused CSS definitions are the stylesheets that are completely useless for rendering and loading the page. If you remove these unused CSS definitions then nothing will happen to your webpage. Why you should re
2 min read
Create a CSS Fireflies background using HTML/CSS
In this article, we will see how to create the CSS Fireflies background using HTML &amp; CSS, along with understanding its implementation through the illustration. The CSS fireflies effect can be created by utilizing the CSS animation and we will implement 3 animations to achieve the fireflies effect. To create randomness, we used different animati
7 min read
Using Primer CSS - A Simple and Transparent CSS Library for Web Development
Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation of a document written in HTML or XML. CSS helps you to control the layout and appearance of web pages. In this article, we will discuss the concept of Primer CSS, a CSS library that provides a simple and easy-to-use set of styles for web development. Obvious an
3 min read
How to Add a @tailwind CSS Rule for CSS Checker ?
Tailwind CSS is basically a Utility first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. In this article, we will ex
4 min read
CSS pseudo elements every CSS developer should know
CSS pseudo-elements are used to add special effects/styles to a specific part of the selected elements. A CSS selector selects the HTML element(s) for styling purposes. CSS selectors select HTML elements according to its id, class, type, attribute etc. For example, the ::before and ::after pseudo-elements can be used to insert content before or aft
5 min read
CSS Complete Guide - A to Z CSS Concepts
What is CSS ? CSS stands for "Cascading Style Sheet". It is used to style HTML Documents. CSS simplifies the process of making web pages presentable. It describes how web pages should look it prescribes colors, fonts, spacing, and much more. What is CSS Complete Guide ? CSS Complete Guide is a list of A to Z CSS concepts from beginner to advanced l
5 min read
Styling React Components: CSS vs CSS-in-JS
When it comes to styling React components, developers often face the dilemma of choosing between traditional CSS and CSS-in-JS solutions. Both approaches offer unique advantages and drawbacks, influencing how developers design and maintain their application's user interface. In this article, we delve into the differences between CSS and CSS-in-JS,
5 min read
How to use CSS Animations with Tailwind CSS ?
Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility ClassesCSS Animations with Tailwind CSS and JavaScriptT
3 min read
How to override the CSS properties of a class using another CSS class ?
To override CSS properties with another class, use the `!important` directive in CSS, emphasizing a style's importance, overriding others. Applying a new class to an element replaces or modifies styles from its original class, allowing targeted adjustments to appearance, layout, or behavior. Table of Content Using !important propertyUsing specific
3 min read