135

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;
}
5
  • 2
    Please provide your code and describe all the steps that you have done. Commented Jun 26, 2017 at 9:16
  • You can't prevent click with css, you must use javascript. The dislpay:none only hide a element, in this case, a div. Commented Aug 21, 2017 at 11:31
  • 1
    Do not change questions that much (revision 4)! Commented Aug 21, 2017 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. Commented May 29, 2018 at 13:56
  • 2
    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; } - developer.mozilla.org/en/docs/Web/CSS/pointer-events Commented Mar 25, 2019 at 6:24

3 Answers 3

325

You can try the css class:

.noClick {
   pointer-events: none;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, but how to still allow hover actions on that element? For example; .noClick:hover { cursor: not-allowed; }
@iPzard check this out.
Thanks! I ended up using a JavaScript solution, but this CSS one would be better if it works. I'll have to give it a try tomorrow.
1

I did the following to still allow hover events:

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

It 'removes' focus from the element once focused. This way we're making sure it can't gain focus at all. It's very suitable for the input tag, for example. Note you must not use the event.preventDefault function in combination with the focus event.

Comments

-3

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.

1 Comment

The question asks for a CSS solution, not JavaScript

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.