Articles about European Sharepoint Hosting Service
SharePoint Hosting – How To Apply Styles Dynamically Using JavaScript In SPFx Web Part
In this blog, we will learn how we can apply styles dynamically based on the color change in the property panel of the SPFx web part.
Requirement
We need to apply the color of text based on the selected color in the property panel. In the property panel, we have added a text box, where users will add color code or a color name that they want as the text color.
Implementation
We have applied the class name for the div where we are showing texts and in the CSS file, we need to define the color of text using variables.
The CSS will be as below,
And our HTML will be as below,
As we can see, in the CSS we have used variables for applying the color. Now we will assign the value to this variable.
First, we will get the value from the text box of the property panel as below,
Now we will use colorName variable, to assign a color to the style’s variable.
So here we created a new element for style and append this element in the head element of the page.
Whatever value we assign to the variable – projectTitleColor, it will assign to the color style in the CSS file.
Here in this example, we have used a CSS file, but you can also use the same steps for scss as well.
This is how we can apply dynamic styles. Hope this blog will be helpful!
Print article | This entry was posted by Peter on September 23, 2021 at 2:05 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |