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.
I got what you intend, regards for putting up. Woh I am lucky to find this website through google.
I am sure this paragraph has touched all the internet viewers, its really really nice article on building up new webpage.
I will immediately seize your rss as I can not in finding your e-mail subscription hyperlink or e-newsletter service. Do you’ve any? Kindly permit me recognise in order that I may just subscribe. Thanks.
Ahaa, its fastidious conversation on the topic of this article here at this website, I have read all that, so now me also commenting here.
Ahaa, its pleasant conversation regarding this post here at this web site, I have read all that, so now me also commenting at this place.
It is providing the undetectable features and therefore, you will not be recognized to different gamers as you’re utilizing this hack software.
I’ll immediately clutch your rss feed as I can’t to find your email subscription hyperlink or e-newsletter service. Do you have any? Please allow me know in order that I could subscribe. Thanks.
I’ll right away snatch your rss feed as I can not to find your e-mail subscription hyperlink or e-newsletter service. Do you’ve any? Kindly let me recognize so that I may subscribe. Thanks.
Thank you for another great article. The place else may just anyone get that type of info in such a perfect approach of writing? I have a presentation next week, and I’m at the look for such information.
I am sure this post has touched all the internet viewers, its really really pleasant piece of writing on building up new web site.
I visited several sites but the audio quality for audio songs existing at this site is really wonderful.
I’ll right away grasp your rss feed as I can’t to find your email subscription link or newsletter service. Do you have any? Kindly allow me understand so that I may subscribe. Thanks.
I just could not go away your website prior to suggesting that I really enjoyed the standard information an individual supply on your visitors? Is going to be back continuously to inspect new posts
Ahaa, its good dialogue on the topic of this paragraph here at this webpage, I have read all that, so at this time me also commenting here.
Hello, after reading this awesome piece of writing i am also cheerful to share my know-how here with friends.
Howdy! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!
I am sure this paragraph has touched all the internet users, its really really fastidious post on building up new web site.
Hi, Neat post. There is an issue with your website in internet explorer, might check this? IE nonetheless is the marketplace chief and a good component to other folks will pass over your fantastic writing due to this problem.
Ahaa, its pleasant discussion concerning this article at this place at this web site, I have read all that, so now me also commenting here.
I like it whenever people get together and share thoughts. Great site, keep it up!
I’ll right away take hold of your rss as I can not find your e-mail subscription hyperlink or e-newsletter service. Do you’ve any? Kindly allow me know in order that I may subscribe. Thanks.
This post really speaks to me. Thank you!
Thiss iss a toopic that’s near too mmy heart… Maany thanks! Exacdtly whre aare yourr contawct dtails though?
Fine way of describing, and pleasant article to get facts about my presentation subject, which i am going to convey in school. wholesale NBA jerseys
Real informative and fantastic anatomical structure of subject matter, now that’s user genial (:.