You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: update Office UI Fabric refs to Fluent UI and refresh library c… (#10735)
* docs: update Office UI Fabric refs to Fluent UI and refresh library component overview
* docs: fix formatting, style, and terminology across SPFx articles
- remove extra `<br/>` tags and blank lines in css-recommendations.md
- promote H4 code example headings to H3 for proper hierarchy
- fix "Can I use__?" link text to "Can I use?"
- flatten nested folder tree for readability
- bold first-column entries in permission-style tables
- fix "typescript" casing to "TypeScript"
- update "Azure Active Directory" to "Entra ID"
- fix "webpart" to "web part" in fluent-ui-integration.md
- replace "alternative" with "alternate" in library-component-overview.md
---------
Co-authored-by: Andrew Connell <me@andrewconnell.com>
Copy file name to clipboardExpand all lines: docs/spfx/css-recommendations.md
+21-50Lines changed: 21 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,46 +1,41 @@
1
1
---
2
2
title: Recommendations for working with CSS in SharePoint Framework solutions
3
3
description: Use CSS to define how your SharePoint Framework customization should look and behave.
4
-
ms.date: 03/08/2023
4
+
ms.date: 03/23/2026
5
5
ms.localizationpriority: high
6
6
---
7
-
8
-
9
7
# Recommendations for working with CSS in SharePoint Framework solutions
10
8
11
9
When building SharePoint Framework solutions, you can use CSS to define how your customization should look and behave. This article describes how you can make the best use of the capabilities provided with the SharePoint Framework and build your CSS styles in a robust way.
12
10
13
11
## SharePoint Framework customizations are part of the page
14
12
15
-
When building SharePoint customizations using the add-in model, the solution is isolated from other elements on the page. Your code can be executed in an `<iframe>` as an add-in part, or as an immersive application that takes control of the whole page. In both cases, your code is not affected by other elements and styles defined on the page.
13
+
When building SharePoint customizations using the add-in model, the solution is isolated from other elements on the page. Your code can be executed in an `iframe` as an add-in part, or as an immersive application that takes control of the whole page. In both cases, your code isn't affected by other elements and styles defined on the page.
16
14
17
-
SharePoint Framework solutions are a part of the page and integrate fully with the page's DOM. While this removes a number of restrictions that come with the add-in model, it exposes your solution to risk. Because it's a part of the page, unless explicitly isolated, all CSS styles present on the page apply to it, potentially resulting in an experience different from what you intended. To avoid such risks, you should define your CSS styles in such a way so that they won't affect anything else on the page other than your customization.
15
+
SharePoint Framework solutions are a part of the page and integrate fully with the page's DOM. While this removes many restrictions that come with the add-in model, it exposes your solution to risk. Because it's a part of the page, unless explicitly isolated, all CSS styles present on the page apply to it, potentially resulting in an experience different from what you intended. To avoid such risks, you should define your CSS styles in such a way so that they won't affect anything else on the page other than your customization.
18
16
19
17
## Organize CSS files in your solution
20
18
21
19
The UI of your solutions often consists of multiple building blocks. In many JavaScript libraries, these building blocks are called components. A component can be simple and define only the presentation, or it can be more complex and include state and other components. Splitting your solution into multiple components allows you to simplify the development process and makes it easier to test and reuse the components in your solution.
22
20
23
-
Because components have presentation, they often require CSS styles. Ideally, components should be isolated and be able to be used on their own. With that in mind, it makes perfect sense for you to store CSS styles for the particular component along with all other asset files next to the component. Following is a sample structure of a React application with a number of components, each with its own CSS file.
21
+
Because components have presentation, they often require CSS styles. Ideally, components should be isolated and be able to be used on their own. With that in mind, it makes perfect sense for you to store CSS styles for the particular component along with all other asset files next to the component. Following is a sample structure of a React application with many components, each with its own CSS file.
24
22
25
23
```text
26
-
todoWebPart\components
27
-
\todoList
28
-
\todoList.tsx
24
+
todoWebPart\components\todoList\todoList.tsx
29
25
\todoList.module.scss
30
-
\todoItem
31
-
\todoItem.tsx
26
+
\todoItem\todoItem.tsx
32
27
\todoItem.module.scss
33
28
```
34
29
35
30
## Use Sass
36
31
37
-
In SharePoint Framework, you can use both CSS and Sass. Sass is a superset of CSS and offers you a number of features such as variables, nesting selectors, or mixins, all of which simplify working with and managing CSS styles over the long term.
32
+
In SharePoint Framework, you can use both CSS and Sass. Sass is a superset of CSS and offers you many features such as variables, nesting selectors, or mixins, all of which simplify working with and managing CSS styles over the long term.
38
33
39
-
For a complete set of features, see the [Sass website](http://sass-lang.com). All valid CSS is also valid Sass, which is very helpful if you haven't worked with Sass before and want to gradually learn its capabilities.
34
+
For a complete set of features, see the [Sass website](http://sass-lang.com). All valid CSS is also valid Sass, which is helpful if you haven't worked with Sass before and want to gradually learn its capabilities.
40
35
41
36
## Avoid using IDs in markup
42
37
43
-
Using SharePoint Framework, you build customizations that end-users add to SharePoint. It's impossible to tell upfront if the particular customization is used only once on a page or if there are multiple instances of it.
38
+
Using SharePoint Framework, you build customizations that endusers add to SharePoint. It's impossible to tell upfront if the particular customization is used only once on a page or if there are multiple instances of it.
44
39
45
40
To avoid issues, you should always assume that there are multiple instances of your customization on the same page. With that in mind, you should avoid using any IDs in your markup. IDs are meant to be unique on a page, and if a user adds your web part to the page twice, it violates this premise, possibly leading to errors.
46
41
@@ -62,8 +57,6 @@ export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorld
62
57
}
63
58
```
64
59
65
-
<br/>
66
-
67
60
#### Good practice
68
61
69
62
```typescript
@@ -90,7 +83,7 @@ To help you avoid styling conflicts, SharePoint Framework uses [CSS modules](htt
90
83
91
84
Following the previous example, assume that you had the following two Sass files:
92
85
93
-
####todoList.module.scss
86
+
### todoList.module.scss
94
87
95
88
```scss
96
89
.todoList {
@@ -103,9 +96,7 @@ Following the previous example, assume that you had the following two Sass files
103
96
}
104
97
```
105
98
106
-
<br/>
107
-
108
-
#### todoItem.module.scss
99
+
### todoItem.module.scss
109
100
110
101
```scss
111
102
.todoItem {
@@ -117,11 +108,9 @@ Following the previous example, assume that you had the following two Sass files
117
108
}
118
109
```
119
110
120
-
<br/>
121
-
122
111
After building the project, in the **lib** folder you would see the following two CSS files generated (line breaks and indenting added for readability):
123
112
124
-
####todoList.module.css
113
+
### todoList.module.css
125
114
126
115
```css
127
116
.todoList_3e9d35f0 {
@@ -134,9 +123,7 @@ After building the project, in the **lib** folder you would see the following tw
134
123
}
135
124
```
136
125
137
-
<br/>
138
-
139
-
#### todoItem.module.css
126
+
### todoItem.module.css
140
127
141
128
```css
142
129
.todoItem_f7081cc4 {
@@ -148,13 +135,11 @@ After building the project, in the **lib** folder you would see the following tw
148
135
}
149
136
```
150
137
151
-
<br/>
152
-
153
-
Even though there was a **.text** class defined in both Sass files, notice how in the generated CSS files it has two different hashes appended to it, becoming a unique class name specific to each component.
138
+
Even though there was a `.text` class defined in both Sass files, notice how in the generated CSS files it has two different hashes appended to it, becoming a unique class name specific to each component.
154
139
155
140
The CSS class names in CSS modules are generated dynamically, which makes it impossible for you to refer to them in your code directly. Instead, when processing CSS modules, the SharePoint Framework toolchain generates a JavaScript file with references to the generated class names.
156
141
157
-
####todoList.module.scss.js
142
+
### todoList.module.scss.js
158
143
159
144
```javascript
160
145
"use strict";
@@ -171,8 +156,6 @@ exports.default = styles;
171
156
//# sourceMappingURL=todoList.module.scss.js.map
172
157
```
173
158
174
-
<br/>
175
-
176
159
To use the generated class names in your code, you first import the styles of your component, and then use the property pointing to the particular class:
For the CSS modules to work correctly, you have to meet the following conditions:
196
177
197
178
* Your Sass files must have the **.module.scss** extension. If you use the **.scss** extension without **.module**, you see a warning in the build process. The Sass file is transpiled to an intermediate CSS file, but the class names *will not be made unique*. In cases when you need to override third-party CSS styles, this might be intended.
@@ -206,7 +187,7 @@ By combining CSS modules with Sass support for nesting rule sets, you can simpli
206
187
207
188
When building CSS styles for a component, wrap them in a class named after the component. In the component, assign that class to the component's root element.
208
189
209
-
####todoList.module.scss
190
+
### todoList.module.scss
210
191
211
192
```scss
212
193
.todoList {
@@ -216,9 +197,7 @@ When building CSS styles for a component, wrap them in a class named after the c
After transpilation, the generated CSS file looks similar to:
240
217
241
218
```css
@@ -258,8 +235,6 @@ In case a web part should use the new flex box model defined by the `display: fl
258
235
}
259
236
```
260
237
261
-
<br/>
262
-
263
238
The Sass code of the SharePoint Framework artefact does not need to have vendor prefixes included. After the Sass-to-CSS compilation, those were added automatically, resulting in the following CSS declaration.
264
239
265
240
```css
@@ -270,8 +245,6 @@ The Sass code of the SharePoint Framework artefact does not need to have vendor
270
245
}
271
246
```
272
247
273
-
<br/>
274
-
275
248
Removing already applied prefixes does not only make the code of the artefact cleaner, it also makes it easier to read and future-ready. This process is also configured to support only SharePoint Framework-supported browsers and makes it more error-safe.
276
249
277
250
In case a web part already has vendor prefixes included in the Sass files that are not needed anymore, the same process removes those declarations automatically.
@@ -289,8 +262,6 @@ The following example makes use of the `border-radius` property, which does not
289
262
}
290
263
```
291
264
292
-
<br/>
293
-
294
265
The resulting CSS in the package is converted to the following code.
295
266
296
267
```css
@@ -299,13 +270,13 @@ The resulting CSS in the package is converted to the following code.
299
270
}
300
271
```
301
272
302
-
For more information about auto-prefixing, see the [autoprefixer](https://github.com/postcss/autoprefixer) GitHub repository. The database for this process is available on [Can I use__?](https://caniuse.com).
273
+
For more information about auto-prefixing, see the [autoprefixer](https://github.com/postcss/autoprefixer) GitHub repository. The database for this process is available on [Can I use?](https://caniuse.com).
303
274
304
-
## Integrate Office UI Fabric
275
+
## Integrate Fluent UI
305
276
306
-
By making your customizations look and behave like the standard functionality of SharePoint and Office 365, you make it easier for end-users to work with them. Office UI Fabric offers you a set of controls and styles to use in your customizations to seamlessly integrate with the existing user experience.
277
+
By making your customizations look and behave like the standard functionality of SharePoint and Microsoft 365, you make it easier for endusers to work with them. Fluent UI (formerly Office UI Fabric) offers you a set of controls and styles to use in your customizations to seamlessly integrate with the existing user experience.
307
278
308
-
For more information about using Office UI Fabric in SharePoint Framework, see [Using Office UI Fabric Core and Fabric React in SharePoint Framework](./office-ui-fabric-integration.md).
279
+
For more information about using Fluent UI in SharePoint Framework, see [Using Fluent UI in SharePoint Framework](./fluent-ui-integration.md).
0 commit comments