CSS Hover Effects
Hover effects provide immediate visual feedback and enhance user interaction. This page demonstrates various hover animations using pure CSS.Text Hover Effects
Underline Animation
Create an animated underline that grows from the left when hovering over text.- Parent element uses
display: inline-blockandposition: relative ::afterpseudo-element creates the underline with full width- Initially hidden with
transform: scaleX(0) transform-origin: bottom rightmakes it grow from the right- On hover,
scaleX(1)expands to full width transform-origin: bottom lefton hover reverses the growth direction
- Remove
transform-originto grow from center - Reverse values to grow from right to left
- Adjust
heightandbackground-colorto match your design
Button Transitions
Multiple button hover effects to add life to your interface.Grow Animation
Button scales up on hover.Shrink Animation
Button scales down on hover.Fill Animation
Background and text colors swap on hover.Swing Animation
Button swings side to side using keyframes.- Keyframes define a sequence of horizontal movements
- Amplitude decreases over time (5px → 3px → 2px → 0)
- Creates a natural swinging motion that settles
Border Animation
Animated borders appear on hover using pseudo-elements.::beforecreates top border, positioned at top-right::aftercreates bottom border, positioned at bottom-left- Both start at 24px width
- On hover, width expands to 100%
- Borders animate from opposite corners toward each other
Card Hover Effects
Sophisticated card animations for engaging layouts.Rotating Card
Two-sided card that flips on hover.perspectiveon container creates 3D space- Both sides positioned absolutely in same space
backface-visibility: hiddenhides the back of rotated elements- Back side starts rotated 180° (hidden)
- On hover, front rotates to 180° and back rotates to 0°
- Creates a flipping effect revealing the back side
Shifting Card
Card tilts based on mouse position using CSS variables and JavaScript.- Calculate card center position
- Track mouse position on
mousemove - Calculate normalized distance from center (-1 to 1)
- Update CSS variables
--dxand--dy - Transform applies rotation based on mouse position
- Card tilts toward cursor
Perspective Card
Card with 3D perspective that adjusts on hover.- Initial state has perspective of 1500px with 15° rotation
- Creates angled, 3D-looking card
- On hover, perspective increases to 3000px (less extreme)
- Rotation decreases to 5° (more face-on)
- Card appears to “straighten” toward viewer
Shadow Effects
Hover Shadow Box
Elevated shadow on hover.Typewriter Effect
Animated typing effect using CSS and JavaScript.- Text element starts with
max-width: 0(hidden) overflow: hiddenclips invisible textwhite-space: nowrapprevents wrapping- JavaScript sets text and character count variable
typinganimation expands width usingsteps()for discrete character reveals::afteradds blinking cursor withblinkanimation- Animation loops infinitely
Performance Tips
- Use transform and opacity - GPU-accelerated properties
- Avoid layout thrashing - Don’t animate
width,height,margin, etc. - Keep transitions short - 200-500ms for most hover effects
- Use
will-changesparingly - Only for complex animations - Test on mobile - Hover effects don’t work on touch devices
- Provide alternatives - Consider focus states for keyboard navigation
- Respect user preferences - Honor
prefers-reduced-motion
Accessibility Considerations
- Not all users can use a mouse (keyboard, screen readers, touch)
- Ensure hover effects have
:focusequivalents - Don’t rely solely on hover to reveal critical information
- Test with keyboard navigation