Tillitsdone
down Scroll to discover

CSS Transition Enhance Web Design with Smooth Animations

Learn how to use the CSS transition property to create smooth animations.

Discover available options for duration, timing functions, and delays.
thumbnail

Introduction

The transition property in CSS is a powerful tool for web developers and designers. It allows you to create smooth transitions between different states of an element, making your web pages more dynamic and engaging. You can animate changes to properties like color, size, and position using pseudo-classes like :hover or :active, or dynamically with JavaScript. This property is a shorthand for several transition properties, making it easy to specify the exact behavior you want.

By using the transition property effectively, you can enhance the user experience on your website, making it more visually appealing and interactive. Let’s dive in and learn how to use this powerful CSS feature!

Specification

The transition property is part of the CSS Transitions module. It defines the behavior of transitions, allowing you to create smooth animations on your website. The transition property is a shorthand for several other properties, making it easy to specify the exact behavior you want.

The official specification for the transition property can be found in the CSS Transitions Module Level 1. This specification outlines the syntax, constituent properties, and behavior of the transition property. It provides a detailed explanation of how transitions work, including timing functions, delays, and durations.

Understanding the specification is crucial for using transitions effectively. By following the guidelines, you can create smooth and visually appealing animations that enhance the user experience on your website.

Description

The transition property in CSS allows you to create smooth animations between different states of an element. It is a shorthand property that combines several transition-related properties into a single declaration. This makes it easier to define how an element should transition from one state to another, without the need for complex JavaScript animations.

By using the transition property, you can specify which properties should animate, the duration of the transition, the timing function to control the speed of the transition, and any delay before the transition starts. This gives you fine-grained control over the animation behavior, allowing you to create visually appealing and interactive web designs.

Constituent Properties

The transition property in CSS is a shorthand that combines multiple individual transition properties. Understanding these constituent properties is key to effectively using the transition shorthand. Here are the individual properties that make up the transition shorthand:

  1. transition-property:
    • Specifies the CSS properties to which a transition effect should be applied. You can target specific properties or use the keyword all to apply the transition to all properties that change.
  2. transition-duration:
    • Defines the length of time a transition animation should take to complete. This is specified in seconds (s) or milliseconds (ms).
  3. transition-timing-function:
    • Specifies the speed curve of the transition effect. Common timing functions include ease, ease-in, ease-out, ease-in-out, and linear. Custom timing functions can be defined using cubic-bezier() or steps().
  4. transition-delay:
    • Sets the delay before the transition effect starts. This is specified in seconds (s) or milliseconds (ms). A positive value delays the start of the transition, while a negative value starts the transition immediately but offsets the animation’s start point.

By combining these properties, you can create complex and nuanced transition effects with minimal code.

Syntax

The transition property in CSS is used to define transitions between different states of an element. The syntax for the transition property is straightforward and allows you to specify multiple transition effects in a single declaration.

Here is the basic syntax for the transition property:

transition: [<transition-property>] [<transition-duration>] [<transition-timing-function>] [<transition-delay>];

Each part of the syntax is optional, and the order in which they are specified matters. Here’s a breakdown of each component:

  1. <transition-property>: Specifies the CSS properties to which the transition effect should be applied. This can be a single property, multiple properties separated by commas, or the keyword all to apply the transition to all animatable properties.
  2. <transition-duration>: Defines the length of time the transition animation should take to complete. This is specified in seconds (s) or milliseconds (ms).
  3. <transition-timing-function>: Specifies the speed curve of the transition effect. Common timing functions include ease, ease-in, ease-out, ease-in-out, and linear. You can also define custom timing functions using cubic-bezier() or steps().
  4. <transition-delay>: Sets the delay before the transition effect starts. This is specified in seconds (s) or milliseconds (ms). A positive value delays the start of the transition, while a negative value starts the transition immediately but offsets the animation’s start point.

Examples of Syntax

Here are some examples of how to use the transition property:

/* Apply transition to a single property */
transition: background-color 2s;
/* Apply transition with duration and delay */
transition: background-color 2s 1s;
/* Apply transition with duration and timing function */
transition: background-color 2s ease-in-out;
/* Apply transition with duration, timing function, and delay */
transition: background-color 2s ease-in-out 1s;
/* Apply transition to multiple properties */
transition: background-color 2s, width 1s;
/* Apply transition to all properties */
transition: all 0.5s;
/* Global values */
transition: inherit;
transition: initial;
transition: revert;
transition: revert-layer;
transition: unset;

