Forum Discussion

imbourg's avatar
imbourg
Copper Contributor
Nov 28, 2018
Solved

How to add custom CSS when building SPFx web part

Currently I add my custom CSS style to the NameWebPart.module.scss but when generated Sharepoint appends a random suffix to the class I created. For example a class .circle could render as .circle_3c...
  • Joel Rodrigues's avatar
    Nov 29, 2018

    Hi imbourg

     

    The prefix is a good thing and ensures that the CSS on your solution will never override any existing CSS class on the page.

    I don't now if I understood your question correctly, but my impression is that you are having problems using your CSS on your own HTML elements.

    If this is the case (apologies if not), you can use the styles typed object that is imported at the top of your components as it contains a reference to your CSS classes and is resolved into the correct  class name + suffix when you build your solution.

    Example for a react component:

    <div className="{styles.myCSSClassName}"></div>

     

    If you still want to generate global styles (I would not recommend unless you have a valid reason), you can prefix your CSS classes with :global

    Example:

    :global .myGlobalClassName {

      display: block;

    }

     

    This will generate a class without the suffix at the end.

     

    You can get some useful recommendations here

     

    Hope it helps

     

Resources