{"id":56367,"date":"2020-11-23T08:00:22","date_gmt":"2020-11-23T07:00:22","guid":{"rendered":"https:\/\/code-maze.com\/?p=56367"},"modified":"2021-12-24T14:46:47","modified_gmt":"2021-12-24T13:46:47","slug":"css-isolation-in-blazor-applications","status":"publish","type":"post","link":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/","title":{"rendered":"CSS Isolation in Blazor Applications"},"content":{"rendered":"<p>Until the release of .NET 5, to be more precise .NET 5 Preview 8, we could&#8217;ve used only the global styles in the Blazor project. After the mentioned release, Microsoft added support for the CSS styles that are scoped to a specific component. This means that we can use CSS isolation in Blazor WebAssembly to attach the CSS file only for the specific component in our applications and not use it globally. That said, in this article, we are going to learn how to implement CSS isolation in Blazor WebAssembly applications. Also, we will learn about the integration with a preprocessor like SAAS and how the Blazor application includes isolated styles from a lazy-loaded project.<\/p>\n<p>If you want to learn more about Blazor WebAssembly, we strongly suggest visiting our <a href=\"https:\/\/code-maze.com\/blazor-webassembly-series\/\" target=\"_blank\" rel=\"noopener noreferrer\">Blazor WebAssembly series of articles<\/a>, where you can read about Blazor WebAssembly development, authentication, authorization, JSInterop, and other topics as well.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit the <a href=\"https:\/\/github.com\/CodeMazeBlog\/blazor-wasm-css-isolation\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Css Isolation in Blazor WebAssembly<\/a> repository<\/div>\n<p>Let&#8217;s start.<\/p>\n<h2 id=\"project-preparation\">Project Preparation<\/h2>\n<p>We are going to start by creating a new Blazor WebAssembly project using the .NET 5 framework:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/01-Css-Isolation-in-Blazor-WebAssembly-project-creation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56376\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/01-Css-Isolation-in-Blazor-WebAssembly-project-creation.png\" alt=\"Css Isolation in Blazor WebAssembly project creation\" width=\"721\" height=\"340\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/01-Css-Isolation-in-Blazor-WebAssembly-project-creation.png 721w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/01-Css-Isolation-in-Blazor-WebAssembly-project-creation-300x141.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/a><\/p>\n<p>Our project comes with three already prepared pages: Index, Counter, and FetchData that we can use for our example. All of these pages have the h1 element, and we can use the <code>wwwroot\/css\/app.css<\/code> file to modify all of them:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scss\">h1 {\r\n    color: red;\r\n    font-style: italic;\r\n    text-shadow: 2px 2px 2px gray;\r\n}<\/pre>\n<p>If we start our application now and inspect all the pages, we are going to see that all the h1 elements are modified:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/02-Global-styling-in-Blazor-webassembly-app.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-56377 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/02-Global-styling-in-Blazor-webassembly-app-e1605008551104.png\" alt=\"Global styling in Blazor webassembly app\" width=\"544\" height=\"323\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/02-Global-styling-in-Blazor-webassembly-app-e1605008551104.png 544w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/02-Global-styling-in-Blazor-webassembly-app-e1605008551104-300x178.png 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/a><\/p>\n<p>This is a good solution if we want to apply our styles to all the components in our project. But what if we want separate styles for specific components? Well for that, we can isolate the CSS file for a specific component and apply the styles from that file only to a related component.<\/p>\n<p>Let&#8217;s see how to do that.<\/p>\n<h2 id=\"css-isolation-implementation\">Implementing CSS Isolation in Blazor WebAsssembly<\/h2>\n<p>We are going to implement the changes only for the <code>Index<\/code> component since it will be enough to explain the topic.<\/p>\n<p>That said, let&#8217;s start by creating a new <code>Index.razor.css<\/code> file in the <code>Pages<\/code> folder:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/03-CSS-file-connected-to-a-specific-compnent-to-support-CSS-Isolation-in-Blazor-WebAssembly.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56378\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/03-CSS-file-connected-to-a-specific-compnent-to-support-CSS-Isolation-in-Blazor-WebAssembly.png\" alt=\"CSS file connected to a specific compnent to support CSS Isolation in Blazor WebAssembly\" width=\"225\" height=\"119\" \/><\/a><\/p>\n<p>We can see this file as a part of the <code>Index.razor<\/code> file, which is the same behavior as with the <a href=\"https:\/\/code-maze.com\/partial-classes-renderfragment-lifecycle-blazor-wasm\/\" target=\"_blank\" rel=\"noopener noreferrer\">partial classes in the Blazor WebAssembly application<\/a>.<\/p>\n<p>Now we can add a new element selector in the CSS file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scss\">h1 {\r\n    color: blue;\r\n    font-style: italic;\r\n    text-shadow: 2px 2px 2px gray;\r\n}<\/pre>\n<p>We just change the color, to be able to see the difference from the h1 elements on other pages.<\/p>\n<p>And that&#8217;s all it takes.<\/p>\n<p>If we start our app now, we are going to see that only the h1 element on the <code>Index<\/code> page has a different color:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/04-Css-Isolation-in-Blazor-WebAssembly-implemented.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-56380 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/04-Css-Isolation-in-Blazor-WebAssembly-implemented-e1605078659446.png\" alt=\"Css Isolation in Blazor WebAssembly implemented\" width=\"555\" height=\"303\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/04-Css-Isolation-in-Blazor-WebAssembly-implemented-e1605078659446.png 555w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/04-Css-Isolation-in-Blazor-WebAssembly-implemented-e1605078659446-300x164.png 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/a><\/p>\n<p>Excellent.<\/p>\n<h3>But how this really works?<\/h3>\n<p>If we open the Developer Tools window of our browser and inspect the h1 element on the Index page, we are going to see a new attribute attached to it:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/05-Attribute-attached-to-the-h1-element-for-css-isolation-implementation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56381\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/05-Attribute-attached-to-the-h1-element-for-css-isolation-implementation.png\" alt=\"Attribute attached to the h1 element for css isolation implementation\" width=\"348\" height=\"204\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/05-Attribute-attached-to-the-h1-element-for-css-isolation-implementation.png 348w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/05-Attribute-attached-to-the-h1-element-for-css-isolation-implementation-300x176.png 300w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/a><\/p>\n<p>With a help of this attribute, Blazor binds the isolated styles to the specific element. What Blazor does is combining all the isolated styles into a single file named <code>CssIsolationExample.styles.css<\/code>(you will have the name of your project as a first part of the file name). To confirm that, we can first inspect the head element of the <code>index.html<\/code> page:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-highlight=\"8\">&lt;head&gt;\r\n    &lt;meta charset=\"utf-8\" \/&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" \/&gt;\r\n    &lt;title&gt;CSSISolationExample&lt;\/title&gt;\r\n    &lt;base href=\"\/\" \/&gt;\r\n    &lt;link href=\"css\/bootstrap\/bootstrap.min.css\" rel=\"stylesheet\" \/&gt;\r\n    &lt;link href=\"css\/app.css\" rel=\"stylesheet\" \/&gt;\r\n    &lt;link href=\"CSSISolationExample.styles.css\" rel=\"stylesheet\" \/&gt;\r\n&lt;\/head&gt;<\/pre>\n<p>We can see the file reference here.<\/p>\n<p>Also, let&#8217;s inspect the Sources tab of our Developer Tools window:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/06-Bundled-css-file-for-all-isolates-styles.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56382\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/06-Bundled-css-file-for-all-isolates-styles.png\" alt=\"Bundled css file for all isolates styles\" width=\"677\" height=\"373\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/06-Bundled-css-file-for-all-isolates-styles.png 677w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/06-Bundled-css-file-for-all-isolates-styles-300x165.png 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/a><\/p>\n<p>We can see the file on the left, and its content on the right. Additionally, we can confirm that the h1 element has the attribute attached, and all our styles implemented. We can also see a nice comment above the element reference that points to the component that we implement the styles on.<\/p>\n<h2 id=\"css-isolation-child-components\">Css Isolation with Child Components<\/h2>\n<p>Since we know all the theory behind the isolation process, we can test this solution with a child component. For that, we are going to stick to the <code>Index.razor<\/code> page because it already uses a child component to display some additional content.<\/p>\n<p>Let&#8217;s open the <code>Shared\/SurveyPropmt.razor<\/code> file and modify it a bit:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3\">&lt;div class=\"alert alert-secondary mt-4\" role=\"alert\"&gt;\r\n    &lt;span class=\"oi oi-pencil mr-2\" aria-hidden=\"true\"&gt;&lt;\/span&gt;\r\n    &lt;h1&gt;@Title&lt;\/h1&gt;\r\n\r\n    &lt;span class=\"text-nowrap\"&gt;\r\n        Please take our\r\n        &lt;a target=\"_blank\" class=\"font-weight-bold\" href=\"https:\/\/go.microsoft.com\/fwlink\/?linkid=2137916\"&gt;brief survey&lt;\/a&gt;\r\n    &lt;\/span&gt;\r\n    and tell us what you think.\r\n&lt;\/div&gt;\r\n\r\n@code {\r\n    \/\/ Demonstrates how a parent component can supply parameters\r\n    [Parameter]\r\n    public string Title { get; set; }\r\n}<\/pre>\n<p>As you can see, we just modify the &lt;strong&gt; element to the &lt;h1&gt; element. And that&#8217;s all. Since the h1 element of the parent component already has new styles, we expect them to be implemented on this component as well. So, let&#8217;s start the app and check it:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/07-Css-isolation-not-implemented-to-the-child-component.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56384\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/07-Css-isolation-not-implemented-to-the-child-component.png\" alt=\"Css isolation not implemented to the child component\" width=\"731\" height=\"298\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/07-Css-isolation-not-implemented-to-the-child-component.png 731w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/07-Css-isolation-not-implemented-to-the-child-component-300x122.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/a><\/p>\n<p>But, as we can see, the global styles are applied to the child component and not the isolated ones.<\/p>\n<p>To make this work, we have to implement the <code>::deep<\/code> combinator in the <code>Index.razor.css<\/code> file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scss\" data-enlighter-highlight=\"1\">::deep h1 {\r\n    color: blue;\r\n    font-style: italic;\r\n    text-shadow: 2px 2px 2px gray;\r\n}<\/pre>\n<p>In our case, just doing this is not enough. If we start the app, we are going to see the global styles implemented on both h1 elements.<\/p>\n<p>So, let&#8217;s check the Developer Tools again, to see what is going on:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/08-Css-isolation-deep-combinator-problem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56385\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/08-Css-isolation-deep-combinator-problem.png\" alt=\"Css isolation deep combinator problem\" width=\"551\" height=\"351\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/08-Css-isolation-deep-combinator-problem.png 551w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/08-Css-isolation-deep-combinator-problem-300x191.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/a><\/p>\n<p>We can see the parent&#8217;s h1 element has that attribute but the child&#8217;s don&#8217;t.<\/p>\n<p>In this markup &#8211; as it is right now &#8211; Blazor has a problem to determine the relationship between the two components. So, we have to help it a bit.<\/p>\n<p>To do that, let&#8217;s wrap the content of the <code>Index.razor<\/code> file inside the div element:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-highlight=\"3,9\">@page \"\/\"\r\n\r\n&lt;div&gt;\r\n    &lt;h1&gt;Hello, world!&lt;\/h1&gt;\r\n\r\n    Welcome to your new app.\r\n\r\n    &lt;SurveyPrompt Title=\"How is Blazor working for you?\" \/&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>Now if we start the app, everything should work:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/09-CSS-isolation-in-Blazor-WebAssembly-for-child-components.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56386\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/09-CSS-isolation-in-Blazor-WebAssembly-for-child-components.png\" alt=\"CSS isolation in Blazor WebAssembly for child components\" width=\"726\" height=\"297\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/09-CSS-isolation-in-Blazor-WebAssembly-for-child-components.png 726w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/09-CSS-isolation-in-Blazor-WebAssembly-for-child-components-300x123.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/a><\/p>\n<p>This works because Blazor now knows that these two h1 elements are inside a parent element that has the custom generated attribute:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/10-Parent-div-element-has-the-custom-created-attribute.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56387\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/10-Parent-div-element-has-the-custom-created-attribute.png\" alt=\"Parent div element has the custom created attribute\" width=\"555\" height=\"355\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/10-Parent-div-element-has-the-custom-created-attribute.png 555w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/10-Parent-div-element-has-the-custom-created-attribute-300x192.png 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/a><\/p>\n<p>Excellent.<\/p>\n<h2 id=\"preprocessor\">Adding a Preprocessor to Our Application<\/h2>\n<p>The CSS isolation topic always raises a question about the preprocessor implementation and is it supported. Well, the answer is yes. To be more precise, Blazor CSS isolation doesn&#8217;t offer a preprocessor functionality but it supports it. All we have to do is to enable the preprocessor to compile styles to the css file before we build the Blazor app. To do that, we can use the Delegate.SassBuilder package (of course, you can use the one you prefer).<\/p>\n<p>So, let&#8217;s install the package first:<\/p>\n<p><code>PM&gt; Install-Package Delegate.SassBuilder -Version 1.4.0<\/code><\/p>\n<p>Then, let&#8217;s\u00a0create the <code>Index.razor.scss<\/code> file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scss\">$color: blue;\r\n$fontStyle: italic;\r\n$textShadow: 2px 2px 2px gray;\r\n\r\n::deep h1 {\r\n    color: $color;\r\n    font-style: $fontStyle;\r\n    text-shadow: $textShadow;\r\n}<\/pre>\n<p>Now, all we have to do is to rebuild our solution and check the Output window:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/11-Precompiling-the-css-file-from-the-scss-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56388\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/11-Precompiling-the-css-file-from-the-scss-file.png\" alt=\"Precompiling the css file from the scss file\" width=\"616\" height=\"191\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/11-Precompiling-the-css-file-from-the-scss-file.png 616w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/11-Precompiling-the-css-file-from-the-scss-file-300x93.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/a><\/p>\n<p>We can see that the package generates a new <code>Index.razor.css<\/code> file. We can check that file to verify the content:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scss\">::deep h1{color:blue;font-style:italic;text-shadow:2px 2px 2px gray;}<\/pre>\n<p>Great.<\/p>\n<p>If we start our app, everything should work as it did.<\/p>\n<h2 id=\"css-isolation-lazy-loaded-projects\">Css Isolation with Lazy Loaded Projects<\/h2>\n<p>Blazor supports lazy loading which means that it loads the resources only when users require them. In other words, if the user visits only the Index page, the resources for the FetchData page (if it is configured to load in a lazy manner) won&#8217;t be loaded at all. To learn more about this feature, you can read our <a href=\"https:\/\/code-maze.com\/lazy-loading-in-blazor-webassembly\/\" target=\"_blank\" rel=\"noopener noreferrer\">Lazy Loading in Blazor WebAssembly article<\/a>.<\/p>\n<p>We might wonder, if our lazy-loaded project contains the isolated css files, how are they included in the main project.<\/p>\n<p>Well, let&#8217;s check that.<\/p>\n<p>We are going to open the project from our <a href=\"https:\/\/code-maze.com\/lazy-loading-in-blazor-webassembly\/\" target=\"_blank\" rel=\"noopener noreferrer\">Lazy Loading in Blazor WebAssembly<\/a> article and modify it a bit. In this project, we have two projects, the <code>BlazorWasmLazyLoading<\/code>, which is the main one, and the <code>Weather<\/code>, which is the project that we load in a lazy manner:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/12-Lazy-Loading-project-structure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56389\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/12-Lazy-Loading-project-structure.png\" alt=\" Lazy Loading project structure\" width=\"270\" height=\"352\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/12-Lazy-Loading-project-structure.png 270w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/12-Lazy-Loading-project-structure-230x300.png 230w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/a><\/p>\n<p>Now, let&#8217;s create a new <code>FetchData.razor.css<\/code> file in the <code>Weather<\/code> project:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"scss\">h1 {\r\n    color: red;\r\n    font-style: italic;\r\n    text-shadow: 2px 2px 2px gray;\r\n}<\/pre>\n<p>So, nothing we didn&#8217;t already see.<\/p>\n<p>Now, let&#8217;s start the app and inspect the Developer Tools window:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/13-Css-bundle-imported-in-the-main-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56390\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/13-Css-bundle-imported-in-the-main-project.png\" alt=\"Css bundle imported in the main project\" width=\"749\" height=\"609\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/13-Css-bundle-imported-in-the-main-project.png 749w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/13-Css-bundle-imported-in-the-main-project-300x244.png 300w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><\/a><\/p>\n<p>We can see that even though our Weather project is not loaded yet, the <code>bundle.scp.css<\/code> is loaded with the required styles. So this tells us that the CSS bundling is executed for the separate project before we even load it.<\/p>\n<p>Also, if we check the Sources tab, we are going to see the import statement for this bundle file:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/14-Import-files-into-main-css-scoped-bundle-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56391\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/14-Import-files-into-main-css-scoped-bundle-file.png\" alt=\" Import files into main css scoped bundle file\" width=\"747\" height=\"290\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/14-Import-files-into-main-css-scoped-bundle-file.png 747w, https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/14-Import-files-into-main-css-scoped-bundle-file-300x116.png 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/a><\/p>\n<p>Of course, if you navigate to the FetchData menu item, you are going to see the <code>weather.dll<\/code> file loaded.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Now we know how to isolate our CSS files for specific components and how this isolation process works. We also know more about preprocessing and how CSS isolation in Blazor works with lazy-loaded projects.<\/p>\n<p>Until the next article.<\/p>\n<p>All the best.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Until the release of .NET 5, to be more precise .NET 5 Preview 8, we could&#8217;ve used only the global styles in the Blazor project. After the mentioned release, Microsoft added support for the CSS styles that are scoped to a specific component. This means that we can use CSS isolation in Blazor WebAssembly to [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":56524,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[683,684,12],"tags":[685,686,816],"class_list":["post-56367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blazor","category-blazor-webassembly","category-csharp","tag-blazor","tag-blazor-webassembly","tag-css-isolation","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>CSS Isolation in Blazor Applications - Code Maze Blog<\/title>\n<meta name=\"description\" content=\"In this article, we are going to learn about CSS ISolation in Blazor WebAssembly, how it works, and how to support preprocessor.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CSS Isolation in Blazor Applications - Code Maze Blog\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to learn about CSS ISolation in Blazor WebAssembly, how it works, and how to support preprocessor.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-23T07:00:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-24T13:46:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Marinko Spasojevi\u0107\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marinko Spasojevi\u0107\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\"},\"author\":{\"name\":\"Marinko Spasojevi\u0107\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533\"},\"headline\":\"CSS Isolation in Blazor Applications\",\"datePublished\":\"2020-11-23T07:00:22+00:00\",\"dateModified\":\"2021-12-24T13:46:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\"},\"wordCount\":1321,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png\",\"keywords\":[\"Blazor\",\"Blazor WebAssembly\",\"CSS Isolation\"],\"articleSection\":[\"Blazor\",\"Blazor WebAssembly\",\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\",\"url\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\",\"name\":\"CSS Isolation in Blazor Applications - Code Maze Blog\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png\",\"datePublished\":\"2020-11-23T07:00:22+00:00\",\"dateModified\":\"2021-12-24T13:46:47+00:00\",\"description\":\"In this article, we are going to learn about CSS ISolation in Blazor WebAssembly, how it works, and how to support preprocessor.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png\",\"width\":1100,\"height\":620,\"caption\":\"CSS ISolation in Blazor\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS Isolation in Blazor Applications\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533\",\"name\":\"Marinko Spasojevi\u0107\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg\",\"caption\":\"Marinko Spasojevi\u0107\"},\"description\":\"Hi, my name is Marinko Spasojevic. Currently, I work as a full-time .NET developer and my passion is web application development. Just getting something to work is not enough for me. To make it just how I like it, it must be readable, reusable, and easy to maintain. Prior to being an author on the CodeMaze blog, I had been working as a professor of Computer Science for several years. So, sharing knowledge while working as a full-time developer comes naturally to me.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/marinko-spasojevic\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/marinko\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CSS Isolation in Blazor Applications - Code Maze Blog","description":"In this article, we are going to learn about CSS ISolation in Blazor WebAssembly, how it works, and how to support preprocessor.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/","og_locale":"en_US","og_type":"article","og_title":"CSS Isolation in Blazor Applications - Code Maze Blog","og_description":"In this article, we are going to learn about CSS ISolation in Blazor WebAssembly, how it works, and how to support preprocessor.","og_url":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/","og_site_name":"Code Maze","article_published_time":"2020-11-23T07:00:22+00:00","article_modified_time":"2021-12-24T13:46:47+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png","type":"image\/png"}],"author":"Marinko Spasojevi\u0107","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Marinko Spasojevi\u0107","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/"},"author":{"name":"Marinko Spasojevi\u0107","@id":"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533"},"headline":"CSS Isolation in Blazor Applications","datePublished":"2020-11-23T07:00:22+00:00","dateModified":"2021-12-24T13:46:47+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/"},"wordCount":1321,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png","keywords":["Blazor","Blazor WebAssembly","CSS Isolation"],"articleSection":["Blazor","Blazor WebAssembly","C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/","url":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/","name":"CSS Isolation in Blazor Applications - Code Maze Blog","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png","datePublished":"2020-11-23T07:00:22+00:00","dateModified":"2021-12-24T13:46:47+00:00","description":"In this article, we are going to learn about CSS ISolation in Blazor WebAssembly, how it works, and how to support preprocessor.","breadcrumb":{"@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/11\/CSS-ISolation-in-Blazor-1-e1606118135502.png","width":1100,"height":620,"caption":"CSS ISolation in Blazor"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/css-isolation-in-blazor-applications\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"CSS Isolation in Blazor Applications"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533","name":"Marinko Spasojevi\u0107","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg","caption":"Marinko Spasojevi\u0107"},"description":"Hi, my name is Marinko Spasojevic. Currently, I work as a full-time .NET developer and my passion is web application development. Just getting something to work is not enough for me. To make it just how I like it, it must be readable, reusable, and easy to maintain. Prior to being an author on the CodeMaze blog, I had been working as a professor of Computer Science for several years. So, sharing knowledge while working as a full-time developer comes naturally to me.","sameAs":["https:\/\/www.linkedin.com\/in\/marinko-spasojevic\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/marinko\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/56367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=56367"}],"version-history":[{"count":2,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/56367\/revisions"}],"predecessor-version":[{"id":62167,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/56367\/revisions\/62167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/56524"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=56367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=56367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=56367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}