Special Values

  • none: Specifies that no transitions will occur on this element. This is the default value.
  • inherit: Inherits the transition property from the element’s parent.
  • initial: Sets the transition property to its default value.
  • revert: Reverts the transition property to the value specified by the user agent’s default stylesheet.
  • unset: Resets the transition property to its inherited value if it inherits; otherwise, it resets to its initial value.

Multiple Transitions

You can specify multiple transitions by separating them with commas. For example:

transition: background-color 2s, width 1s;

In this case, the background color will transition over 2 seconds, and the width will transition over 1 second.

By understanding the syntax and options available for the transition property, you can create smooth and engaging animations that enhance the user experience on your website.

Formal Definition

The transition property in CSS is formally defined as a shorthand for several individual transition properties. This shorthand allows you to specify multiple transition-related properties in a single declaration, making it easier to manage and apply transitions to your elements.

Here is the formal definition of the transition property:

| Initial Value | As each of the properties of the shorthand:

  • transition-delay: 0s
  • transition-duration: 0s
  • transition-property: all
  • transition-timing-function: ease
Applies toAll elements, including ::before and ::after pseudo-elements
InheritedNo
Computed ValueAs each of the properties of the shorthand:
  • transition-delay: As specified
  • transition-duration: As specified
  • transition-property: As specified
  • transition-timing-function: As specified | | Animation Type | Not animatable |

Constituent Properties

The transition property is a shorthand for the following CSS properties:

  1. transition-property: Specifies the CSS properties to which the transition effect should be applied.
  2. transition-duration: Defines the length of time the transition animation should take to complete.
  3. transition-timing-function: Specifies the speed curve of the transition effect.
  4. transition-delay: Sets the delay before the transition effect starts.

Default Values

  • transition-delay: 0s
  • transition-duration: 0s
  • transition-property: all
  • transition-timing-function: ease

Computed Values

  • transition-delay: As specified
  • transition-duration: As specified
  • transition-property: As specified
  • transition-timing-function: As specified

Animation Type

The transition property itself is not animatable. This means that you cannot animate the transition property itself, but you can animate the properties that it affects.

Applies to

The transition property can be applied to all elements, including ::before and ::after pseudo-elements. This makes it a versatile tool for creating animations throughout your web pages.

Understanding the formal definition of the transition property is crucial for effectively using CSS transitions in your web development projects. By knowing the default values, computed values, and the elements to which it applies, you can create smooth and engaging animations that enhance the user experience on your website.

Formal Syntax

The transition property in CSS follows a specific syntax that allows you to define how transitions should be applied to your elements. The formal syntax for the transition property is structured to be both flexible and powerful, enabling you to control various aspects of the transition effect.

Here is the formal syntax for the transition property:

transition: <single-transition> [, <single-transition>]*

Components of the Syntax

  1. <single-transition>:
    • Represents a single transition effect that can be applied to one or more properties. It can include the following components:
      • <transition-property>: Specifies the CSS properties to which the transition effect should be applied.
      • <transition-duration>: Defines the length of time the transition animation should take to complete.
      • <transition-timing-function>: Specifies the speed curve of the transition effect.
      • <transition-delay>: Sets the delay before the transition effect starts.

Detailed Breakdown

  1. <single-transition>:
    • This component can be defined as:
      <single-transition> = [none | <single-transition-property> || <time> || <easing-function> || <time>]
  2. <single-transition-property>:
    • Specifies the property or properties to which the transition effect should be applied. It can be:
      <single-transition-property> = all | <custom-ident>
  3. <easing-function>:
    • Specifies the speed curve of the transition effect. It can be:
      <easing-function> = <linear-easing-function> | <cubic-bezier-easing-function> | <step-easing-function>
  4. <linear-easing-function>:
    • Defines a linear transition effect. It can be:
      <linear-easing-function> = linear | <linear()>
  5. <cubic-bezier-easing-function>:
    • Defines a cubic Bezier curve for the transition effect. It can be:
      <cubic-bezier-easing-function> = ease | ease-in | ease-out | ease-in-out | <cubic-bezier()>
  6. <step-easing-function>:
    • Defines a step transition effect. It can be:
      <step-easing-function> = step-start | step-end | <steps()>
  7. <linear()>:
    • Defines a custom linear transition effect. It can be:
      <linear()> = linear([<number> && <percentage>]{0,2})
  8. <cubic-bezier()>:
    • Defines a custom cubic Bezier curve for the transition effect. It can be:
      <cubic-bezier()> = cubic-bezier([<number [0,1]>, <number>]{2})
  9. <steps()>:
    • Defines a custom step transition effect. It can be:
      <steps()> = steps(<integer>, <step-position>? )
  10. <step-position>:
    • Specifies the step position for the transition effect. It can be:
      <step-position> = jump-start | jump-end | jump-none | jump-both | start | end

