👆 Mobile Touch Gestures

Making your site touch-friendly for phones and tablets

📚 Theory: Touch vs Mouse

The Core Problem

On a desktop, users have a mouse cursor that can hover over elements. On a phone, users have fingers that tap. Hover effects don't work as expected on touch devices.

What Happens on Touch Devices

The Solutions

1. Tap Target Size (44×44px minimum)

Apple and Google recommend minimum 44×44px for touch targets. Try tapping each button on your phone.

/* ❌ Too small — hard for fingers */ .btn-too-small { padding: 4px 8px; font-size: 0.75rem; } /* ✅ Good — 44px minimum */ .btn-good-size { padding: 12px 20px; font-size: 1rem; min-width: 44px; min-height: 44px; }

2. Hover vs Active States

On desktop, hover works. On touch, active provides tap feedback.

/* Hover styles ONLY apply on devices with hover capability (mouse) */ @media (hover: hover) { .btn:hover { background-color: #27ae60; transform: scale(1.05); } } /* Active state works on ALL devices (tap feedback) */ .btn:active { background-color: #1e8449; transform: scale(0.98); }
💡 Test on your phone: Tap the button above. You should see an instant color change (active state). On desktop, you'll see the hover effect.

3. Instant Tap Feedback with :active

:active triggers the MOMENT your finger touches the screen — perfect for touch feedback.

/* Active state — triggers instantly on touch */ .btn:active { background-color: #e67e22; transform: scale(0.97); transition: all 0.05s ease; /* Very fast transition */ }

4. Touch-Friendly Navigation Menu

Each menu item has 44px minimum height and tap feedback.

.menu-link { display: block; padding: 12px 16px; min-height: 44px; /* Minimum touch target */ } /* Hover only on mouse devices */ @media (hover: hover) { .menu-link:hover { background-color: #4a90e2; padding-left: 24px; } } /* Tap feedback on all devices */ .menu-link:active { background-color: #e94560; transform: scale(0.99); }

5. Detecting Device Capabilities

CSS can detect whether the device supports hover and what input method is used.

Checking your device...
/* Detect if device supports hover (mouse) */ @media (hover: hover) { /* Styles for mouse/trackpad devices */ } /* Detect if primary input is touch (finger) */ @media (pointer: coarse) { /* Styles for touch screens */ } /* Detect if primary input is mouse (precise) */ @media (pointer: fine) { /* Styles for mouse/stylus */ }

6. Before/After: Touch Optimization

❌ Before (Not Touch-Friendly)

/* Small tap target */ .small-button { padding: 4px 8px; font-size: 12px; } /* Hover only (no touch feedback) */ .button:hover { background: darkblue; }

✅ After (Touch-Friendly)

/* Larger tap target */ .large-button { padding: 12px 20px; font-size: 16px; min-height: 44px; } /* Hover only for mouse */ @media (hover: hover) { .button:hover { background: darkblue; } } /* Tap feedback for touch */ .button:active { background: navy; transform: scale(0.98); }

⚡ Quick Reference Card

/* =================================== 1. TAP TARGET SIZE (44px minimum) =================================== */ .button { padding: 12px 20px; min-width: 44px; min-height: 44px; } /* =================================== 2. DETECT HOVER CAPABILITY =================================== */ @media (hover: hover) { .element:hover { /* Hover styles — only on mouse devices */ } } /* =================================== 3. DETECT TOUCH SCREEN =================================== */ @media (pointer: coarse) { /* Styles for touch screens */ .element { padding: 16px; /* Larger tap targets */ } } /* =================================== 4. TAP FEEDBACK (works on all devices) =================================== */ .element:active { background-color: darker-color; transform: scale(0.98); transition: all 0.05s ease; }

📊 Best Practices Summary