Understanding Smooth Transitions with CSS: A Comparison of Box1 and Box2
CSS transitions add life and interactivity to websites by allowing smooth changes between styles when triggered by events like hover, focus, or clicks. In this blog, we’ll learn how to implement a smooth transition in CSS by comparing two boxes — Box1 and Box2.
Here’s the HTML and CSS code we’ll be using to demonstrate this:
Here's the HTML and CSS code we'll be using to demonstrate this:
CSS Transition
Smooth Transition
The CSS:
body {
height: 100vh;
background-color: blue;
text-align: center;
color: aliceblue;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 30px;
}
.box1, .box2 {
background-color: black;
border: 2px solid white;
width: 150px;
height: 150px;
}
.box1:hover {
border-radius: 50%;
}
.box2 {
transition: all 0.2s;
}
.box2:hover {
border-radius: 50%;
}
The Difference Between Box1 and Box2:
At first glance, both boxes look the same: they have the same size, shape, and color. However, upon hovering, Box1 and Box2 behave differently. Let’s break down what’s happening.
1. Box1: No Transition
.box1:hover {
border-radius: 50%;
}
When you hover over Box1, the border-radius
jumps from 0
(a square) to 50%
(a circle). However, since no transition
property is defined, this change happens instantly, resulting in an abrupt and jarring effect.
- Behavior: Sharp and immediate transition from square to circle.
2. Box2: Smooth Transition
.box2 {
transition: all 0.2s;
}
.box2:hover {
border-radius: 50%;
}
For Box2, we’ve added the CSS property transition: all 0.2s;
. This tells the browser to smoothly animate all properties (in this case, border-radius
) over 0.2 seconds
. As a result, when you hover over Box2, the shape gradually transforms from a square to a circle, creating a more visually appealing and smoother effect.
- Behavior: Smooth, gradual transformation from square to circle over 0.2 seconds.
Why Transitions Matter
Without transitions, sudden changes in the appearance of elements (like in Box1) can feel mechanical or unnatural. By using transitions, as seen in Box2, we introduce smoother interactions that are easier on the eyes and provide a better user experience. Transitions can be applied to a wide variety of CSS properties, such as background-color
, width
, height
, opacity
, and more.
Exploring the transition
Property:
The transition
property is a shorthand that can be broken down into several sub-properties:
- transition-property: Specifies which CSS property to animate (e.g.,
border-radius
,background-color
). - transition-duration: Defines how long the transition should take (e.g.,
0.2s
for 0.2 seconds). - transition-timing-function: Determines the speed curve of the transition (e.g.,
ease
,linear
,ease-in-out
). - transition-delay: Delays the start of the transition.
In our example, we used:
transition: all 0.2s;
- all: This applies the transition to all properties.
- 0.2s: This specifies the transition duration to be 0.2 seconds.
Conclusion
CSS transitions are a simple yet powerful way to add smooth, natural animations to your website. In this example, we’ve seen how a small difference — adding the transition
property — can drastically improve the user experience, turning a jarring change into a fluid transformation.
Try experimenting with different properties and durations to see how transitions can enhance the look and feel of your own projects!