<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sridhar Narasimhan</title>
    <description>The latest articles on DEV Community by Sridhar Narasimhan (@dharsha007).</description>
    <link>https://dev.to/dharsha007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F464279%2F71996d6e-3cc2-44e5-8b87-ed524d95e571.jpg</url>
      <title>DEV Community: Sridhar Narasimhan</title>
      <link>https://dev.to/dharsha007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dharsha007"/>
    <language>en</language>
    <item>
      <title>.NET 5.0 Blazor bites - CSS isolation for Blazor components - part 2</title>
      <dc:creator>Sridhar Narasimhan</dc:creator>
      <pubDate>Sat, 12 Sep 2020 16:45:41 +0000</pubDate>
      <link>https://dev.to/dharsha007/net-5-0-blazor-bites-css-isolation-for-blazor-components-part-2-2d6j</link>
      <guid>https://dev.to/dharsha007/net-5-0-blazor-bites-css-isolation-for-blazor-components-part-2-2d6j</guid>
      <description>&lt;p&gt;In our previous blog, we have seen about basic CSS isolation.&lt;br&gt;&lt;br&gt;
&lt;a href="https://dev.to/dharsha007/net-5-0-blazor-bites-css-isolation-for-blazor-components-2c33"&gt;https://dev.to/dharsha007/net-5-0-blazor-bites-css-isolation-for-blazor-components-2c33&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we are going to see a few more items in CSS isolation &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; SASS for CSS isolation &lt;/li&gt;
&lt;li&gt; How to use CSS isolation with child components &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this blog, we are going to see the CSS isolation in the Blazor server application &lt;/p&gt;

&lt;p&gt;First, you create a Blazor Server application. Since Blazor CSS isolation doesn’t offer support for CSS preprocessor. So we need to ensure the preprocessor compiles to the CSS file before Blazor build is done. We can use any tools like WebPack or library to do this process. Here I am going to choose &lt;a href="https://www.nuget.org/packages/Delegate.SassBuilder/"&gt;Delegate.SassBuilder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add this NuGet package reference to the project using the below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;
    &amp;lt;PackageReference Include="Delegate.SassBuilder" Version="1.4.0"&amp;gt;&amp;lt;/PackageReference&amp;gt;
  &amp;lt;/ItemGroup&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the SCSS file to your project with the component.razor followed by SCSS file extension. Here we are going to add an SCSS file for index.razor and customize the H1 tag. Add Index.razor.scss and include the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$primary-color: red;

h1 {
    color: $primary-color;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will generate Index.razor.css when we build the project. &lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; CSS isolation is a build time step and so please ensure to rebuild whenever you change the CSS file&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t7rplOZ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/020j57dd8wrqqx7bh4ez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t7rplOZ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/020j57dd8wrqqx7bh4ez.png" alt="Delegate SASS builder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rebuild and launch the application to see the Scoped styles applied to Blazor Index component H1 tag element. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n2Zjrmgt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/61qnrxxxg34hxowwdjzn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n2Zjrmgt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/61qnrxxxg34hxowwdjzn.png" alt="Component with Scoped styles generated from SCSS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we are going to make the CSS isolation work for child components without writing specific styles for child components. To make this happen we need to add &lt;strong&gt;::deep&lt;/strong&gt; to our CSS selector in our Index.razor.scss file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$primary-color: red;

::deep h1 {
    color: $primary-color;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To check this we can add a Counter component in our Index.razor file where we have an H1 tag element. Once this is done rebuild and launch the application. It won’t work because the markup structured can’t be recognized by Blazor. &lt;em&gt;To resolve this wrap the parent and child component within the **DIV&lt;/em&gt;* tag element.*&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
    @*Parent*@ 
        &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;

        Welcome to your new app.
    @*Child*@ 
      &amp;lt;Counter&amp;gt;&amp;lt;/Counter&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k_Dn7wWy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oi811qkwb4cgyzutt0ah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k_Dn7wWy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oi811qkwb4cgyzutt0ah.png" alt="Child Component with Scoped styles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can we disable this CSS isolation in Blazor?&lt;/strong&gt; Yes, we can but it will be available in any of the RC versions of .NET 5.0. Please refer the details below in brief&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;PropertyGroup&amp;gt;
  &amp;lt;DisableScopedCssBundling&amp;gt;true&amp;lt;/DisableScopedCssBundling&amp;gt;
&amp;lt;/PropertyGroup&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/dotnet/AspNetCore.Docs/issues/19360#issuecomment-691120629"&gt;https://github.com/dotnet/AspNetCore.Docs/issues/19360#issuecomment-691120629&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you enjoyed how Scoped CSS for Blazor child components can be achieved and use SASS for Blazor CSS isolation. I have attached the sample link &lt;a href="https://github.com/Sridhar-Narasimhan/Blazor-css-isolation/tree/master/CSSIsolation"&gt;https://github.com/Sridhar-Narasimhan/Blazor-css-isolation/tree/master/CSSIsolation&lt;/a&gt; for your reference&lt;/p&gt;

</description>
    </item>
    <item>
      <title>.NET 5.0 Blazor bites - CSS isolation for Blazor components</title>
      <dc:creator>Sridhar Narasimhan</dc:creator>
      <pubDate>Sat, 12 Sep 2020 16:15:12 +0000</pubDate>
      <link>https://dev.to/dharsha007/net-5-0-blazor-bites-css-isolation-for-blazor-components-2c33</link>
      <guid>https://dev.to/dharsha007/net-5-0-blazor-bites-css-isolation-for-blazor-components-2c33</guid>
      <description>&lt;h3&gt;
  
  
  In this blog, we are going to walk through on how the CSS isolation for Blazor components can be done .NET 5.0 preview 8. Please join to check out the easy step by step process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a Blazor Webassembly application by enabling the ASP.NET core hosted option.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VX2hgE6J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uit8e4mp8lt0h2jxr6ky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VX2hgE6J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uit8e4mp8lt0h2jxr6ky.png" alt="Create Blazor webassembly appl"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a new project (Razor class library) MyComponent to the solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7AMvvTdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tnp4ytsf1m0qfpk979e4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7AMvvTdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tnp4ytsf1m0qfpk979e4.png" alt="Create Razor class library"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the created razor class library you will have styles.css in the wwwroot folder. You can delete it and create your component scoped CSS files by creating a new file next to your component with .css extension.
&lt;strong&gt;Scoped styles are available under _framework/scoped.styles.css&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7AMvvTdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tnp4ytsf1m0qfpk979e4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7AMvvTdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tnp4ytsf1m0qfpk979e4.png" alt="Create scoped CSS for Blazor components"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add MyComponent project reference to Blazor client application.&lt;/li&gt;
&lt;li&gt;Create a razor component in Blazor client application Cssisolation.razor
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@page "/cssisolation"
&amp;lt;h3&amp;gt;Cssisolation&amp;lt;/h3&amp;gt;
&amp;lt;Component1&amp;gt;
&amp;lt;/Component1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Build your application and you can check in your browser dev tools to view your newly scoped CSS classes applied to your components. Your HTML elements will have strange identifiers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o8HekzYn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3m1f2jqxvhaksbch500b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o8HekzYn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3m1f2jqxvhaksbch500b.png" alt="Browser dev tool to check strange identifiers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you understood how Scoped CSS for Blazor components can be achieved. I have attached the sample link &lt;a href="https://github.com/Sridhar-Narasimhan/Blazor-css-isolation"&gt;https://github.com/Sridhar-Narasimhan/Blazor-css-isolation&lt;/a&gt; for your reference&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
