Forum Moderators: not2easy

Message Too Old, No Replies

:not() pseudoclass

         

csdude55

5:34 pm on Feb 4, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm kind of debating on which way to do this, and I'm hoping for some input.

Would you guys and gals use a :not() pseudoclass, write a class for a default and then overwrite it, or go back through the entire site to add a class name?

I understand that :not() isn't going to work with < IE8, but I'm getting to a point where I can ignore browsers that old.

[caniuse.com...]

But I also remember reading once that pseudoclasses were a LOT slower and should be avoided. But I use :hover and :focus a lot, and :after to clear floats, so that ship might have already sailed.

So my options are:

// option 1, :not() pseudoclass
label:not([for]) {
display: inline-block;
padding: 3px 0;
width: 50%
}

// option 2, write it once FOR and overwrite it for NOT
label {
display: inline-block;
padding: 3px 0;
width: 50%
}

label[for] {
display: inline !important;
padding: 0 !important;
width: auto !important
}

// option 3 requires going back through the site and finding each one to change
label.foo {
display: inline-block;
padding: 3px 0;
width: 50%
}


Which would you use? Or would you do something else entirely?

lucy24

6:43 pm on Feb 4, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To keep Forums from eating essential anchor:
https://caniuse.com/#feat=css-sel3

Fotiman

10:16 pm on Feb 4, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Option 1. It's the cleanest, clearly expresses the intent, and performance will not be an issue. In fact, option 2 will require a redraw which might even make that option the slowest.

lucy24

5:16 am on Feb 5, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As a rough-and-dirty rule: If your CSS requires the use of !important, there is a problem with the CSS. Write it so the styles do what they've been told to do, without forced overrides. Sometimes it's just a matter of shifting things around and putting Rule B before Rule A.

justa

7:45 am on Feb 5, 2020 (gmt 0)

10+ Year Member



I agree with Fotiman, option one is my preference.

Looking at this another way, all your labels should have a for attribute to link it against the corresponding form input to make it more accessible.

I’ve seen CSS similar to what you’ve got that allows you to flag inaccessible elements to go back and fix them (search for a11y.css)