CSS selectors all exist within the same global scope. Anyone who has worked with CSS long enough has had to come to terms with its aggressively global nature — a model clearly designed in the age of documents, now struggling to offer a sane working environment for today’s modern web applications. Every selector has the potential to have unintended side effects by targeting unwanted elements or clashing with other selectors. More surprisingly, our selectors may even lose out in the global specificity war, ultimately having little or no effect on the page at all.
Any time we make a change to a CSS file, we need to carefully consider the global environment in which our styles will sit. No other front end technology requires so much discipline just to keep the code at a minimum level of maintainability. But it doesn’t have to be this way. It’s time to leave the era of global style sheets behind.
It’s time for local CSS.
In other languages, it’s accepted that modifying the global environment is something to be done rarely, if ever.
In the JavaScript community, thanks to tools like Browserify, Webpack and JSPM, it’s now expected that our code will consist of small modules, each encapsulating their explicit dependencies, exporting a minimal API.
Yet, somehow, CSS still seems to be getting a free pass.
Many of us — myself included, until recently — have been working with CSS so long that we don’t see the lack of local scope as a problem that we can solve without significant help from browser vendors. Even then, we’d still need to wait for the majority of our users to be using a browser with proper Shadow DOM support.
We’ve worked around the issues of global scope with a series of naming conventions like OOCSS, SMACSS, BEM and SUIT, each providing a way for us to avoid naming collisions and emulate sane scoping rules.
We no longer need to add lengthy prefixes to all of our selectors to simulate scoping. More components could define their own foo and bar identifiers which — unlike the traditional global selector model—wouldn’t produce any naming collisions.
import styles from './MyComponent.css'; import React, { Component } from 'react'; export default class MyComponent extends Component { render() { return ( <div> <div className={styles.foo}>Foo</div> <div className={styles.bar}>Bar</div> </div> ); }
The benefits of global CSS — style re-use between components via utility classes, etc. — are still achievable with this model. The key difference is that, just like when we work in other technologies, we need to explicitly import the classes that we depend on. Our code can’t make many, if any, assumptions about the global environment.
Writing maintainable CSS is now encouraged, not by careful adherence to a naming convention, but by style encapsulation during development.
Once you’ve tried working with local CSS, there’s really no going back. Experiencing true local scope in our style sheets — in a way that works across all browsers— is not something to be easily ignored.
Introducing local scope has had a significant ripple effect on how we approach our CSS. Naming conventions, patterns of re-use, and the potential extraction of styles into separate packages are all directly affected by this shift, and we’re only at the beginning of this new era of local CSS.
process.env.NODE_ENV === 'development' ? '[name]__[local]___[hash:base64:5]' : '[hash:base64:5]' )
Understanding the ramifications of this shift is something that we’re still working through. With your valuable input and experimentation, I’m hoping that this is a conversation we can have together as a larger community.
Note: Automatically optimising style re-use between components would be an amazing step forward, but it definitely requires help from people a lot smarter than me.
Throughout the awesome scheme of things you actually get an A with regard to effort and hard work. Where exactly you lost me ended up being in your specifics. You know, people say, details make or break the argument.. And that couldn’t be much more correct in this article. Having said that, let me reveal to you what exactly did give good results. The text is definitely quite convincing which is possibly the reason why I am taking an effort in order to comment. I do not make it a regular habit of doing that. Second, whilst I can see a jumps in reason you make, I am not confident of exactly how you appear to connect your ideas which inturn make your final result. For the moment I will, no doubt subscribe to your position however wish in the foreseeable future you actually connect the facts much better.
I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.
Keep functioning ,impressive job!
Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish only about gossips and net and this is really annoying. A good site with exciting content, that is what I need. Thank you for keeping this website, I will be visiting it. Do you do newsletters? Cant find it.
I like this website because so much useful material on here : D.
I really like your writing style, fantastic info , thanks for posting : D.
I got good info from your blog
Great post, you have pointed out some superb points, I as well believe this s a very good website.
Utterly written content, Really enjoyed studying.
Awsome blog! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also
magnificent points altogether, you simply gained a new reader. What would you suggest in regards to your post that you made a few days ago? Any positive?
Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.
Thanks for the good writeup. It actually used to be a entertainment account it. Glance complex to far delivered agreeable from you! By the way, how can we keep in touch?
Saved as a favorite, I really like your blog!
you have got an ideal weblog right here! would you like to make some invite posts on my weblog?
Wow, wonderful blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is fantastic, let alone the content!
I used to be very happy to search out this net-site.I wanted to thanks to your time for this wonderful read!! I undoubtedly enjoying every little bit of it and I have you bookmarked to take a look at new stuff you blog post.
You have observed very interesting points! ps decent internet site.
I’ve learn some just right stuff here. Definitely price bookmarking for revisiting. I wonder how a lot effort you place to create the sort of fantastic informative web site.
What i don’t understood is in fact how you’re now not really a lot more smartly-appreciated than you might be right now. You’re so intelligent. You realize thus considerably in relation to this subject, made me personally believe it from numerous numerous angles. Its like women and men don’t seem to be involved unless it is one thing to do with Girl gaga! Your own stuffs nice. At all times maintain it up!
Howdy this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!
Thanks , I have just been looking for info about this subject for ages and yours is the best I’ve discovered so far. But, what about the bottom line? Are you sure about the source?
It is appropriate time to make a few plans for the long run and it is time to be happy. I have learn this post and if I may I want to counsel you few fascinating issues or suggestions. Perhaps you could write next articles relating to this article. I wish to learn even more things approximately it!
When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!
Hey there! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
What’s Going down i am new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I’m hoping to contribute & help other customers like its helped me. Good job.
I simply couldn’t leave your site before suggesting that I really enjoyed the standard information a person provide in your guests? Is going to be again regularly to check up on new posts