CSS transforms and transitions let you move, scale, rotate, and smoothly animate elements on a web page without writing a single line of JavaScript. You apply a transform to change an element’s visual geometry (like rotating it 45 degrees), and a transition to animate that change over time (like a button that gradually grows when hovered). This guide explains the syntax, the key properties, and practical examples you can use today.
What Are CSS Transforms?
Transforms are CSS properties that modify the coordinate space of an element, allowing you to translate (move), rotate, scale, skew, or apply a 3D perspective. They do not affect the document flow—the element’s original space remains reserved, which can cause overlaps if you move it far.
The core property is transform, which accepts one or more functions. For example:
.box {
transform: rotate(45deg) scale(1.2) translateX(20px);
}
Each function is applied from right to left (the rightmost is applied first to the element’s local coordinate system). You can also use transform-origin to change the pivot point (default is the center: 50% 50%).
What Are CSS Transitions?
Transitions allow you to animate changes in CSS properties over a specified duration. Instead of an instant change, the element smoothly interpolates between the start and end values. The core properties are:
transition-property– which properties to animate (e.g.,transform,opacity,background-color). Useallfor everything, but be specific for performance.transition-duration– how long the animation lasts (e.g.,0.3s).transition-timing-function– the speed curve (e.g.,ease,linear,cubic-bezier(...)).transition-delay– an optional delay before starting.
Shorthand: transition: property duration timing-function delay; – e.g., transition: transform 0.3s ease-in-out;
How to Use Them Together (Step-by-Step)
The most common pattern is to define a base state, then a changed state (like :hover or a class toggle), and apply a transition to the base so the change animates.
- Start with the element’s default style. Add the transition property to the base rule, not the hover rule, so the animation works both on hover and on unhover.
- Define the transformed state. In the
:hoveror.activerule, set the newtransformvalue. - Test and adjust. Change the duration and timing function until it feels natural.
Example – a button that scales up smoothly:
.btn {
background: #3498db;
transition: transform 0.2s ease, background-color 0.2s ease;
}
.btn:hover {
transform: scale(1.1);
background-color: #2980b9;
}
Common Transform Functions and When to Use Them
translate(x, y)– move an element. Useful for tooltips, modals, or subtle hover shifts. UsetranslateX()ortranslateY()for one-axis movement.rotate(angle)– spin an element. Great for icons, loading spinners, or decorative elements.scale(factor)– enlarge or shrink. Perfect for buttons, images, or cards on hover.skew(x-angle, y-angle)– tilt an element’s axes. Often used for ribbon effects or parallax.perspective()– apply 3D depth to a child. Use withrotateX()orrotateY()for card flips.
For 3D transforms, you also need transform-style: preserve-3d on the parent and a perspective value on the parent or in the transform itself.
Choosing the Right Timing Function
The timing function controls the acceleration of the animation. The default ease starts slow, speeds up, then slows down. linear is constant speed, good for infinite spinners. ease-in starts slow and ends fast (like a falling object), while ease-out starts fast and ends slow (like a button release).
For more control, use cubic-bezier(x1, y1, x2, y2). For example, a bounce effect uses a curve that goes above 1 on the y-axis. You can experiment in browser dev tools to find the perfect feel.
Common Mistakes and How to Avoid Them
- Transition on the wrong rule: If you put
transitiononly on:hover, the change will animate on hover but snap back instantly on unhover. Always put it on the base rule. - Animating non-animatable properties: Not all CSS properties can be transitioned (e.g.,
display,visibility). Useopacityandvisibilitywith a delay for fade-out effects. - Forgetting
transform-origin: A rotate or scale uses the center by default. If you want a card to flip from its left edge, settransform-origin: left center. - Overusing
all: Transitioning every property can cause unexpected animations and performance issues. List the specific properties you need. - Not considering performance: Animating
transformandopacityis GPU-accelerated and smooth. Animatingwidth,height, ortopcauses layout reflows and can be janky. Prefer transforms.
Transforms vs. Animations (Keyframes)
Transitions are for simple two-state changes (hover to normal). CSS animations with @keyframes are for multi-step sequences or infinite loops. For example, a bouncing ball needs keyframes, while a button hover effect works fine with a transition.
Use transitions when you want a smooth response to a state change (hover, focus, active, class toggle). Use keyframe animations when you need more than two states, a delay loop, or a continuous motion. They can also be combined: a transition on a parent and an animation on a child.
Accessibility and Reduced Motion
Large or rapid movements can cause discomfort for people with vestibular disorders. Always provide a prefers-reduced-motion media query to disable or minimize animations:
@media (prefers-reduced-motion: reduce) {
* {
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
}
}
This snippet is a common accessibility best practice. It doesn’t remove the transform, but it makes the transition instant, which is usually acceptable.
Frequently Asked Questions
Can I transition from display: none to display: block?
No, display is not animatable. A common workaround is to use opacity: 0 and visibility: hidden with a transition, then set display: none after the transition ends using JavaScript, or use the @starting-style rule for modern browsers.
Why is my transform not working?
Check that the element is not inline. Transforms do not apply to inline elements (like a <span> without display: inline-block). Also verify you haven’t accidentally overridden the transform property in another rule, and that you’re using the correct syntax (e.g., rotate(45deg) not rotate(45)).
What is the difference between translate and position: relative + top/left?
Using transform: translate() is better for animation because it doesn’t trigger layout reflow; it only moves the element on the compositor. top/left changes the position in the document flow and forces the browser to recalculate layout, which is slower.
Can I use transitions on 3D transforms?
Yes, 3D transforms like rotateY() and translateZ() are animatable. Just ensure the parent has perspective and transform-style: preserve-3d to see the depth effect. Without perspective, 3D transforms look flat.
How do I make a smooth infinite rotation?
Use a CSS animation with @keyframes and animation: spin 2s linear infinite. The keyframe should go from transform: rotate(0deg) to rotate(360deg). A transition alone cannot loop.
Does transition work on all browsers?
Yes, CSS transitions and transforms are supported in all modern browsers, including Internet Explorer 10 and later (with vendor prefixes for older versions). For maximum compatibility, you can add -webkit- prefixes, but they are rarely needed today.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Add & Remove Favorites in Windows 10 Photos App
- Fix Links Not Opening in Chrome: 7 Top Ways
- Connect Alexa to a Soundbar: Choose the Right Setup
- Fix Host Process for Setting Synchronization in Windows 10
- Windows 11 search how to control automatic indexing of frequently used folders?
- How to Set Up Tips on Twitter (X) for Android & iOS