Skip to content

Commit fe0ca41

Browse files
brandyscarneycamwiegert
authored andcommitted
docs(theming): add more limitations for CSS variables (ionic-team#481)
closes ionic-team#167
1 parent 77e7fcf commit fe0ca41

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

‎src/pages/theming/advanced.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,54 @@ body {
238238
color: rgba(var(--ion-text-color-rgb), 0.25);
239239
}
240240
```
241+
242+
243+
### Variables in Media Queries
244+
245+
CSS variables in [media queries](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries) are not currently supported, but there are open drafts to add [custom media queries](https://drafts.csswg.org/mediaqueries-5/#custom-mq) and [custom environment variables](https://drafts.csswg.org/css-env-1/) that would solve this problem! However, with the current state of support, the following will **not** work:
246+
247+
```css
248+
:root {
249+
--breakpoint: 600px;
250+
}
251+
252+
@media (min-width: var(--breakpoint)) {
253+
/* Doesn't work :( */
254+
}
255+
```
256+
257+
258+
### Modifying CSS Color Variables
259+
260+
While it is possible to easily alter a color in Sass using its built-in functions, it is currently not as easy to modify colors set in CSS Variables. This can be accomplished in CSS by splitting the [RGB](https://developer.mozilla.org/en-US/docs/Glossary/RGB) or [HSL](https://en.wikipedia.org/wiki/HSL_and_HSV) channels and modifying each value, but it is complex and has missing functionality.
261+
262+
What exactly does this mean? Basically, using a CSS preprocessor, such as Sass, allows us to use functions to manipulate a single color. For example, we can create the following colors in Sass:
263+
264+
```scss
265+
// Background color, shade, and tint
266+
$background: #3880ff;
267+
$background-shade: mix(#000, $background, 12%);
268+
$background-tint: mix(#fff, $background, 10%);
269+
270+
// Text color, darker and lighter
271+
$text: #444;
272+
$text-darker: darken($text, 15);
273+
$text-lighter: lighten($text, 15);
274+
```
275+
276+
After running through the Sass compiler, the colors will have the following values:
277+
278+
| Variable | Value |
279+
| --------------------| ----------------------------------------------------------|
280+
| `$background` | <code-color mode="md" value="#3880ff"></code-color> |
281+
| `$background-shade` | <code-color mode="md" value="#3171e0"></code-color> |
282+
| `$background-tint` | <code-color mode="md" value="#4c8dff"></code-color> |
283+
| `$text` | <code-color mode="md" value="#444444"></code-color> |
284+
| `$text-darker` | <code-color mode="md" value="#1e1e1e"></code-color> |
285+
| `$text-lighter` | <code-color mode="md" value="#6a6a6a"></code-color> |
286+
287+
However, because CSS variables can be set at runtime and are more dynamic, it is not currently possible to manipulate them using a simple function.
288+
289+
This is normally not a problem, but when an application needs to have dynamic theming it presents issues. In Ionic, this is the reason that there are [variations to each color](#layered-colors), and it is also why [stepped colors](#stepped-colors) are necessary for theming.
290+
291+
There are drafts and issues discussing [color modification proposals](https://github.com/w3c/csswg-drafts/issues/3187) that would make this possible.

0 commit comments

Comments
 (0)