Examples

Here are some examples of how to use the transition property with the formal syntax:

/* Apply transition to a single property */
transition: background-color 2s;
/* Apply transition with duration and delay */
transition: background-color 2s 1s;
/* Apply transition with duration and timing function */
transition: background-color 2s ease-in-out;
/* Apply transition with duration, timing function, and delay */
transition: background-color 2s ease-in-out 1s;
/* Apply transition to multiple properties */
transition: background-color 2s, width 1s;
/* Apply transition to all properties */
transition: all 0.5s;
/* Global values */
transition: inherit;
transition: initial;
transition: revert;
transition: revert-layer;
transition: unset;

By understanding the formal syntax and options available for the transition property, you can create smooth and engaging animations that enhance the user experience on your website.

/* Apply transition to a single property with duration */
transition: background-color 2s;
/* Apply transition with duration and delay */
transition: background-color 2s 1s;
/* Apply transition with duration and timing function */
transition: background-color 2s ease-in-out;
/* Apply transition with duration, timing function, and delay */
transition: background-color 2s ease-in-out 1s;
/* Apply transition to multiple properties */
transition: background-color 2s, width 1s;
/* Apply transition to all properties */
transition: all 0.5s;
/* Global values */
transition: inherit;
transition: initial;
transition: revert;
transition: revert-layer;
transition: unset;

Special Values

  • none: No transitions will occur. This is the default value.
  • inherit: Inherits the transition property from the element’s parent.
  • initial: Sets the transition property to its default value.
  • revert: Reverts the transition property to the value specified by the user agent’s default stylesheet.
  • unset: Resets the transition property to its inherited value if it inherits; otherwise, it resets to its initial value.

Understanding the formal syntax of the transition property is essential for creating precise and effective transitions in your web development projects. By mastering the syntax, you can control the behavior of your animations and create more engaging and dynamic web designs.

Examples

Let’s explore various examples of the transition property in action. These examples will demonstrate how to use the transition property to create smooth and engaging animations on different elements.

Basic Example

This example shows how to change the background color of an element when it is hovered over.

Width and Height Transition

