We have a few dozen CSS pseudo-classes. Most browsers' developer tools allow us to examine/toggle only a handful of them like :hover
, :focus
, :active
.
How do you examine if a specific element has other pseudo-classes? How can you toggle them?
One example is that Bootstrap form validation applies :invalid
and :valid
pseudo-classes to an input
element depending on whether it has passed the validation. Suppose we need to debug custom validation rules and feedback by examining and toggling those pseudo-classes. How would you do it in developer tools?
I'm not limiting the question to Chrome DevTools; it's perfectly fine to answer this using any browser developer tools. Likewise, I'm not limiting this question to the specific use case of Bootstrap validation (it's just the first that comes to mind), since that use case covers just two of many other CSS pseudo-classes.
Pseudo classes on elements can be triggered to investigate how an element may react if it were to be hovered over for example. You can right click on a node in the Elements panel and select Force element state. Alternatively, the Toggle element state icon can be clicked on in the Styles sub-pane.
You can use the plus sign, then while the class or element is highlighted, add the pseudo element by editing the class or element name. Hitting the right arrow key will put you at the end.
Press Ctrl + Shift + i for Windows/Linux (or command + option + i for Mac). Right-click on an element on your website page and select Inspect. Now that you are familiar with accessing Google Chrome Developer Tools, you will be able to inspect CSS elements to modify them live.
In Chrome you can check with the Developer Tools the states of some of the CSS pseudo classes, to do that let's take an example from your Bootstrap link:
Pseudo class | Property name | value | Force State exists |
---|---|---|---|
:active | N/A | N/A | Yes |
:checked | checked | true | No |
:default | defaultChecked | true | No |
:disabled | disabled | true | No |
:empty | childNodes | empty list OR only comment(s) | No |
:enabled | disabled | false | No |
:focus | N/A | N/A | Yes |
:focus-visible | N/A | N/A | Yes |
:focus-within | N/A | N/A | Yes |
:hover | N/A | N/A | Yes |
:indeterminate | indeterminate | true | No |
:in-range | validity.rangeOverflow AND validity.rangeUnderflow | false | No |
:invalid | validity.valid | false | No |
:optional | required | false | No |
:out-of-range | validity.rangeOverflow OR validity.rangeUnderflow | true | No |
:read-only | readOnly | true | No |
:read-write | readOnly | false | No |
:required | required | true | No |
:valid | validity.valid | true | No |
:visited | N/A | N/A | Yes |
Although as we have seen above we can check the value of some pseudo-classes, a portion of them are read-only like :valid
and :invalid
since the browser compute the validity of the input then put the result inside a ValidityState
(property object named validity
):
BUT for a majority of the properties listed above you can edit the value, to do that easily follow these steps:
:required
one:
PS: the global variable(s) created will be automatically deleted when you reload or close the page/tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With