Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespacing/Scoping in CSS

I want to apply the rules in a CSS file to a certain div/class so, for example, the contents of events.css is only applied to the contents of a class called .events and not outside this scope without the need to add the class .events to the start of each css rule.

I am thinking this is not possible though - but you never know.

If not I am thinking of achieving the same effect by prefixing the .events to the CSS rules after they have been uploaded/edited. I am thinking this should be fairly trouble-free, but does anyone forsee any problems?

like image 292
Meep3D Avatar asked May 16 '26 13:05

Meep3D


2 Answers

I am thinking this is not possible though

Indeed. Unless you put the content in question in an iframe. Then you get a whole separate document to style differently.

I am thinking of achieving the same effect by prefixing the .events to the CSS rules after they have been uploaded/edited. I am thinking this should be fairly trouble-free, but does anyone forsee any problems?

In an automated fashion? You would need to be able to parse the CSS input to determine where the beginnings of the selectors were.

This is not as trivial as you might think to do correctly:

/* This is my grate CSS
.sausage { color: red; } adds a class rule
/*/ .eggs, .bacon { color: red; }
.potato:before { content: "; }
.beans { content: "; }

is an example of tricky-to-parse CSS. .eggs, .bacon and .potato begin selectors and would want a .events prefix. .sausage and .beans are not.

like image 171
bobince Avatar answered May 18 '26 12:05

bobince


If not I am thinking of achieving the same effect by prefixing the .events to the CSS rules after they have been uploaded/edited. I am thinking this should be fairly trouble-free, but does anyone forsee any problems?

That's the only way to do it and should be fine.

like image 40
wefwfwefwe Avatar answered May 18 '26 12:05

wefwfwefwe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!