Tillitsdone
down Scroll to discover

CSS touch-action Customizing Touch Interactions

Learn how to use the CSS touch-action property to customize touch interactions on web pages.

Discover available options like auto, none, pan-x, and pan-y.
thumbnail

Understanding the touch-action Property

The touch-action property in CSS controls how elements respond to touch gestures on touchscreen devices. It allows developers to customize touch behaviors, making web applications more interactive and responsive.

Why is it Important?

By using the touch-action property, you can ensure that your web application provides a consistent user experience across different browsers and devices. This property is crucial for managing touch gestures, whether through the browser’s default behavior or custom JavaScript code.

Standards and Specifications

  1. Compatibility Standard:

  2. Pointer Events:

Browser Compatibility

When using touch-action, consider browser compatibility to ensure a consistent experience across different platforms.

Supported Browsers

  • Google Chrome: Supported since version 36.
  • Microsoft Edge: Supported since version 12.
  • Firefox: Supported since version 52.
  • Opera: Supported since version 23.
  • Safari: Supported since version 13.

Compatibility Notes

  • Internet Explorer: Not supported. Use polyfills or alternative methods.
  • Mobile Browsers: Generally well-supported. Test on various devices and browsers.
  • Future-Proofing: Stay updated with the latest browser releases and specifications. Test regularly across different browsers and versions.

Example of Browser Compatibility Check

To ensure your web application handles touch interactions correctly, you can use a simple compatibility check.

HTML

<div id="compatibility-check">Check Browser Compatibility</div>

CSS

#compatibility-check {
width: 300px;
height: 300px;
background-color: lightgray;
touch-action: none; /* Disable default browser handling */
}

JavaScript

const compatibilityCheck = document.getElementById('compatibility-check');
if ('touchAction' in document.documentElement.style) {
console.log('touch-action property is supported.');
} else {
console.log('touch-action property is not supported.');
}
compatibilityCheck.addEventListener('touchstart', (event) => {
console.log('Touch start:', event);
});
compatibilityCheck.addEventListener('touchmove', (event) => {
console.log('Touch move:', event);
});
compatibilityCheck.addEventListener('touchend', (event) => {
console.log('Touch end:', event);
});
compatibilityCheck.addEventListener('touchcancel', (event) => {
console.log('Touch cancel:', event);
});

Practical Examples

Here are some practical examples of how to use the touch-action property in different scenarios.

Disabling All Gestures

Disabling all touch gestures is common for elements that provide their own dragging and zooming behavior, such as a map or game surface.

HTML
<div id="map">Map Area</div>
CSS
#map {
height: 150vh;
width: 70vw;
background: linear-gradient(blue, green);
touch-action: none;
}
Result

By setting touch-action: none, you disable all browser-managed touch gestures on the element. This allows you to handle all touch interactions with custom JavaScript code.

Enabling Horizontal Panning

To enable only horizontal panning gestures on an element, use the pan-x value.

HTML
<div id="horizontal-scroll">Scroll Horizontally</div>
CSS
#horizontal-scroll {
width: 300px;
height: 300px;
background-color: lightblue;
overflow: auto;
touch-action: pan-x;
}
Result

By setting touch-action: pan-x, you enable horizontal panning gestures on the element while disabling vertical panning and pinch-zooming.

Enabling Vertical Panning

To enable only vertical panning gestures, use the pan-y value.

HTML
<div id="vertical-scroll">Scroll Vertically</div>
CSS
#vertical-scroll {
width: 300px;
height: 300px;
background-color: lightgreen;
overflow: auto;
touch-action: pan-y;
}
Result

By setting touch-action: pan-y, you enable vertical panning gestures on the element while disabling horizontal panning and pinch-zooming.

Enabling Pinch-Zoom

To enable pinch-zoom gestures on an element, use the pinch-zoom value.

HTML
<div id="pinch-zoom-area">Pinch to Zoom</div>
CSS
#pinch-zoom-area {
width: 300px;
height: 300px;
background-color: lightcoral;
touch-action: pinch-zoom;
}
Result

By setting touch-action: pinch-zoom, you enable multi-finger panning and zooming gestures on the element while disabling single-finger panning.

Combining Gestures

You can combine multiple gestures by using a combination of values. For example, you can enable both horizontal and vertical panning as well as pinch-zooming.

HTML
<div id="combined-gestures">Combined Gestures</div>
CSS
#combined-gestures {
width: 300px;
height: 300px;
background-color: lightyellow;
touch-action: pan-x pan-y pinch-zoom;
}
Result

By setting touch-action: pan-x pan-y pinch-zoom, you enable horizontal and vertical panning as well as pinch-zooming on the element.

Using manipulation

The manipulation value is an alias for pan-x pan-y pinch-zoom and is useful for reducing the delay in generating click events by disabling additional non-standard gestures such as double-tap to zoom.

HTML
<div id="manipulation-area">Manipulation Area</div>
CSS
#manipulation-area {
width: 300px;
height: 300px;
background-color: lightseagreen;
touch-action: manipulation;
}
Result

By setting touch-action: manipulation, you enable panning and pinch-zoom gestures while disabling additional non-standard gestures.

Summary

The touch-action property is a powerful tool for customizing touch interactions on web pages. While it is widely supported by modern browsers, it’s essential to consider browser compatibility to ensure a consistent user experience. By testing your implementation on various browsers and devices, you can identify and address any compatibility issues that may arise.

See Also

For further exploration and understanding of touch interactions and related CSS properties, consider the following resources:

  1. [pointer-events]WebsiteUrl

    • Learn about the pointer-events CSS property, which controls when an element can be the target of a pointer event.
  2. [Pointer Events]WebsiteUrl

    • Dive deeper into Pointer Events, a unified way to handle input from various devices like touchscreens, mice, and styluses.
  3. WebKit Blog: More Responsive Tapping on iOS

    • Discover insights and tips on improving the responsiveness of touch interactions on iOS devices.
  4. Google Developers Blog: Making touch scrolling fast by default

    • Explore how Google is working to make touch scrolling faster and more responsive by default.
  5. [Scroll Snap]WebsiteUrl

    • Learn about the CSS Scroll Snap module, which allows you to create smooth scrolling experiences with precise snap points.

By using these resources, you can gain a comprehensive understanding of touch interactions, pointer events, and related CSS properties, helping you create more responsive and engaging web experiences.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.