This example demonstrates how to change the width and height of an element smoothly when it is hovered over.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.rectangle {
width: 100px;
height: 50px;
background-color: red;
transition: width 0.3s ease, height 0.3s ease;
}
.rectangle:hover {
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="rectangle"></div>
</body>
</html>

CSS

.rectangle {
width: 100px;
height: 50px;
background-color: red;
transition: width 0.3s ease, height 0.3s ease;
}
.rectangle:hover {
width: 200px;
height: 100px;
}

In this example, the transition property is used to animate the width and height changes over 0.3 seconds with an ease timing function when the element is hovered over.

Multiple Transitions

This example demonstrates how to apply multiple transitions to different properties of an element.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.circle {
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 50%;
transition: background-color 1s, border-radius 1s;
}
.circle:hover {
background-color: orange;
border-radius: 0%;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="circle"></div>
</body>
</html>

CSS

.circle {
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 50%;
transition: background-color 1s, border-radius 1s;
}
.circle:hover {
background-color: orange;
border-radius: 0%;
}

In this example, the transition property is used to animate the background color and border-radius changes over 1 second when the element is hovered over.

Transition with Delay

This example demonstrates how to use the transition property with a delay before the transition starts.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.delayed-box {
width: 100px;
height: 100px;
background-color: purple;
transition: background-color 2s 1s;
}
.delayed-box:hover {
background-color: pink;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="delayed-box"></div>
</body>
</html>

CSS

.delayed-box {
width: 100px;
height: 100px;
background-color: purple;
transition: background-color 2s 1s;
}
.delayed-box:hover {
background-color: pink;
}

In this example, the transition property is used to animate the background color change over 2 seconds, with a 1-second delay before the transition starts when the element is hovered over.

Transition with Timing Function

This example demonstrates how to use the transition property with a custom timing function.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.custom-timing {
width: 100px;
height: 100px;
background-color: teal;
transition: background-color 2s cubic-bezier(0.42, 0, 0.58, 1);
}
.custom-timing:hover {
background-color: cyan;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="custom-timing"></div>
</body>
</html>

CSS

.custom-timing {
width: 100px;
height: 100px;
background-color: teal;
transition: background-color 2s cubic-bezier(0.42, 0, 0.58, 1);
}
.custom-timing:hover {
background-color: cyan;
}

In this example, the transition property is used to animate the background color change over 2 seconds with a custom cubic-bezier timing function when the element is hovered over.

Basic Example

This example shows how to change the background color of an element when it is hovered over.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 0.5s ease;
}
.box:hover {
background-color: green;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="box"></div>
</body>
</html>

CSS

.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 0.5s ease;
}
.box:hover {
background-color: green;
}

In this example, the transition property is used to animate the background color change over 0.5 seconds with an ease timing function when the element is hovered over.

Explanation

In this example, an HTML element with the class box has a width and height of 100 pixels and a blue background color. The transition property is applied to the box class, specifying that the background-color property should transition over 0.5 seconds with an ease timing function. When the user hovers over the element, the background color changes to green, creating a smooth animation effect.

Output

![CSS Transition Example]WebsiteUrl

Supported Browsers

The transition property in CSS is widely supported across modern web browsers, making it a reliable choice for creating smooth and engaging animations on your website. Here is a list of the major browsers that support the transition property and the versions starting from which it is supported:

BrowserVersionRelease Date
Google Chrome26.0March 2013
Mozilla Firefox16.0October 2012
Microsoft Edge12.0July 2015
Internet Explorer10.0September 2012
Safari6.1June 2013
Opera12.1November 2012

Note on Browser Compatibility

While the transition property is widely supported, it’s always a good practice to test your website on different browsers and versions to ensure that your animations work as expected. This is especially important if you are targeting a broad audience that may use a variety of browsers and devices.

By knowing the supported browsers and versions, you can ensure that your CSS transitions are compatible and accessible to the majority of your users. This will help you create a consistent and engaging user experience across different platforms.

Conclusion

The transition property in CSS is a powerful tool for creating smooth and engaging animations on your website. With wide browser support, you can confidently use this property to enhance the user experience and make your web designs more dynamic and visually appealing. Whether you are targeting users on Chrome, Firefox, Edge, Internet Explorer, Safari, or Opera, the transition property is a reliable choice for creating modern and interactive web designs.

CSS Transition FAQs

What is the purpose of the transition property in CSS?

The transition property in CSS lets you smoothly change CSS properties over a set time. This makes animations that enhance your website’s user experience.

Which CSS properties can be transitioned using the transition property?

Most CSS properties like color, background-color, height, width, etc., can be transitioned. This allows smooth visual effects for various properties.

How do I specify multiple transitions on the same element?

Separate them with commas:

transition: color 2s, background-color 1s;

Can different properties have different transition durations?

Yes, specify them separately:

transition: color 2s, background-color 1s;

What happens if a transition is on a non-animatable property?

The transition won’t occur, and the property will change instantly. Ensure you’re only transitioning animatable properties.

How do I apply a transition to all properties of an element?

Use the all keyword:

transition: all 0.5s ease;

Can I delay the start of a transition?

Yes, use transition-delay:

transition: background-color 2s 1s;

What is the default value for the transition property?

The default is none, meaning no transitions occur. Without specifying a transition, properties change instantly.

How do I control the speed of the transition?

Use transition-timing-function. Common options include ease, ease-in, ease-out, ease-in-out, and linear. Example:

transition: background-color 2s ease-in-out;

Can I use JavaScript to dynamically set transitions?

Yes, you can manipulate the style property of an element:

element.style.transition = 'background-color 2s ease';

Is the transition property supported in all browsers?

Yes, it’s widely supported in modern browsers. Always test on different browsers and versions to ensure compatibility.

How do I ensure my transitions are accessible to all users?

  • Use appropriate timing functions and durations.
  • Test on various devices and screen sizes.
  • Provide alternative content or descriptions for users who can’t see animations.

Can I use transitions with the display property?

No, use visibility or opacity instead. Example:

transition: opacity 0.5s ease;

How do I troubleshoot issues with CSS transitions?

  • Ensure the properties are animatable.
  • Check the syntax of your transition property.
  • Test in different browsers and devices.
  • Use browser developer tools to inspect elements.

Using Transitions

The transition property in CSS creates smooth animations when an element changes state. It enhances user experience by making your website more dynamic and visually appealing.

When to Use Transitions

  • Hover Effects: Change appearance on hover.
  • Focus States: Highlight interactive elements on focus.
  • Dynamic Changes: Animate changes added/removed via JavaScript.
  • User Interactions: Provide visual feedback for interactions.

Key Concepts

  • Transition Property: Specifies which properties to animate.
  • Transition Duration: How long the animation lasts.
  • Transition Timing Function: Speed curve of the animation.
  • Transition Delay: Delay before the animation starts.

Basic Usage

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.button {
padding: 10px 20px;
background-color: blue;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.5s ease;
}
.button:hover {
background-color: green;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<button class="button">Hover over me</button>
</body>
</html>

CSS

.button {
padding: 10px 20px;
background-color: blue;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.5s ease;
}
.button:hover {
background-color: green;
}

Explanation

  • Initial State: A button with a blue background.
  • Hover State: Background changes to green smoothly over 0.5 seconds.

Advanced Usage

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: red;
transition: width 0.5s ease, height 0.5s ease, background-color 0.5s ease;
}
.box:hover {
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="box"></div>
</body>
</html>

CSS

.box {
width: 100px;
height: 100px;
background-color: red;
transition: width 0.5s ease, height 0.5s ease, background-color 0.5s ease;
}
.box:hover {
width: 200px;
height: 200px;
background-color: yellow;
}

Explanation

  • Initial State: A red square.
  • Hover State: Size and color change smoothly over 0.5 seconds.

Tips for Effective Use

  • Keep Transitions Subtle: Avoid long durations or complex timing functions.
  • Test on Different Devices: Ensure consistency across devices and screen sizes.
  • Use Transitions Sparingly: Avoid overuse to maintain a clean user experience.

More Examples

Example 1: Transitioning Opacity

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.fade-box {
width: 100px;
height: 100px;
background-color: blue;
transition: opacity 0.5s ease;
}
.fade-box:hover {
opacity: 0.5;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="fade-box"></div>
</body>
</html>

Example 2: Transitioning Transform Properties

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.rotate-box {
width: 100px;
height: 100px;
background-color: green;
transition: transform 0.5s ease;
}
.rotate-box:hover {
transform: rotate(45deg);
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="rotate-box"></div>
</body>
</html>

Example 3: Transitioning Font Size

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.text {
font-size: 16px;
transition: font-size 0.5s ease;
}
.text:hover {
font-size: 24px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<p class="text">Hover over this text</p>
</body>
</html>

By following these tips and examples, you can effectively use the transition property to create smooth and engaging animations that enhance the user experience on your website. Sure! Here’s the rewritten content simplified and clarified:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.text {
font-size: 16px;
transition: font-size 0.5s ease;
}
.text:hover {
font-size: 24px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<p class="text">Hover over me</p>
</body>
</html>

Explanation

In this example, the transition property animates the font size change of text with the class text. When you hover over the text, the font size changes from 16px to 24px over 0.5 seconds with an ease timing function.

Examples

Example 1: Transitioning Font Size

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.text {
font-size: 16px;
transition: font-size 0.5s ease;
}
.text:hover {
font-size: 24px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<p class="text">Hover over me</p>
</body>
</html>
CSS
.text {
font-size: 16px;
transition: font-size 0.5s ease;
}
.text:hover {
font-size: 24px;
}

Example 2: Transitioning Border Radius

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.border-box {
width: 100px;
height: 100px;
background-color: purple;
border-radius: 0;
transition: border-radius 0.5s ease;
}
.border-box:hover {
border-radius: 50%;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="border-box"></div>
</body>
</html>
CSS
.border-box {
width: 100px;
height: 100px;
background-color: purple;
border-radius: 0;
transition: border-radius 0.5s ease;
}
.border-box:hover {
border-radius: 50%;
}

Example 3: Transitioning Box Shadow

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.shadow-box {
width: 100px;
height: 100px;
background-color: orange;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
transition: box-shadow 0.5s ease;
}
.shadow-box:hover {
box-shadow: 0 0 20px rgba(0, 0, 0, 1);
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="shadow-box"></div>
</body>
</html>
CSS
.shadow-box {
width: 100px;
height: 100px;
background-color: orange;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
transition: box-shadow 0.5s ease;
}
.shadow-box:hover {
box-shadow: 0 0 20px rgba(0, 0, 0, 1);
}

Example 4: Transitioning Margin

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.margin-box {
width: 100px;
height: 100px;
background-color: brown;
margin: 0;
transition: margin 0.5s ease;
}
.margin-box:hover {
margin: 20px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="margin-box"></div>
</body>
</html>
CSS
.margin-box {
width: 100px;
height: 100px;
background-color: brown;
margin: 0;
transition: margin 0.5s ease;
}
.margin-box:hover {
margin: 20px;
}

Example 5: Transitioning Opacity

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.fade-box {
width: 100px;
height: 100px;
background-color: blue;
transition: opacity 0.5s ease;
}
.fade-box:hover {
opacity: 0.5;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="fade-box"></div>
</body>
</html>
CSS
.fade-box {
width: 100px;
height: 100px;
background-color: blue;
transition: opacity 0.5s ease;
}
.fade-box:hover {
opacity: 0.5;
}

Browser Support

The transition property is well-supported across modern web browsers. Here’s a summary:

  • Google Chrome: Supported since version 26.0 (March 2013)
  • Mozilla Firefox: Supported since version 16.0 (October 2012)
  • Microsoft Edge: Supported since version 12.0 (July 2015)
  • Internet Explorer: Supported since version 10.0 (September 2012)
  • Safari: Supported since version 6.1 (June 2013)
  • Opera: Supported since version 12.1 (November 2012)

Related Properties

The transition property is a shorthand for several individual properties:

  1. transition-property: Specifies which CSS properties to animate.
  2. transition-duration: Defines how long the transition takes.
  3. transition-timing-function: Sets the speed curve of the transition.
  4. transition-delay: Sets the delay before the transition starts.

Example Using Related Properties

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 2s ease-in-out 1s, width 1s linear 0s;
}
.box:hover {
background-color: green;
width: 200px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="box"></div>
</body>
</html>
CSS
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 2s ease-in-out 1s, width 1s linear 0s;
}
.box:hover {
background-color: green;
width: 200px;
}

Conclusion

The transition property in CSS is a powerful tool for creating smooth and engaging animations. With wide browser support, you can use it confidently to enhance user experience. Whether you’re targeting Chrome, Firefox, Edge, Internet Explorer, Safari, or Opera, the transition property is a reliable choice for modern web design.

Introduction to CSS Transitions

CSS transitions allow elements to change smoothly over a specified duration. This is great for enhancing user experience by making interactions more dynamic. Let’s dive into the basics of CSS transitions.

Transition Properties

transition

The transition property is a shorthand for defining multiple transition properties in one declaration. It is used to specify the duration, timing function, and delay of transitions.

transition-property

Specifies the CSS property to which the transition is applied.

transition-duration

Defines how long the transition will take.

transition-timing-function

Describes how the transition will progress over time (e.g., linear, ease, ease-in, ease-out, ease-in-out).

transition-delay

Sets a delay before the transition starts.

Example

Let’s see an example to understand how transitions work.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Transition Example</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 2s ease-in-out 1s, width 1s linear 0s;
}
.box:hover {
background-color: green;
width: 200px;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<div class="box"></div>
</body>
</html>

CSS

.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 2s ease-in-out 1s, width 1s linear 0s;
}
.box:hover {
background-color: green;
width: 200px;
}

Conclusion

CSS transitions are a powerful way to enhance web designs by making elements change smoothly. Whether you use individual properties or the shorthand, transitions can create engaging and visually appealing experiences for users.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.