Make text not highlightable in HTML

One CSS property turns selection off. It is right for buttons and labels, wrong for body copy, and worth nothing as a way to stop copying.

To make text not highlightable in HTML, set user-select: none on the element in CSS.

.no-select {
  -webkit-user-select: none; /* older Safari */
  user-select: none;
}

Selection stops for that element and everything nested inside it. Dragging across it produces no highlight, and a double click selects nothing.

A cursor dragged across a label with selection disabled, leaving no highlight behind.
A cursor dragged across a label with selection disabled, leaving no highlight behind.

The prefixed line is still worth keeping for older Safari and for iOS. Firefox and Chrome have supported the unprefixed property for years.

Where making text not highlightable belongs

The property earns its place on interface furniture, where an accidental highlight looks like a glitch.

Element Turn selection off Why
Buttons and toolbar labels Yes Double-clicking a button should not highlight its text
Drag handles Yes Dragging otherwise selects the label instead of moving the item
Tab labels and menu items Yes Repeated clicking selects text by accident
Icons and decorative glyphs Yes Nothing there is worth copying
Body paragraphs No Readers select text on purpose
Code blocks, addresses, IDs No Copying is the entire point

The line is simple. If someone might want to copy it, leave selection alone.

A page-wide user-select: none on body is the version that generates complaints, and it is also the one most often reached for.

Scoping it properly

Disable it where it is needed, not globally, and re-enable it where content lives.

.toolbar, .tabs, .btn {
  -webkit-user-select: none;
  user-select: none;
}

.toolbar .filename {
  user-select: text; /* the one part worth copying */
}

user-select: text puts selection back on a child of a blocked parent, which is the cleanest way to carve out an exception.

Two related values are worth knowing. user-select: all selects the whole element with a single click, which suits a licence key or a short identifier. user-select: contain keeps a selection started inside an element from spilling out of it.

A short key rendered with user-select: all, fully highlighted after one click.
A short key rendered with user-select: all, fully highlighted after one click.

It is not protection

This is the part that matters most, because the property is usually found while looking for a way to stop content being taken.

The page was delivered to the reader before any CSS ran. Every route below still works:

  1. View source, or the developer tools, which show the text as markup.
  2. Disabling CSS, or reader mode, which drops your rules entirely.
  3. Printing to PDF, which produces selectable text again.
  4. A screenshot, followed by any text recognition tool.
  5. Fetching the URL directly, with no browser involved at all.

Blocking the right-click menu or listening for copy events adds friction for ordinary readers and stops nobody who is deliberate. It also breaks legitimate uses: translation tools, speech readers, and dictionary lookups.

If the content genuinely must not be seen by certain people, the control belongs at the address, not in the stylesheet. Password protecting a page covers what is and is not achievable there.

Styling the highlight instead

Often the real complaint is that the default highlight looks wrong against a dark page, not that selection exists.

::selection {
  background: #2563eb;
  color: #ffffff;
}

Only a small set of properties apply to ::selection: colour, background colour, text decoration and shadow. Layout properties are ignored.

Keep the contrast strong. A highlight the same tone as the background makes selected text harder to read than unselected text, which is a worse outcome than the default.

The things people combine it with

Selection blocking rarely arrives alone. It usually appears alongside three other measures, and each has the same problem.

Blocking the context menu. A contextmenu handler that calls preventDefault removes right-click for everyone, including the people using it to open a link in a new tab or to run a spell check.

Cancelling copy events. A copy listener can clear the clipboard or substitute your own text. Readers who use the clipboard for accessibility tools lose that, and anyone with the developer tools open is unaffected.

Blocking keyboard shortcuts. Watching for Ctrl and S or Ctrl and U catches nothing, because the browser menus offer the same commands and the network tab offers the file directly.

All three are visible to the reader as the site fighting them, which is a stronger impression than whatever the content said. The measured position is to disable selection where it improves the interface and leave the rest alone.

Checking it in a clean window

Selection behaviour depends on the stylesheet actually arriving, so test the file away from your own folder.

The page in the HTML file opener, with a drag across the heading producing no selection.
The page in the HTML file opener, with a drag across the heading producing no selection.

Open it in the HTML file opener and try three things: drag across the blocked element, drag across a paragraph, and double-click a button label.

If selection works everywhere, the CSS is in an external file that did not travel. Put the rules in a <style> block or use inline styles so they stay with the page.

Sending the page to someone

The .html file itself is the least reliable way to deliver any of this. It gets filtered by mail gateways, opens in a code editor on some desktops, and on a phone it usually lands in storage and stops.

Paste the HTML into a NOS document instead. It renders as written, CSS included, at its own address. Share, then Share link, then Create link.

The address survives edits, so re-scoping a selection rule next week does not mean sending a second file. If what you actually wanted was a fixed image rather than live text, an image of the page is the more honest answer.

Questions people ask

How do I stop text from being highlighted in HTML?

Apply user-select: none in CSS to the element, with the -webkit- prefix for older Safari. Selection stops for that element and everything inside it. Nothing else is required and no JavaScript is involved.

Does disabling selection stop people copying my content?

No. The text is already on the reader's machine. View-source, reader mode, the developer tools, disabling CSS, a screenshot and a print-to-PDF all still work. Treat it as an interface choice, not a protection.

Is it bad for accessibility to turn selection off?

On body text, yes. People select text to keep their place, to look up a phrase, or to feed it to a translator or speech tool. Blocking that on paragraphs removes a working habit. On buttons and icons it costs nothing.

How do I change the highlight colour instead of removing it?

Use the ::selection pseudo-element and set background and color. Only a few properties apply there, and keep the contrast high, because a low-contrast highlight is harder to read than the default.

Keep reading