Introduction to CSS
Text and Font Styling
Box Model and Layout
Advanced Layout with Flexbox and Grid
Responsive Design
CSS Transitions and Animations

Smooth transitions in CSS are used to create a gradual change from one state to another, enhancing user experience by making changes appear more fluid and less abrupt. This is achieved using the ‘transition‘ property, which allows you to define the timing, duration, and easing function for the transition.

Key Concepts of CSS Transitions

  1. Properties to Transition: The CSS properties you want to animate. Commonly animated properties include width, height, opacity, color, background-color, transform, etc.
  2. Duration: How long the transition will take to complete. This is specified in seconds (s) or milliseconds (ms).
  3. Timing Function: The speed curve of the transition. Common values are ease, linear, ease-in, ease-out, and ease-in-out.
  4. Delay: Time to wait before starting the transition. This is also specified in seconds or milliseconds.

Basic Syntax

				
					selector {
  transition: property duration timing-function delay;
}

				
			

Example 1: Transition on Hover

Let’s create a simple example where a button changes its background color smoothly when hovered over.

				
					<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .button {
      background-color: #3498db;
      color: white;
      padding: 15px 30px;
      border: none;
      cursor: pointer;
      font-size: 16px;
      transition: background-color 0.3s ease; /* Applying transition */
    }

    .button:hover {
      background-color: #2980b9; /* New background color on hover */
    }
  </style>
  <title>Smooth Transitions</title>
</head>
<body>
  <button class="button">Hover Me</button>
</body>
</html>

				
			
  • The ‘transition:' 'background-color 0.3s ease;‘ specifies that the ‘background-color‘ property will take 0.3 seconds to change, following the ‘ease‘ timing function.
  • When the button is hovered over, its background color changes from ‘#3498db‘ to ‘#2980b9‘ smoothly.

Example 2: Multiple Property Transitions

You can also transition multiple properties at the same time. Let’s enhance the previous example to include a size change.

				
					<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .button {
      background-color: #3498db;
      color: white;
      padding: 15px 30px;
      border: none;
      cursor: pointer;
      font-size: 16px;
      transition: background-color 0.3s ease, transform 0.3s ease;
    }

    .button:hover {
      background-color: #2980b9;
      transform: scale(1.1); /* Slightly increase the size on hover */
    }
  </style>
  <title>Smooth Transitions</title>
</head>
<body>
  <button class="button">Hover Me</button>
</body>
</html>

				
			
  • The ‘transition‘ property now includes both ‘background-color‘ and ‘transform', each with the same duration and timing function.
  • On hover, the button not only changes color but also slightly increases in size, creating a more dynamic effect.

Example 3: Custom Timing Functions

Custom timing functions can be defined using ‘cubic-bezier‘ values for more control over the transition. Here’s an example using a custom timing function.

				
					<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .button {
      background-color: #3498db;
      color: white;
      padding: 15px 30px;
      border: none;
      cursor: pointer;
      font-size: 16px;
      transition: background-color 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
    }

    .button:hover {
      background-color: #2980b9;
    }
  </style>
  <title>Smooth Transitions</title>
</head>
<body>
  <button class="button">Hover Me</button>
</body>
</html>

				
			
  • The cubic-bezier function creates a custom timing function, providing more control over the transition’s acceleration curve.

Example 4: Transition Delay

You can add a delay before the transition starts.

				
					<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .button {
      background-color: #3498db;
      color: white;
      padding: 15px 30px;
      border: none;
      cursor: pointer;
      font-size: 16px;
      transition: background-color 0.3s ease 0.2s; /* Adding a 0.2s delay */
    }

    .button:hover {
      background-color: #2980b9;
    }
  </style>
  <title>Smooth Transitions</title>
</head>
<body>
  <button class="button">Hover Me</button>
</body>
</html>

				
			
  • The transition: background-color 0.3s ease 0.2s; adds a 0.2-second delay before starting the transition.

These examples demonstrate how to use CSS transitions to create smooth, visually appealing animations for different properties and scenarios. Experimenting with different properties, durations, and timing functions can help you achieve the desired effect in your projects.

Scroll to Top