SOLVED

How to add custom CSS when building SPFx web part

%3CLINGO-SUB%20id%3D%22lingo-sub-292258%22%20slang%3D%22en-US%22%3EHow%20to%20add%20custom%20CSS%20when%20building%20SPFx%20web%20part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292258%22%20slang%3D%22en-US%22%3E%3CP%3ECurrently%20I%20add%20my%20custom%20CSS%20style%20to%20the%20NameWebPart.module.scss%20but%20when%20generated%20Sharepoint%20appends%20a%20random%20suffix%20to%20the%20class%20I%20created.%20For%20example%20a%20class%20%3CSTRONG%3E.circle%3C%2FSTRONG%3E%20could%20render%20as%20%3CSTRONG%3E.circle%3C%2FSTRONG%3E%3CSPAN%3E%3CSTRONG%3E_3c48f1c2%3C%2FSTRONG%3E.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20assign%20CSS%20class%20to%20nodes%20programmatically.%20However%20this%20suffix%20makes%20it%20impossible%20to%20get%20my%20design%20to%20render%20correctly.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EAny%20solution%20to%20having%20my%20own%20custom%20css%20without%20the%20_suffix%20%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EOr%20at%20least%20a%20way%20to%20predict%20the%20css%20class%20that%20will%20be%20generated%20in%20Javascript%3F%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292774%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20custom%20CSS%20when%20building%20SPFx%20web%20part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292774%22%20slang%3D%22en-US%22%3E%3CP%3EAnother%20option%20perhaps%20is%20you%20can%20create%20a%20function%20with%20a%20switch%2Fcase%20that%20returns%20the%20correct%20CSS%20class%20based%20on%20the%20status%20value.%20That%20way%20you%20still%20remove%20the%20logic%20from%20the%20rendering%20function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292758%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20custom%20CSS%20when%20building%20SPFx%20web%20part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292758%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20tried%20your%202%20options%20without%20luck.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20first%20one%20generates%20the%20class%20as%3A%20%3CEM%3Estyles.pending%3C%2FEM%3E%20...%3C%2FP%3E%3CP%3EWith%20the%20style%20object%2C%20I%20have%20%3CEM%3Eundefined%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%20I%20will%20just%20use%20the%20%3CSTRONG%3Eglobal%3C%2FSTRONG%3E%20keyword%20...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292752%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20custom%20CSS%20when%20building%20SPFx%20web%20part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292752%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20I%20can%20see%20the%20problem%20now.%3C%2FP%3E%3CP%3EI%20think%20it's%20just%20down%20to%20how%20the%26nbsp%3B%3CSTRONG%3E_statusstyle%20%3C%2FSTRONG%3Eobject%20is%20created.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20try%20something%20like%20this%3F%20Hopefully%20the%20generated%20class%20name%20for%20pending%20is%20correct%3C%2FP%3E%3CP%3E_statusstyle%3D%20%7B%3C%2FP%3E%3CP%3Epending%3A%20styles.pending%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20perhaps%20try%20to%20access%20the%20class%20directly%20from%20the%20styles%20object%2C%20assuming%20that%20the%20class%20names%20are%20the%20same%3C%2FP%3E%3CP%3E%3CEM%3E%3C%2FEM%3E%3C%2FP%3E%3CDIV%20class%3D%22%26quot%3B%24%7B%22%20styles.listitem%3D%22%22%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EI'm%20just%20trying%20to%20think%20about%20options%2C%20but%20haven't%20tested%20it%20so%20sorry%20if%20it%20doesn't%20work%3C%2FEM%3E%3C%2FP%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292744%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20custom%20CSS%20when%20building%20SPFx%20web%20part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292744%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Joel%20for%20this.%20It%20can%20guide%20me%20in%20the%20right%20direction...%3C%2FP%3E%3CP%3EHowever%20I%20can%20be%20more%20explicit%20about%20what%20I%20wanted%20to%20do%3A%3C%2FP%3E%3CP%3EI%20load%20items%20from%20a%20SP%20List%20(REST)%3A%20depending%20on%20the%20value%20of%20a%20field%20called%20%3CSTRONG%3Estatus%3C%2FSTRONG%3E%2C%20I%20apply%20different%20style%20via%20CSS%20classes%20to%20the%20rendering%20of%20the%20item.%20And%20there%20are%20many%20statuses.%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20I%20created%20a%20javascript%20object%20that%20matches%20the%26nbsp%3B%3CSTRONG%3Estatus%3C%2FSTRONG%3E%26nbsp%3Bvalue%20with%20corresponding%20CSS%20class%20called%26nbsp%3B%3CSTRONG%3E_statusstyle%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20when%20it%20comes%20to%20generate%20the%20HTML%2C%20I%20do%20the%20following%20(in%20a%20loop%20over%20the%20list%20item).%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CEM%3E%3CDIV%20class%3D%22%26quot%3B%24%7B%22%20styles.listitem%3D%22%22%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EThis%20will%20automatically%20apply%20the%20corresponding%20CSS%20class%20to%20the%20div%20e.g.%20%3CSTRONG%3Epending%3C%2FSTRONG%3E%20but%20the%20problem%20is%20that%20SP%20will%20generate%20something%20like%20%3CSTRONG%3Epending_3c48f1c2.%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EDo%20you%20see%20the%20dilemma%20now%3F%20Is%20there%20a%20way%20to%20do%20something%20like%26nbsp%3B%3CEM%3E%24%7B%20styles._statusstyle%5Bstatus%5D%7D%3C%2FEM%3E%3C%2FDIV%3E%3CDIV%3EAn%20alternative%20will%20be%20using%20%3CSTRONG%3Eif%3C%2FSTRONG%3E%20or%20%3CSTRONG%3Eswitch%3C%2FSTRONG%3E%20statement%20but%20it%20will%20be%20too%20long...%3C%2FDIV%3E%3C%2FEM%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292607%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20custom%20CSS%20when%20building%20SPFx%20web%20part%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292607%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F247174%22%20target%3D%22_blank%22%3E%40imbourg%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20prefix%20is%20a%20good%20thing%20and%26nbsp%3Bensures%20that%20the%20CSS%20on%20your%26nbsp%3Bsolution%20will%20never%20override%20any%20existing%20CSS%20class%20on%20the%20page.%3C%2FP%3E%3CP%3EI%20don't%20now%20if%20I%26nbsp%3Bunderstood%20your%20question%20correctly%2C%20but%20my%20impression%20is%20that%20you%20are%20having%20problems%20using%20your%20CSS%20on%20your%20own%26nbsp%3BHTML%20elements.%3C%2FP%3E%3CP%3EIf%20this%20is%20the%20case%20(apologies%20if%20not)%2C%26nbsp%3Byou%20can%20use%20the%20%3CSTRONG%3Estyles%3C%2FSTRONG%3E%20typed%20object%20that%20is%20imported%20at%20the%20top%20of%20your%20components%26nbsp%3Bas%20it%20contains%20a%20reference%20to%20your%20CSS%20classes%20and%20is%20resolved%20into%20the%20correct%26nbsp%3B%20class%20name%20%2B%20suffix%20when%20you%20build%20your%20solution.%3C%2FP%3E%3CP%3EExample%20for%20a%20react%20component%3A%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CDIV%20classname%3D%22%26quot%3B%7Bstyles.myCSSClassName%7D%26quot%3B%22%3E%3C%2FDIV%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20still%20want%20to%20generate%20global%20styles%20(I%20would%20not%20recommend%20unless%20you%20have%20a%20valid%20reason)%2C%20you%20can%20prefix%20your%20CSS%20classes%20with%20%3CSTRONG%3E%3Aglobal%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EExample%3A%3C%2FP%3E%3CP%3E%3CSPAN%3E%3Aglobal%20.myGlobalClassName%20%7B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%20display%3A%20block%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThis%20will%20generate%20a%20class%20without%20the%20suffix%20at%20the%20end.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EYou%20can%20get%20some%20useful%20recommendations%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fspfx%2Fcss-recommendations%3Fview%3Dsp-typescript-latest%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EHope%20it%20helps%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

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_3c48f1c2.

