Mastering CSS Transitions


CSS transitions are a powerful tool in web development that allow for smooth and animated changes in CSS property values over time. They provide a way to add dynamic and visually appealing effects to elements on a webpage without using JavaScript or other scripting languages.

In CSS, transitions are used to specify how an element should smoothly transition from one state to another when a change in a CSS property occurs. This change can be triggered by user interactions, such as hovering over an element, clicking on it, or changing its state using pseudo-classes like :focus or :checked, or through JavaScript-based events.

Transitions are defined using the transition property in CSS, which specifies the duration, timing function, and delay of the transition. The transition property can be applied to any CSS property, such as color, background, size, position, opacity, and more.

Source Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Transition</title>
  <style>
      .box{
        width: 150px;
        height: 150px;
        background-color: orangered;
        /* transition-property: width,height,background-color;
        transition-duration: 1s,2s,0.5s; */

        /* transition-property:all;
        transition-duration: 1s; */

        /* transition:width,height,background-color 1s,2s,0.5s; 
        transition-delay: 2s;
            transition-timing-function: ease;
        */
        /* transition:all 1s 2s ease; */
        transition:all 1s cubic-bezier(0.075, 0.82, 0.165, 1);*/
        transition:2s;
        
      }

      .box:hover{
        width: 300px;
        height: 300px;
        background-color: olivedrab;
      }
  </style>
</head>
<body>
<div class="box"></div>
</body>
</html>
Live Preview

The provided code defines a CSS class .box with initial properties of width: 150px, height: 150px, and background-color: orangered. It also includes a transition property transition: 2s; which specifies that any changes in the properties of .box should transition smoothly over a duration of 2 seconds.

The :hover pseudo-class is used to define the styles that should be applied to .box when it is hovered over by the user. In this case, the width and height properties are changed to 300px, and the background-color property is changed to olivedrab. These changes will also be transitioned smoothly over a duration of 2 seconds due to the transition property defined on the .box class.

In summary, when an element with the class .box is hovered over by the user, it will smoothly transition from its initial size and background color to a larger size and a different background color over a duration of 2 seconds, resulting in a visual animation effect.

CSS transitions offer a wide range of customization options, such as using different timing functions (easing), specifying multiple properties to transition simultaneously, setting delays, and more. They can be used creatively to enhance the user experience by adding subtle animations and visual effects to elements on a webpage.


CSS Tutorial


Properties Reference


Cascading Style Sheet


Flexbox Tutorial


CSS Grid Tutorial


Transitions Properties


CSS Properties with Examples


CSS Selectors


CSS Pseudo Elements


CSS Attribute Selectors


Input Pseudo Classes


CSS Examples


CSS Animation Projects