How to Disable Text Selection in Tailwind CSS?
Last Updated :
23 Jul, 2025
Tailwind CSS can also be used for disabling text selection, providing a straightforward way to enhance user experience in web applications. By using Tailwind's utility classes, developers can easily control the selectability of text elements, preventing unwanted highlighting during interactions.
This feature is particularly useful in scenarios where text selection may interfere with functionality, such as buttons or interactive components.
These are the following approaches to disable text selection:
Using select-none Utility
In this approach, we are using the select-none utility from Tailwind CSS to disable text selection within a specific container. By applying this class to the parent <div>, any text inside it becomes unselectable, enhancing the user experience by preventing unwanted text highlighting.
Example: The below example uses select-none Utility to disable text selection in Tailwind CSS.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Example 1</title>
<script src="https://cdn.tailwindcss.com/3.4.16"></script>
</head>
<body class="bg-gray-100 p-10">
<h1 class="text-green-500 text-4xl mb-4">GeeksforGeeks</h1>
<h3 class="text-xl text-gray-700 mb-6">
Approach 1: Using `select-none` Utility</h3>
<div class="bg-white p-6 rounded-md shadow-lg select-none">
<h2 class="text-lg font-semibold">Text Selection Disabled</h2>
<p>This text cannot be selected because the `select-none`
class has been applied to this container.</p>
</div>
</body>
</html>
Output:
OutputUsing Group and Child Utilities
In this approach, we are using Tailwind CSS's group utility to control the text selection behavior when hovering over a parent container. The group-hover:select-none class is applied to the paragraph, which disables text selection only when the user hovers over the parent <div>.
Example: The below example uses select-none Utility to disable text selection in Tailwind CSS.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Example 2</title>
<script src="https://cdn.tailwindcss.com/3.4.16"></script>
</head>
<body class="bg-gray-100 p-10">
<h1 class="text-green-500 text-4xl mb-4">GeeksforGeeks</h1>
<h3 class="text-xl text-gray-700 mb-6">
Approach 2: Using Group and Child Utilities</h3>
<div class="group bg-white p-4 shadow-md rounded-md">
<p class="group-hover:select-none">Hovering over this text will
disable selection.</p>
</div>
</body>
</html>
Output:
Output
Similar Reads
How to Disable Text Selection in HTML with CSS? Disabling text selection in HTML can be useful in scenarios where you want to prevent users from copying text, dragging elements, or accidentally selecting text while interacting with web page elements.These are the following approaches:Table of ContentUsing user-select PropertyUsing Vendor Prefixes
2 min read
How to target next sibling in Tailwind CSS ? In Tailwind CSS, you may run into a situation where you only want to target and format direct siblings of HTML elements. This article walks you through the process of accessing the direct sibling of a tag in Tailwind CSS by providing syntax, methods, and examples. A solution that allows developers t
3 min read
How to Set Text Color in RGBA in Tailwind CSS ? In this article, we will change the color of text to rgba(0, 0, 0, 0.54) which is an RGB color format with an added parameter alpha that tells the opacity of the color. In this article, we are going to learn how we can achieve the text color of rgba(0, 0, 0, 0.54) in Tailwind CSS and hence, change t
3 min read
How to Disable Text Selection Highlighting using CSS? The user-select property in CSS is used to disable text selection highlighting in CSS. This feature is useful when we need to disable the copy option of content.Syntaxuser-select: none;-webkit-user-select: none; /* Chrome, Opera */-webkit-touch-callout: none; /* Safari */-moz-user-select: none; /* F
2 min read
Tailwind CSS Text Decoration This class accepts more than one value in tailwind CSS. All the properties are covered in class form. It is the alternative to the CSS text-decoration property. This class is used to âdecorateâ the content of the text. It is essentially decorating the text with different kinds of lines. Text Decorat
2 min read
How to Customize the Font Size in Tailwind CSS ? Customizing font size in Tailwind CSS involves using the text-[size] utility class, where [size] can be predefined size identifiers like xs, sm, md, lg, xl, or numerical values. Additionally, you can extend the default font sizes in the configuration file for more customization.Syntax<p class="te
2 min read