Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
When the Delhi Metro or Mumbai suburban train pulls out of a station, does it instantly snap from 0 km/h to 80 km/h in 1 millisecond? Of course not! If it did, every standing passenger would fly backward and tumble onto the floor.
The train starts slowly (ease-in), accelerates smoothly across the track, and gently glides to a stop as it approaches the next platform (ease-out).
+-------------------------------------------------------------------------+ | THE METRO ACCELERATION ANALOGY | | | | Without CSS Transitions (Abrupt, Harsh, Broken): | | Normal State [ Blue Button ] ===(INSTANT 0.0s SNAP!)===> [ Red Button ]| | (Looks like a flickering bulb or glitching screen) | | | | With CSS Transitions (Silky, Natural, 60 FPS): | | Normal State [ Blue ] ---(Smooth 0.3s morphing)---> Hover State [ Red ]| | (Feels premium, tactile, and responsive to human touch) | +-------------------------------------------------------------------------+
In basic CSS, changing a property on :hover or :focus causes an abrupt instantaneous snap. In this tutorial, you will master CSS Transitions to breathe life, motion, and elegance into your user interfaces.
To create a transition, CSS needs to know four specific parameters:
In production web development, we combine all 4 properties into a single concise line:
transition on the base class (.button), NOT on the :hover pseudo-class! Declaring it on the base ensures the element transitions smoothly both when the mouse enters AND when the mouse leaves. If declared only on :hover, the reverse animation snaps back abruptly!The transition-timing-function governs how speed varies throughout the duration of the animation:
| Timing Function | Real-World Motion | Best Use Case |
|---|---|---|
ease (default) | Starts moderately, accelerates, slows down | General UI element morphs |
linear | Unchanging mechanical conveyor belt speed | Color cycling, rotating loading spinners |
ease-in | Heavy truck accelerating from a standstill | Elements exiting or sliding off the screen |
ease-out | Passenger car braking to a gentle stop | Elements entering the screen or drawer menus |
ease-in-out | High-speed express train between stations | Hover lifts, modal dialog reveals |
cubic-bezier(x1, y1, x2, y2) | Custom mathematical physics curve | Bouncy elastic buttons, spring physics |
Can you transition everything in CSS? No! CSS can only transition properties that have intermediate numerical or chromatic values.
opacity (0.0 to 1.0), transform, color, background-color, border-radius, box-shadow, width, height.display: none to display: block (an element is either there or not; there is no 50% block), font-family, visibility (partially discreet).display: none! Animate opacity: 0 and transform: scale(0.95), paired with pointer-events: none to disable clicks when hidden!Browsers render webpages using two different engines: the CPU (Main Layout Engine) and the GPU (Graphics Processor).
+------------------------------------+------------------------------------+ | BAD: CPU Layout Thrashing | GOOD: GPU Hardware Accelerated | | (Triggers expensive recalculations)| (Composited directly on the GPU) | +------------------------------------+------------------------------------+ | `top: 20px` | `transform: translateY(20px)` | | `left: 50px` | `transform: translateX(50px)` | | `width: 300px` | `transform: scaleX(1.2)` | | `height: 200px` | `transform: scaleY(1.2)` | +------------------------------------+------------------------------------+
Whenever you want to move, scale, or fade an element on hover, always use transform and opacity. They run silky-smooth at 60 frames per second on mobile phones without battery drain or stuttering!
| Bad Practice (Don't) | Good Practice (Do) | Why? |
|---|---|---|
Writing transition: all 0.3s; on every element | Specify exact properties: transition: transform 0.2s, opacity 0.2s; | transition: all degrades CPU performance by monitoring 100+ properties unnecessarily. |
Declaring transition inside the :hover block | Declare transition on the base un-hovered selector | Prevents the animation from snapping back instantly when the cursor leaves. |
| Using long 2-second durations for simple buttons | Keep interactive UI transitions between 0.15s and 0.3s | Long transition durations make user interfaces feel laggy, unresponsive, and sluggish. |
Animating top, left, or margin for hover lifts | Use transform: translateY(-4px); | transform uses GPU compositing, avoiding slow DOM reflow calculations. |
transition: property duration timing-function delay;.:hover pseudo-class.150ms (0.15s) and 300ms (0.3s). Anything above 500ms feels painfully slow.ease-out for Entrances: Decelerates naturally as elements land on screen.transform (translate, scale, rotate) and opacity for butter-smooth 60fps animations.transition property be declared to ensure a smooth animation both on hover entry and exit?transition: display 0.5s; fail to smoothly fade an element in and out?transition: all 0.3s; considered an anti-pattern in high-performance web development?Build a responsive, polished School Action Button and Interactive Card Suite:
transform: translateY(-2px)) and intensifies its shadow on hover with GPU acceleration.0.25s.Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.