SharePoint Framework - CSS Considerations

Overview
 
SharePoint Framework (SPFx) client-side web parts are lightweight and work on both classic and modern SharePoint sites. The look and feel is an integral part of a web part. CSS (Cascading Style Sheet) helps to apply the better look and feel to SharePoint web parts.
 
The Office UI Fabric helps in designing a seamless experience in Office 365. Please refer to my previous article here for more information. However, in certain scenarios, we also have to override a few default CSS classes offered by Office UI Fabric and SharePoint Framework
 
In this article, we will explore the common CSS considerations while developing SPFx web parts and how to override the default external reference classes from other components when needed. 
 
Manage CSS files in the SPFx solution
 
The SPFx solution consists of many components. Each component should be isolated and can be used individually whenever needed. Considering this, the CSS styles used by the component should be stored along with other assets used by that component.
 
Below is the recommended structure of SPFx solution based on React that uses multiple components.

 
Use of Sass
 
SharePoint Framework supports both SASS and CSS. SASS (Syntactically Awesome Style Sheets) is interpreted or compiled to CSS. SASS is a superset of CSS and it allows us to use variables, nested rules, mixins, inline imports, and much more, with fully CSS-compatible syntax.
 
More information about SASS can be found here (https://sass-lang.com/).
 
Use CSS classes over ID
 
Each element on a page can be accessed by ID or with CSS applied to it. Similarly, each element in SharePoint Framework web part can have an ID and CSS.
 
In real case scenarios, we may have multiple instances of the web part on a single page. Hence referring to the elements by ID inside markup might not be a good idea since an ID should be unique.
 
The below code uses ID to refer to an element and is not recommended to use.
  1. export default class GettingStartedWebPart extends BaseClientSideWebPart<IGettingStartedWebPartProps> {  
  2.   
  3.   public render(): void {  
  4.     this.domElement.innerHTML = `  
  5.       <div id="greetMessage">  
  6.         Welcome to SharePoint Framework  
  7.       </div>`;  
  8.   }  
  9. }  
It is recommended to use the code like below, which refers to the elements by CSS class.
  1. export default class GettingStartedWebPart extends BaseClientSideWebPart< IGettingStartedWebPartProps> {  
  2.   
  3.   public render(): void {  
  4.     this.domElement.innerHTML = `  
  5.       <div class="${styles. greetMessage}">  
  6.         Welcome to SharePoint Framework  
  7.       </div>`;  
  8.   }  
  9. }  
Use CSS Modules
 
SharePoint Framework web part is just one of the components along with other web parts or controls on the SharePoint page. CSS styles of one SPFx web part may conflict with CSS styles of another SPFx web part or other controls on the page.
 
SPFx resolves this conflict issue by CSS modules. The SPFx toolchain processes all the files with .module.scss extension and appends a unique hash value to each file.
 
For example, if we have the below files in the solution.
 
employee.module.scss
  1. .employee {  
  2.      margin1em 0;  
  3.   
  4.      .text {  
  5.           font-weightbold;  
  6.           font-size1em;  
  7.      }  
  8. }  
department.module.scss
  1. .department {  
  2.      padding0.5em 1em;  
  3.   
  4.      .text {  
  5.           font-weightitalic;  
  6.           font-size0.5em;  
  7.      }  
  8. }  
After building the project, the above .module.scss file gets converted to the CSS files in the lib folder as below.
 
employee.module.css
  1. .employee_f6071de4 {  
  2.      margin1em 0;  
  3. }  
  4.   
  5. .employee_f6071de4 .text_f6071de4 {  
  6.           font-weightbold;  
  7.           font-size1em;  
  8. }  
department.module.css
  1. .department_2f9c53f0 {  
  2.      padding0.5em 1em;  
  3. }  
  4.   
  5. .department_2f9c53f0 .text_2f9c53f0 {  
  6.           font-weightitalic;  
  7.           font-size0.5em;  
  8. }  
The CSS class generated is unique. Even though there is the same CSS class name being referred to in different web parts, the hash value generated makes them distinct.
 
Override external referenced CSS
 
We refer to various components in our SPFx web parts like Office 365 UI Fabric, or any third party UI components. These components come with their predefined CSS. We may need to override any of the CSS classes of an externally referenced component. In this case, we can use “global” keyword to override the CSS class in our .module.scss file as below.
  1. // Make workbench canvas use full page width  
  2. :global(#workbenchPageContent) {  
  3.     max-width: initial;  
  4. }  
  5.   
  6. // Override third party SuperTreeView CSS  
  7. :global(.super-treeview>div>.super-treeview-node>.super-treeview-node-content>input[type=checkbox])  
  8. {  
  9.   -webkit-appearance: radio; /* Chrome, Safari, Opera */  
  10.   -moz-appearance: radio;    /* Firefox */  
  11. }  
Summary
 
SharePoint Framework web parts support SASS and CSS. Each component should have its own CSS references to maintain the isolation. Always refer to the elements by CSS class instead of ID. The external CSS styles can be overridden using "global" keyword.