---
title: "How do I disable text selection with CSS or JavaScript?"  
description: "How do I disable text selection with CSS or JavaScript?"  
author: "Revati S Misra"  
published: 2023-04-27  
updated: 2023-12-04  
canonical: https://www.mindstick.com/forum/158030/how-do-i-disable-text-selection-with-css-or-javascript  
category: "javascript"  
tags: ["javascript", "javascript events", "css", "html validation"]  
reading_time: 1 minute  

---

# How do I disable text selection with CSS or JavaScript?

How do I [disable](https://www.mindstick.com/forum/12901/how-to-disable-enable-input-box-in-aspx-with-value-from-sql) [text selection](https://www.mindstick.com/forum/484/all-text-selection-in-html-textbox-using-javascript) with [CSS](https://www.mindstick.com/forum/514/background-gradient-ie7-css-problem) or [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Gulshan Negi

In CSS, you can disable [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) [selection](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) with below code:

/* Disable text selection for the entire document */\
body {\
user-select: none;\
}

/* Disable text selection for a specific element */\
.disable-selection {\
user-select: none;\
}\

In JS you can do same by following code:

// Using event listeners to prevent default behavior for text selection events\
function disableTextSelection() {\
document.addEventListener('selectstart', function (e) {\
e.preventDefault();\
});

document.addEventListener('contextmenu', function (e) {\
e.preventDefault();\
});\
}

// Call the function to disable text selection\
disableTextSelection();\

I hope it will helps you.

Thanks


---

Original Source: https://www.mindstick.com/forum/158030/how-do-i-disable-text-selection-with-css-or-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
