Skip to content

Commit 1cfbb83

Browse files
authored
Add some documentation on using CSS vars (#25176)
* Add some documentation on using CSS vars Fixes #25147 * link to MDN doc
1 parent 460849d commit 1cfbb83

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/4.0/getting-started/theming.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,71 @@ These Sass loops aren't limited to color maps, either. You can also generate res
336336
{% endhighlight %}
337337

338338
Should you need to modify your `$grid-breakpoints`, your changes will apply to all the loops iterating over that map.
339+
340+
## CSS variables
341+
342+
Bootstrap 4 includes around two dozen [CSS custom properties (variables)](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables) in it's compiled CSS. These provide easy access to commonly used values like our theme colors, breakpoints, and primary font stacks when working in your browser's Inspector, a code sandbox, or general prototyping.
343+
344+
### Available variables
345+
346+
Here are the variables we include (note that the `:root` is required). They're located in our `_root.scss` file.
347+
348+
{% highlight css %}
349+
:root {
350+
--blue: #007bff;
351+
--indigo: #6610f2;
352+
--purple: #6f42c1;
353+
--pink: #e83e8c;
354+
--red: #dc3545;
355+
--orange: #fd7e14;
356+
--yellow: #ffc107;
357+
--green: #28a745;
358+
--teal: #20c997;
359+
--cyan: #17a2b8;
360+
--white: #fff;
361+
--gray: #6c757d;
362+
--gray-dark: #343a40;
363+
--primary: #007bff;
364+
--secondary: #6c757d;
365+
--success: #28a745;
366+
--info: #17a2b8;
367+
--warning: #ffc107;
368+
--danger: #dc3545;
369+
--light: #f8f9fa;
370+
--dark: #343a40;
371+
--breakpoint-xs: 0;
372+
--breakpoint-sm: 576px;
373+
--breakpoint-md: 768px;
374+
--breakpoint-lg: 992px;
375+
--breakpoint-xl: 1200px;
376+
--font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
377+
--font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
378+
}
379+
{% endhighlight %}
380+
381+
### Examples
382+
383+
CSS variables offer similar flexibility to Sass's variables, but without the need for compilation before being served to the browser. For example, here we're resetting our page's font and link styles with CSS variables.
384+
385+
{% highlight css %}
386+
body {
387+
font: 1rem/1.5 var(--font-family-sans-serif);
388+
}
389+
a {
390+
color: var(--blue);
391+
}
392+
{% endhighlight %}
393+
394+
You can also use our breakpoint variables in your media queries:
395+
396+
{% highlight css %}
397+
.content-secondary {
398+
display: none;
399+
}
400+
401+
@media (min-width(var(--breakpoint-sm))) {
402+
.content-secondary {
403+
display: block;
404+
}
405+
}
406+
{% endhighlight %}

0 commit comments

Comments
 (0)