I assign CSS class to nodes programmatically. However this suffix makes it impossible to get my design to render correctly.

Any solution to having my own custom css without the _suffix ?

Or at least a way to predict the css class that will be generated in Javascript?

5 Replies
Highlighted
Solution

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

 

Highlighted

Thanks Joel for this. It can guide me in the right direction...

However I can be more explicit about what I wanted to do:

I load items from a SP List (REST): depending on the value of a field called status, I apply different style via CSS classes to the rendering of the item. And there are many statuses. 

So I created a javascript object that matches the status value with corresponding CSS class called _statusstyle

 

Now when it comes to generate the HTML, I do the following (in a loop over the list item).

<div class="${ styles.listItem } ${ _statusstyle[status]}">
 
This will automatically apply the corresponding CSS class to the div e.g. pending but the problem is that SP will generate something like pending_3c48f1c2.
 
Do you see the dilemma now? Is there a way to do something like ${ styles._statusstyle[status]}
An alternative will be using if or switch statement but it will be too long...

 

 

Highlighted

Hi,

 

Yes, I can see the problem now.

I think it's just down to how the _statusstyle object is created.

 

Can you try something like this? Hopefully the generated class name for pending is correct

_statusstyle= {

pending: styles.pending

}

 

Or perhaps try to access the class directly from the styles object, assuming that the class names are the same

<div class="${ styles.listItem } ${ styles[status]}">

 

I'm just trying to think about options, but haven't tested it so sorry if it doesn't work

Highlighted

Just tried your 2 options without luck.

 

The first one generates the class as: styles.pending ...

With the style object, I have undefined

 

I think I will just use the global keyword ...

Highlighted

Another option perhaps is you can create a function with a switch/case that returns the correct CSS class based on the status value. That way you still remove the logic from the rendering function