Question
For anchor elements that behave like buttons or tabs—such as sidebar links like Questions, Tags, and Users—is there a CSS-based way to prevent users from accidentally highlighting the text when dragging or clicking?
I understand this can be done with JavaScript, and I found the Mozilla-specific -moz-user-select property. Is there a standards-based CSS solution for this? If not, what is the best-practice approach?
Example context:
<a href="#" class="tab-button">Questions</a>
<a href="#" class="tab-button">Tags</a>
<a href="#" class="tab-button">Users</a>
Short Answer
By the end of this page, you will understand how text selection works in the browser, how to disable it for UI-like elements using CSS, when to use the user-select property, and what trade-offs to consider for usability and accessibility.
Concept
Browsers allow users to select text by default. This is useful for reading, copying, and interacting with content. However, some elements are styled to behave more like controls than readable text—for example:
- tab labels
- navigation pills
- toolbar actions
- links styled as buttons
In those cases, accidental text selection can feel awkward. A common CSS solution is the user-select property.
.button-like {
user-select: none;
}
This tells the browser that the text inside the element should not be selectable by the user.
Why this matters
In real interfaces, users often click and drag slightly. If a button label gets highlighted during that interaction, the UI can feel less polished. Disabling selection on control-like elements can improve the interaction experience.
Is it standard?
Yes. user-select is the standard CSS property used for this purpose. Historically, browsers required vendor-prefixed versions such as:
-webkit-user-select-moz-user-select-ms-user-select
In practice, many codebases still include prefixes for broader compatibility.
Important caveat
Do not disable selection on normal content just for aesthetics. Users often need to:
Mental Model
Think of selectable text like text printed on paper that a user can drag a highlighter over.
For regular paragraphs, that is helpful because the user may want to mark or copy something.
For a button label, the text is more like the word printed on a physical elevator button. You press it, but you do not expect to highlight the letters on it.
user-select: none tells the browser: treat this text more like a control label than a document passage.
Syntax and Examples
The core CSS syntax is:
.element {
user-select: none;
}
For broader browser support, many developers write:
.element {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Example: links styled as tabs
<nav>
<a href="#questions" class="tab-button">Questions</a>
<a href="#tags" class="tab-button">Tags</a>
<a href="#users" class="tab-button">Users</a>
</nav>
.tab-button {
: inline-block;
: ;
: solid ;
: none;
: ;
-webkit-: none;
-moz-: none;
-ms-: none;
: none;
}
Step by Step Execution
Consider this example:
<a href="#" class="action-link">Save</a>
.action-link {
-webkit-user-select: none;
user-select: none;
}
What happens step by step:
- The browser renders the anchor text
Save. - By default, anchor text can usually be selected like other text.
- The CSS rule applies to
.action-link. - The browser sees
user-select: none. - When the user clicks and drags across the text, the browser avoids creating a text selection for that element.
- The anchor still remains clickable and focusable unless other CSS or JavaScript changes that behavior.
Small trace example
<p>Selectable paragraph</p>
<a href="#" class="button-link">Click Me</a>
Real World Use Cases
Disabling text selection is useful in UI elements that act like controls rather than content.
Common examples
- Tabs:
Overview,Settings,Billing - Button-like links:
Save,Edit,Delete - Sidebar navigation items: labels in menus
- Toolbar actions: icon-and-text controls
- Drag handles: labels that should not highlight during dragging
- Game or app-like interfaces: where fast clicking and dragging is common
Example scenarios
- In a dashboard, users rapidly click navigation items. Preventing accidental text selection makes the UI feel smoother.
- In a custom file manager, toolbar labels should behave like controls, not text content.
- In a tabbed component, dragging across tab labels should switch focus or do nothing—not highlight the label text.
Real Codebase Usage
In real projects, developers usually apply user-select: none narrowly, not globally.
Common pattern: utility class
Many codebases create a reusable class:
.no-select {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Then apply it only where needed:
<a href="#" class="tab no-select">Profile</a>
<button class="no-select">Save</button>
Common pattern: component-level styling
In component-based apps, the rule is often placed directly in the component styles:
.sidebar-item,
.tab,
.toolbar-button {
user-select: none;
}
Best-practice usage
- Use it for controls, tabs, and draggable UI parts.
- Avoid using it on article text, form labels users may copy, or code snippets.
Common Mistakes
1. Disabling selection everywhere
Broken approach:
* {
user-select: none;
}
Why it is a problem:
- users cannot copy text
- it harms usability
- it can frustrate accessibility workflows
Better:
.tab,
.button-like {
user-select: none;
}
2. Using an anchor when a button is more appropriate
Broken approach:
<a href="#" onclick="saveData()">Save</a>
If the element performs an action instead of navigation, prefer:
<button type="button">Save</button>
This improves semantics and accessibility.
3. Relying only on old vendor-prefixed properties
Older code:
Comparisons
| Approach | What it does | Best for | Downsides |
|---|---|---|---|
user-select: none | Prevents text selection | UI controls, tabs, button-like labels | Should not be used on normal content |
| JavaScript selection prevention | Manually blocks selection events | Edge cases or custom interactions | More code, less clean than CSS for simple cases |
| Default browser behavior | Allows text selection | Articles, labels, code, general content | Can cause accidental highlights on controls |
user-select values
| Value | Meaning |
|---|---|
none | Text cannot be selected |
Cheat Sheet
/* Standard */
.element {
user-select: none;
}
/* Wider compatibility */
.element {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Use user-select: none for
- tabs
- button-like links
- toolbar buttons
- drag handles
- navigation items
Avoid it for
- paragraphs
- articles
- code snippets
- API keys
- messages users may need to copy
Useful values
none— prevent selectiontext— allow selectionauto— default browser behaviorall— select all content at once
Best practice
- apply it only to UI controls
- prefer semantic
<button>for actions - include vendor prefixes if supporting older browsers
FAQ
Is there a standard CSS way to disable text selection?
Yes. The standard property is user-select.
Should I use JavaScript to prevent text highlighting?
Usually no. CSS is simpler and cleaner for this case. JavaScript is only needed for special interaction logic.
Does user-select: none stop clicks or keyboard focus?
No. It only affects text selection. The element can still be clicked and focused normally.
Can I use user-select: none on links styled as buttons?
Yes. That is a common use case, especially for tabs or navigation controls.
Should I disable text selection on all buttons?
Often yes for custom UI controls, but use judgment. Do not disable selection where users may need to copy text.
Is -moz-user-select enough?
No. It is an older vendor-prefixed version. Prefer the standard user-select property, optionally with prefixes for compatibility.
Should I use <a> or <button> for a button-like control?
Use <a> for navigation and <button> for actions. Styling does not change the element’s semantic purpose.
Mini Project
Description
Build a small tab navigation component where each tab looks and behaves like a UI control. The goal is to prevent accidental text selection while keeping the tabs clickable and visually clear.
Goal
Create a tab-style navigation bar whose labels cannot be accidentally highlighted during user interaction.
Requirements
- Create at least three tab links using HTML.
- Style the links so they look like tabs.
- Prevent text selection on the tab labels using CSS.
- Keep normal text outside the tabs selectable.
- Add one active tab style for visual feedback.
Keep learning
Related questions
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.