Tillitsdone
down Scroll to discover

Mastering CSS user-select Control Text Selection

Learn how to control text selection on your webpage with CSS user-select.

Discover use cases and available options like auto, none, text, and all.
thumbnail

Introduction

The user-select property in CSS lets you control whether users can select text on your webpage. It’s useful for preventing text from being copied or ensuring specific selection behavior. By using values like auto, none, text, and all, you can enhance user experience and protect your content. This guide explains the user-select property, its values, and provides examples to help you use it effectively.

Specification

The user-select property is defined in the CSS Basic User Interface Module Level 4. It ensures consistent text selection behavior across different browsers. Understanding this specification helps you create reliable user experiences.

Description

The user-select property in CSS controls whether users can select text on your webpage. It’s useful for preventing text from being copied or ensuring specific selection behavior. This property helps enhance user experience and protect your content.

Limited Availability

Note that the user-select property has limited support in some browsers. This means not all users will experience the intended text selection behavior, which can affect the overall user experience. Always test your implementations across different browsers to ensure consistency.

Syntax

The user-select property uses a simple syntax:

user-select: auto | none | text | all;

Default Value

The default value is auto.

Property Values

  • auto: Allows the user to select text as determined by the browser.
  • none: Prevents the user from selecting text.
  • text: Enables the user to select text.
  • all: Allows the user to select the entire text content of an element with a single click.

Example

Here’s how to use the user-select property:

.unselectable {
user-select: none;
}
.all-selectable {
user-select: all;
}

In this example, the class unselectable prevents text selection, while the class all-selectable allows the entire text content of an element to be selected with a single click.

Values

The user-select property can take several values to control text selection:

none

Prevents the text of the element and its sub-elements from being selectable.

auto

Determines the selection behavior based on the following rules:

  • On the ::before and ::after pseudo-elements, the used value is none.
  • If the user-select property of the parent element is none, the used value is none.
  • If the user-select property of the parent element is all, the used value is all.
  • Otherwise, the used value is text.

text

Allows the text to be selected by the user.

all

Ensures that the entire content of the element is selected atomically. If a selection includes part of the element, the selection will encompass the entire element, including all its descendants.

Global Values

  • inherit: Inherits the value from the parent element.
  • initial: Sets the value to its default (auto).
  • revert: Resets the value to the browser’s default.
  • revert-layer: Resets the value to the default of the containing layer.
  • unset: Resets the value to the default (auto) if it is not inherited, or inherits the value from the parent element.

Examples

Let’s look at some practical examples of the user-select property.

Preventing Text Selection

Use the none value to prevent the user from selecting text within a specific element.

.unselectable {
-webkit-user-select: none;
user-select: none;
}

Allowing Text Selection with a Single Click

Use the all value to allow the entire text content of an element to be selected with a single click.

HTML

<p>You should be able to select this text.</p>
<p class="all-selectable">Clicking once will select all of this text.</p>

CSS

.all-selectable {
-webkit-user-select: all;
user-select: all;
}

Enabling Default Text Selection

Use the auto value to enable the default text selection behavior, which allows the user to select text as determined by the browser.

HTML

<p>You should be able to select this text.</p>
<p class="auto-selectable">This text has the default selection behavior.</p>

CSS

.auto-selectable {
-webkit-user-select: auto;
user-select: auto;
}

Ensuring Text is Selectable

Use the text value to explicitly enable text selection, ensuring that the text can be selected by the user.

HTML

<p>You should be able to select this text.</p>
<p class="text-selectable">This text can be selected.</p>

CSS

.text-selectable {
-webkit-user-select: text;
user-select: text;
}

Result

By applying these CSS rules, you can control the text selection behavior on your webpage. The user-select property is a powerful tool for enhancing user experience and protecting your content.

Formal Definition

The user-select property in CSS is formally defined with the following characteristics:

Initial Value

  • Initial Value: auto

Applies To

  • Applies To: All elements

Inherited

  • Inherited: No

Computed Value

  • Computed Value: As specified

Animation Type

  • Animation Type: Discrete

Formal Syntax

The formal syntax for the user-select property is:

user-select = auto | text | none | contain | all;

Note

It’s important to note that while the contain value is defined in the CSS Basic User Interface module, it is not supported in any browsers. This value is intended to enable selection to start within the element and be contained by the bounds of that element.

Browser Compatibility

The user-select property has varying levels of support across different web browsers. Here is an overview of the browser compatibility for this property:

BrowserVersionDate of Support
Chrome54.0Oct 2016
Firefox2.0Oct 2006
IE/Edge10.0Sep 2012
Opera41.0Oct 2016
Safari3.1Mar 2008

While the user-select property is supported in most modern browsers, it’s important to test your implementation across different browsers to ensure consistent behavior. Some browsers may have partial support or may not fully adhere to the CSS specification for this property.

See Also

For further reading and related topics, you may also be interested in the following:

  • [::selection]WebsiteUrl pseudo-element
  • The JavaScript [Selection]WebsiteUrl object

These resources provide additional information and context for understanding and utilizing the user-select property in your web development projects.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.