Skip to content

Commit

Permalink
Update rule.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jackreimers authored Sep 29, 2023
1 parent 92dfafa commit bb77903
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions rules/scoped-css/rule.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
type: rule
title: Do you use scoped CSS?
title: Do you know when to use scoped CSS?
uri: scoped-css
authors:
- title: Jack Reimers
Expand All @@ -12,21 +12,45 @@ guid:
---

On large frontend projects with lots of components it's common to have issues with your CSS classes overwriting eachother or conflicting.
Most frontend frameworks such as Blazor, Angular and React support scoped CSS, allowing you set CSS classes on a component level.
There are a few different ways to solve this.

<!--endintro-->

### Utility Classes

One solution to this problem is using a CSS framework such as [Tailwind](https://tailwindcss.com).

#### Benefits
* Faster setup
* Simple to use
* Faster development speed once devs are familiar with it

#### Disadvantages
* Poor readability as you add more classes
* Learning curve for class names

### Scoped CSS

Most frontend frameworks such as Blazor, Angular and React support scoped CSS, allowing you set CSS classes on a component level.

When you create a scoped CSS file, those classes are only accessible to the component that the CSS file is associated with.

This is useful as you don't need to worry about class names or CSS values conflicting with eachother like you would if all the classes are set at the root level.
It also makes it much easier to change styling on individual components without breaking the styling on other classes.

### Blazor
#### Benefits
* Can reuse class names in each component without conflicts
*

#### Disadvantages
* Styles have to be manually written

#### Blazor

In Blazor scoped CSS files are associated with components with dot syntax.
For example, if I have a component called `MyComponent.razor`, I can create a file called `MyComponent.razor.css` and the classes set in that file will only apply to `MyComponent`.

#### Inheritance
##### Inheritance

Blazor supports having scoped CSS classes inherited by child components.

Expand All @@ -46,7 +70,7 @@ In `MyComponent.razor.cs`, if I use the `::deep` psuedo selector on a class, it

[Blazor CSS Isolation - Microsoft Docs](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-7.0)

### Angular
#### Angular

When you create a component in Angular, a scoped CSS file is automatically created for it along with the HTML and TypeScript files.

Expand All @@ -58,11 +82,13 @@ myComponent
|- myComponent.component.ts
```

##### Inheritance

Angular also offers the `::ng-deep` pseudo selector, but there are [some considerations](https://angular.io/guide/component-styles) when using this.

[Component Styles - Angular Docs](https://angular.io/guide/component-styles)

### React
#### React

We can achieve local, component-level styles in React by using importing from a file ending in `module.css`:

Expand Down

0 comments on commit bb77903

Please sign in to comment.