by Sai gowtham

Disable the text selection highlighting in React

In this tutorial, we are going to learn about how to disable the text selection highlighting from a webpage in React.

Consider, that we have the following text in our React app:

import  React from "react";

function App {
    return (
      <div>
          <p>
            Lorem Ipsum is simply dummy text of the printing
            and typesetting industry. Lorem Ipsum has been the
            industry's standard dummy text ever since the 1500s.
          </p>
      </div>
    );
}

export default App;

Normally, if we double click on some text in the browser it will be selected/highlighted by default to prevent that behavior follow the below tutorial.

To disable the text selection highlighting in React, we can set the css property user-select to a value “none”.

Here is an example:

import  React from "react";

function App {
    return (
      <div>
          <p className="disable-text-selection">
            Lorem Ipsum is simply dummy text of the printing
            and typesetting industry. Lorem Ipsum has been the
            industry's standard dummy text ever since the 1500s.
          </p>
      </div>
    );
}

export default App;
.disable-text-selection{
   -moz-user-select:none; /* firefox */
   -webkit-user-select: none; /* Safari */
   -ms-user-select: none; /* IE*/
   user-select: none; /* Standard syntax */
}

In the example above, we have added the browser prefixes for the user-select property, so it works the same in all browsers.

The user-select css property controls the behavior of a text selection for the HTML elements.

By setting a user-select property with the value none, we can disable the text selection on a webpage.

In case, if you want to disable the text selection for an entire webpage instead of a particular element, you can add the below CSS properties to your body tag.

body {
   -moz-user-select:none; /* firefox */
   -webkit-user-select: none; /* Safari */
   -ms-user-select: none; /* IE */
   user-select: none; /* Standard syntax */
}

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2022 (Beginner + Advanced)
JavaScript - The Complete Guide 2022 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY