Micro - interactions are the small, often unnoticed animations that occur in response to a user’s action. For example, when you click a like button on a social media post, the button might change color and scale slightly. These interactions provide feedback to the user, making the interface feel more responsive and engaging.
CSS (Cascading Style Sheets) and HTML (Hypertext Markup Language) are the building blocks of web pages. CSS can be used to animate HTML elements by changing their properties over time. HTML provides the structure of the page, while CSS adds the visual and interactive elements.
Keyframes allow you to define the start and end states of an animation, as well as any intermediate states. Here is an example of a simple fade - in animation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade - in - element {
animation: fadeIn 2s ease - in - out;
}
</style>
</head>
<body>
<div class="fade - in - element">This element will fade in.</div>
</body>
</html>
In this example, the @keyframes
rule defines the fadeIn
animation. The from
state has an opacity of 0 (fully transparent), and the to
state has an opacity of 1 (fully opaque). The .fade - in - element
class applies the fadeIn
animation with a duration of 2 seconds and an ease - in - out timing function.
Transitions are used to animate the change of an element’s property over a specified duration. Here is an example of a button that changes color on hover:
<!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: blue;
color: white;
padding: 10px 20px;
border: none;
transition: background - color 0.3s ease;
}
button:hover {
background - color: green;
}
</style>
</head>
<body>
<button>Hover me</button>
</body>
</html>
In this example, the transition
property on the button
element specifies that the background - color
property should change over 0.3 seconds with an ease timing function when the button is hovered over.
Button hover effects are one of the most common micro - interactions. They can be used to indicate that a button is clickable and to provide visual feedback to the user. Here is an example of a button that scales on hover:
<!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: purple;
color: white;
padding: 10px 20px;
border: none;
transition: transform 0.3s ease;
}
button:hover {
transform: scale(1.1);
}
</style>
</head>
<body>
<button>Click me</button>
</body>
</html>
Menu slide - in animations can be used to create a more intuitive and engaging navigation experience. Here is an example of a sidebar menu that slides in from the left:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
body {
margin: 0;
}
.sidebar {
width: 200px;
height: 100vh;
background - color: gray;
position: fixed;
left: -200px;
transition: left 0.3s ease;
}
.sidebar.open {
left: 0;
}
button {
margin: 10px;
}
</style>
<script>
function toggleSidebar() {
const sidebar = document.querySelector('.sidebar');
sidebar.classList.toggle('open');
}
</script>
</head>
<body>
<button onclick="toggleSidebar()">Open Menu</button>
<div class="sidebar"></div>
</body>
</html>
Loading spinners are used to indicate that a page or a process is loading. Here is an example of a simple loading spinner:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.spinner {
width: 50px;
height: 50px;
border: 5px solid rgba(0, 0, 0, 0.1);
border - top - color: blue;
border - radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="spinner"></div>
</body>
</html>
transform
and opacity
is more performant because they can be handled by the GPU. Avoid animating properties like width
, height
, and margin
as they can trigger layout reflows.CSS and HTML animation micro - interactions are a powerful way to enhance the user experience of web pages. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can create engaging and responsive interfaces. Remember to optimize for performance and accessibility to ensure that your micro - interactions are beneficial to all users.