Start animating your web pages with CSS! Learn the basics of CSS animations and transitions, including examples and practical tips for beginners.
Table of content
Adding animations to your website can turn an ordinary experience into something interactive and delightful. Thankfully, CSS animations make it straightforward to create movement and transitions, all without JavaScript. This beginner’s guide will walk you through the essentials of CSS animations, offering code examples and best practices for web development enthusiasts on fulldev.pl.
CSS animations let you smoothly change CSS property values over time. You can animate elements like buttons, backgrounds, and text to enhance user engagement and highlight important features.
@keyframes
.Transitions provide an easy way to animate property changes, often triggered by pseudo-classes like :hover
.
.btn {
background: #007bfa;
color: #fff;
padding: 1rem 2rem;
border: none;
border-radius: 4px;
transition: background 0.3s;
}
.btn:hover {
background: #005bb5;
}
With transition: background 0.3s;
, the background color fades smoothly on hover.
@keyframes
For more complex effects, use @keyframes
to define the animation steps.
@keyframes slideIn {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.box {
animation: slideIn 0.8s ease-out;
}
Here, the .box
element will slide in from the left and fade in.
@keyframes
(e.g., slideIn
).1s
, 500ms
).ease
, linear
).infinite
for looping).normal
, reverse
, or alternate
.@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
.animated-btn {
animation: bounce 1s infinite;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.text {
animation: fadeIn 2s;
}
transform
and opacity
for best performance.animation-play-state
to pause/resume on hover or focus.prefers-reduced-motion
media query:@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}
CSS animations add polish and engagement to your web projects without the overhead of JavaScript. Start experimenting with transitions and keyframes to bring your interface to life—and always keep usability and accessibility in mind for a seamless user experience!