All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Disable Text Selection In WebPage Using CSS

Last Updated : Jul 1, 2023

IN - CSS HTML | Written & Updated By - Pragati

In this tutorial we will create how to disable the selection of text using CSS. Whenever the user trying to select the text in your webpage he should not be able to do it.

You may also like disable cut copy and paste using JavaScript.

Disable Text Selection In WebPage Using CSS

To Disable Text Selection it takes only Two steps:-

  1. Make a HTML file and define markup for webpage
  2. Make a CSS file and define styling to disable the text

Step 1. Make a HTML file and define markup for webpage

We make a HTML file and save it with a name disable.html

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="disable_style.css">
</head>
<body>
  
<h1>Disable Text Selection In WebPage Using CSS</h1>
<p class="select">Select The Text In This Paragraph This Is Selectable Like Any Other Text</p>
<p class="no_select">Try To Select This Text You Are Not Able To Select This Text In Modern Browsers</p>

</body>
</html>

Step 2. Make a CSS file and define styling to disable the text

We make a CSS file and save it with name disable_style.css.

body
{
	background-color:#EB99FF;
	font-family:helvetica;
	text-align:center;
	margin:0px auto;
	padding:0px;
}
h1
{
	margin-top:100px;
	color:#424242;
	font-size:40px;
}
h1 p
{
	margin:0px;
	font-size:18px;
	color:black;
	text-decoration:underline;
}
.select
{
	font-size:30px;
	font-weight:bold;
	color:#4000FF;
}
.no_select
{
	font-size:30px;
	font-weight:bold;
	color:#4000FF;
	-webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

In this step we use the user-select property to disable the text selection and also we use cross-browser prefix so that this should work in most of the browser and also with some old browsers.

You may also like enable and disable keyboard keys using JavaScript.

Thats all, this is how to Disable Text Selection In WebPage Using CSS. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on disable text selection html css helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