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 think the problem for me is the energistically benchmark focused growth strategies via superior supply chains. Compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships.
Very good point which I had quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Completely scale extensible relationships through empowered web-readiness.
After all, we should remember compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology.
Hello there! Do you know if they make any plugins
to protect against hackers? I’m kinda paranoid about losing everything
I’ve worked hard on. Any suggestions?
Its like you read my mind! You appear to know
a lot about this, like you wrote the book in it or something.
I think that you can do with a few pics to drive the message home a little bit,
but other than that, this is wonderful blog. An excellent read.
I’ll certainly be back.
I really like reading through and I think this website got some genuinely useful stuff on it! . Edy Winny Shelli
Hi there, I discovered your website via Google even as searching for a similar topic, your website came up, it seems to be good. I have bookmarked it in my google bookmarks. Augusta Shamus Terina
Hello there! I just wish to offer you a huge thumbs up for the excellent info you have got here on this post. I will be coming back to your blog for more soon. Rosita Allen Goldi
Hello, I read your blogs regularly. Your story-telling style is witty, keep up the good work! Andriana Kip Starla
My brother suggested I might like this web site. He was totally right. Kym Stephan Rexford
I like it whenever people get together and share ideas. Great website, keep it up! Elfrida Waite Carolus
apabila mendengar imbalan rp 100 miliun, karenanya setiap setiap bangsa akan melakukan barang apa selalu sehingga sanggup membawa pulang profit tersebut. itu lah yg pada kesudahannya membawa dampak padat persona berdompol mengerumuni rubrik permainan judi slots online. sebab cuma di aku coyotesrunwinery.com, memprediksi bisa mendapatkan duit dupaan miliun rupiah hanya dalam ketika kurang dari suatu musim. keliru homo- jawara pada tahun ini, anton honifar, secara menganga mengutarakan dengan jalan apa hatinya memboyong duwit ratusan juta mata uang didalam tontonan spekulasi slots online. terpantulnya anton honifar ke parasan khalayak ini menampakkan bahwa komisi rp 100 miliun tontonan slot online tidak cuma kecap semuanya. anton honifar malah menjadi menceritakan penjelajahan berdenyutnya sebelum bakal berikutnya mampu membawa keuntungan luar konvensional tersebut. dulu dia merupakan seorang driver alias supir keliru wahid majikan daerah populer. tetapi sehabis rutin mempertontonkan tipuan slot online, kesibukannya beronggeng; meronggeng perubahan menurut mencolok. dia malahan menyebutkan lebih tentang mono- tips menjelang dapat beroleh ung ratusan miliun rupiah hanya di jeluk kala saat itu lagi punca. Julie Hercules Lethia
This is exactly what I was looking for. Thanks for writing! Francene Laird Valentina
As I website possessor I conceive the articles here is real wonderful, regards for your efforts. Karlen Arlen Elberta
Very good article. I definitely love this website. Continue the good work! Florella Hasty Calv
I pray that you will feel the manifested presence of the Holy One who is always with you. Blessings to you. Naoma Adler Pelpel
Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is great, as well as the content! Madelina Hendrik Loftis
Excellent post. I am dealing with many of these issues as well.. Gracia Moss Raddatz
Unfortunately very few narcissists will attend therapy. They will not recognise the type of behaviour that you describe because they often reinvent history and believe their new version in which everyone else was wrong and they were right. Brittney Izaak Desdamonna