Skip to content

Tailwind CSS - Touch Action

The touch-action utilities control how an element’s region can be manipulated by a touchscreen user (e.g., for panning or zooming). They are crucial for creating smooth, predictable mobile and touch-based experiences, especially for components like image carousels, maps, or drawing canvases.

These classes map to the touch-action CSS property, which allows you to fine-tune the browser’s default touch behaviors.

ClassCSS PropertiesEffect on Touch Interaction
touch-autotouch-action: auto;Default browser handling for panning and zooming.
touch-nonetouch-action: none;Disables all panning and zooming. Useful when you have custom JS-based touch handling.
touch-pan-xtouch-action: pan-x;Enables single-finger horizontal panning. Disables vertical panning and zooming.
touch-pan-ytouch-action: pan-y;Enables single-finger vertical panning. Disables horizontal panning and zooming.
touch-pan-lefttouch-action: pan-left;Enables panning only to the left.
touch-pan-righttouch-action: pan-right;Enables panning only to the right.
touch-pan-uptouch-action: pan-up;Enables panning only upwards.
touch-pan-downtouch-action: pan-down;Enables panning only downwards.
touch-pinch-zoomtouch-action: pinch-zoom;Enables multi-finger pinch-to-zoom. Panning may also be supported.
touch-manipulationtouch-action: manipulation;A useful combination of panning and pinch-zooming, but disables double-tap to zoom. Ideal for most interactive widgets.

In a typical image carousel, you want the user to be able to scroll the page up and down (pan-y), but when they swipe horizontally on the carousel itself, you want to move the images (pan-x), not scroll the page. The touch-action property makes this possible.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Touch Action for Carousels</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-8 font-sans">
<h1 class="text-3xl font-bold mb-4">My Photo Gallery</h1>
<p class="mb-6 text-gray-700">You can scroll this page up and down normally. On a touch device, try swiping the image gallery horizontally. The page won't scroll, but the gallery will.</p>
<!-- The container allows horizontal scrolling and sets the touch action -->
<div class="flex space-x-4 overflow-x-auto pb-4 touch-pan-x">
<!-- Image items -->
<div class="flex-shrink-0 w-80">
<img src="https://images.unsplash.com/photo-1682687220247-9f786e34d472?w=500" class="w-full h-48 object-cover rounded-lg shadow-lg" alt="Desert landscape">
<p class="mt-2 font-semibold">Desert View</p>
</div>
<div class="flex-shrink-0 w-80">
<img src="https://images.unsplash.com/photo-1707343843982-f8275f3994c5?w=500" class="w-full h-48 object-cover rounded-lg shadow-lg" alt="Underwater scene">
<p class="mt-2 font-semibold">Underwater Life</p>
</div>
<div class="flex-shrink-0 w-80">
<img src="https://images.unsplash.com/photo-1709289299190-833b3557a552?w=500" class="w-full h-48 object-cover rounded-lg shadow-lg" alt="Architectural detail">
<p class="mt-2 font-semibold">Modern Architecture</p>
</div>
<div class="flex-shrink-0 w-80">
<img src="https://images.unsplash.com/photo-1709418353393-92f69845d048?w=500" class="w-full h-48 object-cover rounded-lg shadow-lg" alt="Person on a boat">
<p class="mt-2 font-semibold">Boat Trip</p>
</div>
</div>
<div class="mt-8 h-96 bg-gray-200 rounded-lg flex items-center justify-center">
<p class="text-gray-500">More content below to allow for page scrolling.</p>
</div>
</body>
</html>

Disabling Touch Actions for Custom Controls

Section titled “Disabling Touch Actions for Custom Controls”

The touch-none utility is particularly useful for elements like a drawing canvas or a custom map interface where you are handling all touch events with JavaScript. This prevents the browser from trying to scroll or zoom the page while the user is drawing.

<!-- On a touch device, swiping on this canvas will not scroll the page -->
<canvas id="drawing-canvas" class="touch-none w-full h-64 bg-white border"></canvas>
  • Use touch-manipulation for custom widgets: For things like sliders or custom buttons, touch-manipulation is often the best choice. It provides a responsive feel by enabling panning and zooming but disables the slight delay on click events associated with double-tap-to-zoom.
  • Prevent Scroll Chaining: Properly setting touch-action on an overflowing element prevents ‘scroll chaining’, where scrolling within the element also causes the main page to scroll once you hit the boundary. This creates a much cleaner, more native-app-like feel.
  • Test on Real Devices: Always test your touch interactions on actual touch-screen devices (phones, tablets) to ensure the behavior is intuitive and performs well.