107

How to prevent the click event using CSS ?

I have created the form page , then i need to prevent the click event using only CSS?
I have tried this css property, but not worked.

<div>Content</div>
div {
  display: none;
}
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
M K
  • 1,821
  • 4
  • 11
  • 22
  • 1
    Please provide your code and describe all the steps that you have done. – knstvk Jun 26 '17 at 09:16
  • You can't prevent click with css, you must use javascript. The dislpay:none only hide a element, in this case, a div. – Roy Bogado Aug 21 '17 at 11:31
  • Do not change questions that much (revision 4)! – Qwertiy Aug 21 '17 at 11:56
  • @Roy on a technicality you're right. But you can just use hacky solutions like not making it clickable by disabling user-select and disabling pointer-events, but it's not gonna work in all cases and it's a bit hacky. – GeorgeWL May 29 '18 at 13:56
  • 1
    `display: none` just hides the element which makes it indeed not clickable, but I presume that's not what you want. I recommend give a class to the element(s) you want to hide and create a CSS rule for it. For example: `div.not-clickable { pointer-events: none; }` - https://developer.mozilla.org/en/docs/Web/CSS/pointer-events – S. Esteves Mar 25 '19 at 06:24

3 Answers3

249

You can try the css class:

.noClick {
   pointer-events: none;
}
kevinSpaceyIsKeyserSöze
  • 3,693
  • 2
  • 16
  • 25
0

I did the following to still allow hover events:

element.addEventListener("focusin", function(event) {
  this.blur()
})

It removes focus from the element once focused. It makes sure that the element won't ever get focus.

It's very suitable for the input tag.

However I don't recommend it if you want a good tab-key-experience.

france1
  • 141
  • 2
  • 12
-2

Try this:

document.body.addEventListener("selectstart", function(event) {
  event.preventDefault();
});

I'd seen it recently used in a test-practice website which was trying to prevent me from copy/pasting the site for notes taking. It was pretty effective, took me about an hour to finally track down what was causing it.