7

I am thinking of an alternative to JavaScript in case it's manually disabled by the user, so that instead of prompting them to enabled it, I could, for instance, in some way simulate the click event and change, for instance, the background colour by creating my own :click pseudo-class, just like it works for :hover.

Is such a scenario possible? Is it possible for one to create their own css pseudo-classes that?

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
  • Possible duplicate of [Can I have an onclick effect in CSS?](http://stackoverflow.com/questions/13630229/can-i-have-an-onclick-effect-in-css) – Joe Thomas May 29 '16 at 08:16

4 Answers4

6

If your question is, can I create a custom pseudo, example;

div:my-custom-pseudo { }

Then not without some JavaScript libraries.

Tutorial on using it with mooTools and slick

Personally, I haven't used this, so I don't know browser support and this is pretty much the only resource I ever found on custom pseudo classes.

Edit:

So pretty much, no, you can't do what you want, as you will always need JavaScript to do what you want.

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Mark
  • 2,061
  • 1
  • 16
  • 26
4

Creating pseudo-classes is not possible and if you can simulate that also on some browser, then it would likely not work on others.

For that simple reason, all :first-child and :last-child polyfills/fallbacks were done by using class names rather than trying to make pseudo-class work.

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
David Chelliah
  • 1,319
  • 1
  • 13
  • 24
1

You can use :active pseudo class. If I understand you properly.

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
Sergey Khalitov
  • 987
  • 7
  • 17
0

Custom State Pseudo Classes may indeed be available in the future (for Custom Elements).

Null
  • 975
  • 13
  • 13