Disable the text-highlighting magnifier on touch-hold on Mobile Safari / Webkit

IphoneWebkit

Iphone Problem Overview


I have some elements in my iPhone website that don't have any text in them but require the user to click and hold on them (DIVs). This causes the text-highlighting/editing loop/cursor to show up, which is really distracting.

I know there is a CSS rule for removing the black-box that shows up on clickable elements when they are touched. Is there anything like that to disable the text magnifier?

Iphone Solutions


Solution 1 - Iphone

Just got a response from the Developer Center help desk. I needed to add this CSS rule:

-webkit-user-select: none;

Solution 2 - Iphone

Add this to the CSS

body {
-webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none;             /* prevent webkit from resizing text to fit */
-webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */}

Solution 3 - Iphone

Use these CSS rules:

-webkit-touch-callout: none;
-webkit-user-select: none; /* Disable selection/copy in UIWebView */

Solution 4 - Iphone

This is also useful in protecting content that you don't want copied or saved, such as an image:

#yourdiv img {-webkit-touch-callout: none; }

Solution 5 - Iphone

This solved it for me, in JS:

document.getElementsByTagName("body")[0].addEventListener("touchstart",
 function(e) { e.returnValue = false });

Seems to bypass whatever the OS has in there to catch the touch.

Solution 6 - Iphone

I found this out while trying it out myself. First of all you have to add this rule to the enclosing element:

-webkit-user-select: none;

But that, by itself, is not enough on the iPhone. It turns out that the magnifying glass can still appear because, for example, a parent element would accept selection, or just because it feels like it.

However, I then discovered something cool - if your element adds a touchend and click handler to an element, then Apple's Safari finally avoids the annoying code path that causes the magnifying glass to appear, probably realizing that this element is meant for some UI interaction, and not selecting text. On an equally awesome note, if you do this on elements near the top of the screen, it will also cancel the appearance of the navigation in landscape mode! Not sure however how to cancel the appearance of navigation when clicking on elements on the bottom, does anyone have a solution for that one?

Solution 7 - Iphone

On IOS 15.2 -webkit-user-select: none; fixed the issue, but only partially.

A long press doesn't show the magnifier anymore. However, if you double-tap and hold, it magically still appears.

There is still no 100% reliable way except event.preventDefault on touchstart. But this also blocks underlying actions so things like buttons with long-press tooltips break. Therefore, it is not always an option...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question3n.View Question on Stackoverflow
Solution 1 - Iphone3n.View Answer on Stackoverflow
Solution 2 - IphonePham Van VungView Answer on Stackoverflow
Solution 3 - IphonevitralyozView Answer on Stackoverflow
Solution 4 - IphoneJay from BKKView Answer on Stackoverflow
Solution 5 - IphoneandrwctView Answer on Stackoverflow
Solution 6 - IphoneGregory MagarshakView Answer on Stackoverflow
Solution 7 - IphonePaaDView Answer on Stackoverflow