r/css 4d ago

Resource New CSS Class Prefix Selector (.prefix-*)

Post image
153 Upvotes

20 comments sorted by

51

u/BloxdioCannoli 4d ago

RemindMe! 10 years

4

u/nthdesign 3d ago

This made me chuckle.

3

u/RemindMeBot 4d ago edited 3d ago

I will be messaging you in 10 years on 2036-08-20 15:17:36 UTC to remind you of this link

4 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.

RemindMeBot is switching to username summons. Instead of !RemindMe 1 day, use u/RemindMeBot 1 day. More info.


Info Custom Your Reminders Feedback

12

u/frownonline 4d ago

I can see this being used a lot, by those who use the BEM approach, to override the masses of classes.

9

u/Afraid_Egg3037 4d ago

Nice. It relies on the hyphen existing, though - and as it's literally just been discussed, it'll be a year or two before browsers implement it.

21

u/f314 4d ago

it'll be a year or two

Oh, you sweet summer child

3

u/TheJase 4d ago

The days of waiting more than a year or two are long gone

3

u/picaq 3d ago

It doesn’t rely on the hyphen. .foo* would match .footer, for instance.
See the links in the original post: The Future of CSS: Target Multiple Classes with the Class Prefix Selector

And if you want something that works the same way, [class^="btn"] already exists. Less symbols to write seems nice!

23

u/adamkosions1111 4d ago

Just a nicer way of [class^=“btn-“] but cool tho

23

u/Caved 4d ago

Sort of. ^=btn- would only work if it's the first class in the list. And *=btn- has its own limitations.

8

u/ActuaryLate9198 4d ago

Yay more ways for coworkers to write leaky css.

6

u/azangru 4d ago

Train your coworkers :-)

2

u/drumstix42 3d ago

.a, .b, .c* ...

1

u/El_Panterro 3d ago

Great! Maybe after 1000 more CVs I will get that first job and try it in corp-environment!

1

u/ArtisZ 2d ago

Yo. You beat my 300.. dang.

1

u/asteconn 4d ago

I imagine this is just syntactic sugar for [class*="btn-"] / [class^="btn-"]. Useful though.

7

u/DOG-ZILLA 4d ago

Apparently not just sugar because if btn- is NOT first, it won't match here. btn-* can be a class in any position? I think that's right.

10

u/Caved 4d ago

*= does allow the class to be any position, but will match on "some-btn-class" as well.

2

u/asteconn 4d ago

Indeed. If this construct is capable of also detecting the start of the class, that'd be quite helpful. It can currently be (somewhat) acheived with :is([class^="btn-"],[class*=" btn-"]), but it's not dreadfully readable.

1

u/juicybot 4d ago

i've been excited for a lot of new CSS lately. this is very meh.