Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Limitations of Scoped CSS

Zell LiewFollowBetter Programming--ListenShareI was excited about Scoped Css when frameworks like React and Vue introduced them a few years back.As I began trying Scoped CSS out for myself, I was disappointed and felt it didn’t live up to its hype.But after more tries, I’m happy to announce that Scoped CSS is indeed useful. You only have to realize its limitations and what to do when you face them.Before you continue further, I want to make sure we’re talking about the same thing.In this article, I’m talking about Scoped CSS, which can be found in major frameworks like React, Vue, Svelte, and Astro.I’m not talking about the native CSS :scope. You can find out more about that here.Scoped CSS was one of the biggest features that frameworks provided us with. It promises to restrict the scope of your CSS so your CSS would not leak into other components.The limitations of Scoped CSS are as follows:Now, let’s see what I mean by these limitations.From my experience, Scoped CSS in all frameworks behaves in the same manner. So, I’m going to use Svelte here to illustrate my point.Let’s say I imported a SVG component, and I'd like to style the SVG from a Menu component. Here’s what that code looks like:The SVG component contains the SVG and nothing more.If I try to style the SVG from the Menu component, the styles will not be applied.And I’m forced to use the global or the global selector to style this SVG.The global attribute negates scoped CSS and allows the entire style block to act like Normal CSS.Using the global attribute isn't a great practice because you might as well write Normal CSS instead.By the way, if you use Astro, the global attribute is a directive and not an attribute.The :global() selector allows you to expose the values wrapped inside it to the global scope.If you want to keep .Menu in the local scope, you can use :global only on the rest of the selectors.This is nice.But there’s a huge problem.The global selector cannot be used in the middle of the selector chain. So this doesn't work.Because the global selector cannot be used in the middle of the selector chain, nesting doesn't work as well.This gives Scoped CSS a big minus point in my world.Scoped CSS is also limited when you have to style slot content.For this, let’s say we have an component, and we are going to pass the contents through a .We’ll use like the following:If you try to style the slot content from Article with Scoped CSS, you'll realize it doesn't work.You need to use the global attribute or the global selector instead.You can use global selectors or the global attribute to style slot content.In this case, let’s go with global selectors.Here’s how I would style the slot content:When you use global selectors to style slot content, pay attention to these points:One: global must be written for every selector you wish to target. If you have to use a large number of global selectors, it may be worthwhile to write normal CSS instead:Two: :global should come AFTER a selector (article in this case). If :global doesn't come after a selector, you'll be writing CSS into the global scope, which is probably not what you want.If you did what I just mentioned, your selectors should look somewhat like this:Scoped CSS is a great future.Scoped CSS can be frustrating to use when you don’t know what its limitations are.Once you know the limitations, you can use Scoped CSS to its fullest potential.By the way, this article was originally written on my blog.Feel free to visit that if you want to have these articles delivered to your email when they’re released! 🙂.----Better ProgrammingI'm just a developer who loves to share what I discovered. I primarily blog at http://zellwk.com and cross-post to this website :)Zell LiewinWe’ve moved to freeCodeCamp.org/news--8VinitainBetter Programming--37Benoit RuizinBetter Programming--214Zell LiewinWe’ve moved to freeCodeCamp.org/news--Yuri BettinStackademic--8Kenton de Jong--Harshit Gambhir--Eric HuinDev Genius--Orpetron Web Design Awards TeaminOrpetron--Fresh Frontend Links--HelpStatusAboutCareersBlogPrivacyTermsText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Limitations of Scoped CSS

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×