🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Sometimes you might want to disable text selection on a webpage to prevent users from selecting text in certain elements. This can be useful for preventing accidental text selections or enhancing the user experience in cases where selecting text isn't necessary, such as in buttons, headings, or specific components of a web page. In this guide, you'll learn how to disable text selection in HTML using only CSS.
Step 1: Basic HTML Structure
We will start with a simple HTML structure that contains some text and buttons where text selection should be disabled.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Disable Text Selection</title>
</head>
<body>
<!-- Step 1: Sample text and buttons -->
<div class="container">
<h1>This is a heading. Try to select the text here.</h1>
<p>This is a paragraph. You can try to select this text, but it will be disabled for some elements below.</p>
<button class="no-select">Do not select this button text</button>
<p class="no-select">This paragraph text selection is disabled.</p>
</div>
</body>
</html>
Explanation:
- We have a heading (
h1), a paragraph (p), and a button. We’ll disable text selection for certain elements using CSS.
Step 2: Use CSS to Disable Text Selection
Now, we will use the user-select CSS property to disable text selection for specific elements.
<head>
<style>
/* General body styling */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
/* Step 2: Disable text selection */
.no-select {
-webkit-user-select: none; /* Chrome, Safari, Opera */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Standard */
}
/* Optional: Style elements */
button {
padding: 10px 20px;
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
font-size: 16px;
}
</style>
</head>
Explanation:
user-select: Theuser-selectproperty is used to control the selection of text in the specified elements.none: Disables text selection for the element.- Cross-browser compatibility: The property is prefixed with
-webkit-for WebKit-based browsers (Chrome, Safari),-moz-for Firefox, and-ms-for Internet Explorer/Edge to ensure it works across all major browsers.
.no-select: This class is applied to elements where you want to disable text selection, like buttons or specific paragraphs.
Complete Example
Here’s the complete HTML and CSS code that disables text selection for specific elements:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Disable Text Selection</title>
<style>
/* General body styling */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
/* Disable text selection */
.no-select {
-webkit-user-select: none; /* Chrome, Safari, Opera */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Standard */
}
/* Optional: Style elements */
button {
padding: 10px 20px;
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
font-size: 16px;
}
</style>
</head>
<body>
<!-- Sample text and buttons -->
<div class="container">
<h1>This is a heading. Try to select the text here.</h1>
<p>This is a paragraph. You can try to select this text, but it will be disabled for some elements below.</p>
<button class="no-select">Do not select this button text</button>
<p class="no-select">This paragraph text selection is disabled.</p>
</div>
</body>
</html>
Output
Conclusion
In this guide, you learned how to disable text selection in HTML using the user-select property in CSS. This method works across major browsers and is useful in preventing text selection for specific elements, such as buttons, headings, or other areas where text selection is unnecessary. You can easily apply this technique to any element by adding the user-select: none property.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment