Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally prevent text selection in Firefox

Tags:

html

css

I'm currently using:

*{
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

Go to http://jsfiddle.net/KyF5x/ and click below the list, see that this highlights the text... which can't be un-highlighted. Reload the page, now try ctrl+a, see that this will also highlight the text.

The above doesn't occur in Chrome, Safari or IE 10.

Disclaimer: I'm using Firefox 18

like image 360
Jack Avatar asked Jan 13 '13 20:01

Jack


People also ask

How do I turn off text selection highlighting?

You can use the user-select property to disable text selection of an element. In web browsers, if you double-click on some text it will be selected/highlighted. This property can be used to prevent this.

How to disable text selection in JavaScript?

How to Disable Text Selection with JavaScript. Apply the onmousedown and onselectstart Events to the <body> or <div> tags to prevent text selection and copy/cut on your website. It override the default behavior of the browsers.

How do I select text in Firefox?

Press ALT. Select text with LMB. Release LMB. Release ALT.


2 Answers

As a temporary answer, the fix is the apply the CSS to the individual 'unselectable' elements. However i'd love to see someone come up with a document-wide fix.

li{
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

See: http://jsfiddle.net/KyF5x/1/

A use case for having document-wide unselectable text is more obvious in the domain of web apps, rather than typical websites.

like image 60
Jack Avatar answered Sep 23 '22 01:09

Jack


As a complement to Jack's answer - even in 2018, firefox does not support user-select, but does support moz-user-select. I chose a cut down version of what the fiddle in the accepted answer does:

/* stop the user selecting page structure with the mouse */
html, body, div, a, i, button, select, option, optgroup, hr, br {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: default;
}

This is for a web app where we don't use any other elements, all page structures are divs, even titles etc, and we didn't want to disallow selection in input or textarea.

This was the only place in our entire web 500k lines of code app where we've had to use -moz- !!

like image 23
user2728841 Avatar answered Sep 24 '22 01:09

user2728841