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
Copy file name to clipboardExpand all lines: packages/documentation-site/patternfly-docs/content/get-started/training/html-css/css-variables-and-overrides.md
+73-39Lines changed: 73 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,19 @@ section: get-started
4
4
subsection: training
5
5
hideNavItem: true
6
6
---
7
-
import { Button, ClipboardCopy, Divider, PageSection } from '@patternfly/react-core';
7
+
import { Button, Divider } from '@patternfly/react-core';
8
8
9
-
<PageSectionvariant="light">
10
9
# CSS variables and overrides
11
-
PatternFly Core is based on the principles of <ahref="http://bradfrost.com/blog/post/atomic-web-design/"target="_blank">Atomic Design</a> and <ahref="http://getbem.com/introduction/"target="_blank">BEM</a> (Block, Element, Modifier). BEM is a popular CSS methodology for building modular, scalable applications. It provides scope, avoids inheritance, and reduces CSS specificity and conflicts.
12
10
13
-
BEM works perfectly with a modular design system, as each unique component can be represented as an independent block. Since a block is tied to a component, developers are able to develop, move around, and nest components without conflicts in their application’s CSS.
11
+
PatternFly is based on the principles of <ahref="http://bradfrost.com/blog/post/atomic-web-design/"target="_blank">Atomic Design</a> and <ahref="http://getbem.com/introduction/"target="_blank">BEM</a> (Block, Element, Modifier). BEM is a popular CSS methodology for building modular, scalable applications. It provides scope, avoids inheritance, and reduces CSS specificity and conflicts.
12
+
13
+
BEM works perfectly with a modular design system, as each unique component can be represented as an independent block. Since a block is tied to a component, developers are able to develop, move around, and nest components without conflicts in their application's CSS.
14
14
PatternFly uses a modified version of BEM for its CSS architecture. PatternFly deviates from BEM in relation to modifiers.
15
15
16
16
This tutorial covers how PatternFly uses BEM as a framework for its component library. You'll learn how to override and create both global and component-level custom properties and component elements.
17
-
</PageSection>
18
-
<PageSection>
17
+
19
18
## Part 1: Overriding PatternFly component-level properties
19
+
20
20
In PatternFly, component-level custom properties follow this general formula:
-`--pf-v6-c-block` refers to the block, usually the component or layout name (for example, `--pf-v6-c-alert`).
@@ -29,36 +29,43 @@ In PatternFly, component-level custom properties follow this general formula:
29
29
30
30
To explore this concept, you can practice overriding the title color custom property in the success variation of the alert component using the provided CodeSandbox.
31
31
32
-
<Buttonvariant="primary"component="a"href="https://codesandbox.io/s/html-fundamentals-start-hy2erg"target="_blank">Codesandbox - Part 1</Button>
32
+
<Buttonvariant="primary"component="a"href="https://codesandbox.io/p/devbox/html-fundamentals-start-forked-dll7lt?workspaceId=ws_SgkMrETzDgBAttxqLiNZfT"target="_blank">Codesandbox - Part 1</Button>
33
33
34
34
### Step 1. Familiarize with `index.html`
35
+
35
36
Note the alert component in the `<body>` of `index.html`.
36
37
37
38
### Step 2. Create custom property name and styles
38
-
Write the CSS for the custom property name in the `style.css` file. Overriding the success variation’s title color means overriding its custom property.
39
39
40
-
#### Step 2.1
40
+
Write the CSS for the custom property name in the `style.css` file. Overriding the success variation's title color means overriding its custom property.
41
+
42
+
#### Step 2.1
43
+
41
44
In the `style.css` file, in the `.pf-v6-c-alert{}` block, write the custom property name.
42
45
43
46
In reference to the formula described in Part 1, this should be: `--pf-v6-c-alert`
44
47
45
-
#### Step 2.2
48
+
#### Step 2.2
49
+
46
50
Add the modifier to the custom property name. As displayed in the CSS variables of PatternFly's <ahref="/components/alert/html#css-variables"target="_blank">alert component documentation</a>, the success variation modifier class `pf-m-success` applies to `pf-v6-c-alert`. Add that modifier to the custom property.
47
51
48
52
The custom property name should now be: `--pf-v6-c-alert--m-success`
49
53
50
-
#### Step 2.3
54
+
#### Step 2.3
55
+
51
56
Add the element to the custom property name. The element that is being changed is the title of the alert.
52
57
53
58
The custom property name should now be: `--pf-v6-c-alert--m-success__title`
54
59
55
-
#### Step 2.4
60
+
#### Step 2.4
61
+
56
62
Add the property that is being modified. In this case, modify the color property of the title in the alert component.
57
63
58
64
The custom property name should now be: `--pf-v6-c-alert--m-success__title--Color`
59
65
60
-
#### Step 2.5
61
-
Define the value of the component-level CSS variable. PatternFly's global danger color is: `--pf-v6-global--danger-color--100`. You can reference PatternFly’s <ahref="/foundations-and-styles/colors"target="_blank">global colors documentation</a> for more information.
66
+
#### Step 2.5
67
+
68
+
Define the value of the component-level CSS variable. PatternFly's global danger color is: `--pf-v6-global--danger-color--100`. You can reference PatternFly's <ahref="/foundations-and-styles/colors"target="_blank">global colors documentation</a> for more information.
62
69
63
70
Assign the custom property name `(--pf-v6-c-alert--m-success__title--Color)` that is already inside the `.pf-v6-c-alert{}` block to the global danger color.
64
71
@@ -67,11 +74,13 @@ It should look like this: `--pf-v6-c-alert--m-success__title--Color: var(--pf-v6
67
74
The resulting alert should match Figure 1.
68
75
69
76
**Figure 1**
77
+
70
78
<imgsrc="../img/alert-red-title.png"alt="Alert component with a red title"width="868" />
71
-
</PageSection>
79
+
72
80
<Divider />
73
-
<PageSection>
81
+
74
82
## Part 2: Overriding PatternFly global properties
83
+
75
84
In PatternFly, global properties follow this general formula:
@@ -86,136 +95,160 @@ To explore this concept, override the global link color by setting it to the cus
86
95
<Buttonvariant="primary"component="a"href="https://codesandbox.io/s/override-global-css-variables-start-llcub8"target="_blank">Codesandbox - Part 2</Button>
87
96
88
97
### Step 1. Familiarize with `index.html`
98
+
89
99
Note the button component and the expandable section component in the `<body>` of the `index.html` file.
90
100
91
101
### Step 2. Override a global custom property
92
102
93
-
#### Step 2.1
103
+
#### Step 2.1
104
+
94
105
Write the prefix for the global custom property in the `:root` block in the `styles.css` file. Since global custom properties are prefixed with the word global, use that for the beginning of the custom property.
95
106
96
107
It should look like this: `--pf-v6-global`
97
108
98
-
#### Step 2.2
109
+
#### Step 2.2
110
+
99
111
Add the concept to the global custom property. The concept for the link color is `link`.
100
112
101
113
The global custom property should now look like this: `--pf-v6-global--link`
102
114
103
-
#### Step 2.3
115
+
#### Step 2.3
116
+
104
117
Add the property to the global custom property. As the color is being modified, add the color property as the next step.
105
118
106
119
The global custom property should now look like: `--pf-v6-global--link--Color`
107
120
108
-
#### Step 2.4
121
+
#### Step 2.4
122
+
109
123
Define the value of the global variable. PatternFly's global danger color is: `--pf-v6-global--danger-color--100`.
110
124
Assign the custom property name `(--pf-v6-global--link--Color)` that is already inside of the `:root` block to the global danger color.
111
125
112
126
It should look like this: `--pf-v6-global--link--Color: var(--pf-v6-global--danger-color--100);`
113
127
114
-
The resulting UI in the CodeSandbox should match Figure 2.
128
+
The resulting UI in the CodeSandbox should match Figure 2.
115
129
116
130
**Note:** The links in the button component and expandable component both have inherited the new red link color.
117
131
118
132
**Figure 2.**
119
133
120
134
<imgsrc="../img/global-link-color-red.png"alt="An inline button and a 'show more' expandable section toggle both colored red"width="300" />
121
-
</PageSection>
135
+
122
136
<Divider />
123
-
<PageSection>
137
+
124
138
## Part 3: Create and use component level CSS variables to override PatternFly styles
139
+
125
140
It is important to understand how BEM is used to create and override variables.
126
141
127
142
To explore this concept, add a BEM element and custom styles with CSS custom properties to support a badge element in the label component using the provided CodeSandbox for part 3. Assume there is a common use case for adding a badge to the left of text in the label component. When adding the custom badge element to the label component, also apply a margin-right of 8px.
128
143
129
144
<Buttonvariant="primary"component="a"href="https://codesandbox.io/s/creating-new-component-css-vars-start-t98i06"target="_blank">Codesandbox - Part 3</Button>
130
145
131
146
### Step 1. Familiarize with `index.html`
147
+
132
148
Note the label component in the `<body>` of the `index.html`. This label contains a badge component and some text. The default PatternFly label component only styles the label itself and the text inside. Since there has been a badge component passed into the label, it is necessary to add space between the badge and the text beside it.
133
149
134
150
To do this the BEM way, a BEM element class `(pf-v6-c-label__badge)` has been applied to the badge.
135
151
136
152
### Step 2. Style the badge
153
+
137
154
**Note:** Never apply a global custom property as the value for a property in a component's CSS.
138
155
139
-
#### Step 2.1
156
+
#### Step 2.1
157
+
140
158
Create a new variable to represent the badge's margin. Within style.css, in the `.pf-v6-c-label{}` block, define the beginning of the custom property, which is the name of the component.
141
159
142
160
It should look like: `--pf-v6-c-label`
143
161
144
-
#### Step 2.2
162
+
#### Step 2.2
163
+
145
164
Define the value of the new custom property. Add the element after the name of the component.
146
165
147
166
The custom property should now be: `--pf-v6-c-label__badge`
148
167
149
-
#### Step 2.3
168
+
#### Step 2.3
169
+
150
170
Add the property being applied to the badge after the element.
151
171
152
172
It should look like this: `--pf-v6-c-label__badge--MarginRight`
153
173
154
-
#### Step 2.4
155
-
Define the value of the new custom CSS variable. PatternFly's global variable for 8px of space is `--pf-v6-global--spacer--sm`. You can reference <ahref="/foundations-and-styles/spacers"target="_blank">PatternFly’s documentation about spacers</a> for more information.
174
+
#### Step 2.4
175
+
176
+
Define the value of the new custom CSS variable. PatternFly's global variable for 8px of space is `--pf-v6-global--spacer--sm`. You can reference <ahref="/foundations-and-styles/spacers"target="_blank">PatternFly's documentation about spacers</a> for more information.
156
177
157
178
Assign the custom property name `--pf-v6-c-label__badge--MarginRight` that is already inside of the `.pf-v6-c-label{}` block to the global variable for 8px of space.
158
179
159
180
It should look like this: `--pf-v6-c-label__badge--MarginRight: var(--pf-v6-global--spacer--sm);`
160
181
161
182
#### Step 2.5
183
+
162
184
Assign the new custom property name to the property that is being overridden. Add a margin-right declaration inside of `.pf-v6-c-label__badge{}` and assign it to the new margin variable.
163
185
164
186
It should look like this: `margin-right: var(--pf-v6-c-label__badge--MarginRight);`
165
187
166
188
**Note:** Once the preview reloads, there should be space to the right of the badge.
167
-
</PageSection>
189
+
168
190
<Divider />
169
-
<PageSection>
191
+
170
192
## Part 4: Create and use global CSS variables to override PatternFly styles
193
+
171
194
To explore this concept, create a new global custom property for a 5xl font size. Use this new global property to make a new variation of the title component using the provided CodeSandbox for part 4.
172
195
173
196
Referencing <ahref="/foundations-and-styles/typography"target="_blank">PatternFly's typography design guidelines</a>, the largest font size PatternFly offers is a "super hero heading" size. It is 36px represented by the global variable `pf-v6-global--FontSize--4xl`. Assume that there is a need for a larger font size for the title component that is used across the entire application.
174
197
175
198
<Buttonvariant="primary"component="a"href="https://codesandbox.io/s/creating-new-global-css-vars-start-cqb8nw"target="_blank">Codesandbox - Part 4</Button>
176
199
177
200
### Step 1. Familiarize with `index.html`
201
+
178
202
Note the title component in the `<body>` of the `index.html` file. It has a modifier class applied to it which has no PatternFly styles defined for it thus far.
179
203
180
204
### Step 2. Define a global custom property name
205
+
181
206
Follow the existing convention for global font size custom properties.
182
207
183
208
#### Step 2.1
209
+
184
210
Add the new custom property for font size inside of the `:root{}` block. This applies it to the global scope of the application. It should look like this: `--pf-v6-global--FontSize--5xl`
185
211
186
-
#### Step 2.2
212
+
#### Step 2.2
213
+
187
214
Define the value of the new global variable. Assign the property name to the pixel value for the new font size.
188
215
Update the line added in step 2.1 to be 42px.
189
216
190
217
It should look like this: `--pf-v6-global--FontSize--5xl: 42px;`
191
218
192
219
### Step 3. Create a local component level custom property
220
+
193
221
Create a local component level custom property in the `.pf-v6-c-title{}` block in the styles.css file. Set its value to the newly defined global property.
194
222
The local custom property should represent the 5xl variation's font size.
195
223
196
-
#### Step 3.1
224
+
#### Step 3.1
225
+
197
226
Beginning with the component name, write the custom property and add it to the `.pf-v6-c-title{}` block in the `style.css` file.
198
227
199
228
It should look like this: `--pf-v6-c-title`
200
229
201
-
#### Step 3.2
230
+
#### Step 3.2
231
+
202
232
Add the new modifier to the custom property name.
203
233
204
234
It should look like this: `--pf-v6-c-title--m-5xl`
205
235
206
-
#### Step 3.3
236
+
#### Step 3.3
237
+
207
238
Add the property that the custom property represents.
208
239
209
240
It should look like this: `--pf-v6-c-title--m-5xl--FontSize`
210
241
211
-
#### Step 3.4
242
+
#### Step 3.4
243
+
212
244
Define the value of the component level custom property to be equal to the newly defined global variable.
213
245
214
246
It should look like this: `--pf-v6-c-title--m-5xl--FontSize: var(--pf-v6-global--FontSize--5xl);`
215
247
216
248
### Step 4. Define the styles for the new 5xl variation.
217
249
218
250
#### Step 4.1 Chain selectors
251
+
219
252
Knowing that the modifier `.pf-m-5xl` will apply to the title component, in the CSS, chain the selector to create `.pf-v6-c-title.pf-m-5xl`
220
253
Add this block inside of `.pf-v6-c-title`, underneath the declaration from step 3.
221
254
@@ -225,15 +258,16 @@ Add this block inside of `.pf-v6-c-title`, underneath the declaration from step
225
258
}
226
259
```
227
260
228
-
#### Step 4.2
261
+
#### Step 4.2
262
+
229
263
Create a declaration within the modifier block for the font-size CSS rule using the new component level custom property.
230
264
231
265
It should look like: `font-size: var(--pf-v6-c-title--m-5xl--FontSize);` inside of `.pf-m-5xl{}`.
232
-
</PageSection>
266
+
233
267
<Divider />
234
-
<PageSection>
268
+
235
269
### Compare your results.
270
+
236
271
A fully constructed card can be viewed and modified in the CodeSandbox solution for part 4. Compare your work with the solution.
237
272
238
273
<Buttonvariant="primary"component="a"href="https://codesandbox.io/s/creating-new-global-css-vars-final-cugbq0?file=/style.css"target="_blank">Codesandbox - Part 4 Solution</Button>
0 commit comments