CSS Transitions for Smooth State Changes
Applies CSS transitions to create smooth property changes on hover, focus, and state toggles with custom timing and easing functions.
Code
/* Basic hover transition */
.button {
background: #3498db;
color: white;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 4px;
transition: background 0.3s ease, transform 0.2s ease;
}
.button:hover {
background: #2980b9;
transform: translateY(-2px);
}
.button:active {
transform: translateY(0);
}
/* Color transition */
.link {
color: #333;
transition: color 0.2s ease;
}
.link:hover {
color: #007bff;
}
/* Expand on hover */
.card {
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1),
box-shadow 0.3s ease;
}
.card:hover {
transform: scale(1.05);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
/* Smooth dropdown */
.dropdown {
max-height: 0;
overflow: hidden;
transition: max-height 0.4s ease-in-out, opacity 0.3s ease;
opacity: 0;
}
.dropdown.open {
max-height: 500px;
opacity: 1;
}
/* Multi-property transition */
.panel {
transition: all 0.3s ease;
}
.panel.collapsed {
height: 0;
padding: 0;
opacity: 0;
margin: 0;
}
/* Transition with delay */
.toast {
transition: transform 0.3s ease 0.1s,
opacity 0.3s ease;
}
/* Focus ring transition */
input {
transition: box-shadow 0.2s ease;
}
input:focus {
box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.3);
}transition smoothly animates property changes. Specify properties, duration, timing function, and delay. cubic-bezier creates custom easing curves. Transitions trigger on state changes (:hover, :focus, class toggles). Use transform and opacity for GPU-accelerated animations. Avoid transitioning height/width for performance; use max-height or transform: scaleY() instead.
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.
Report a problem with this exampleOpens a pre-filled issue on GitHub.