Skip to content

Commit 7b1cc38

Browse files
committed
Add README
1 parent 713bd92 commit 7b1cc38

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Tailwind CSS Custom Properties + Text Opacity Demo
2+
3+
This is a quick demo showing how you can use Tailwind's `text-opacity` (and similar) utilities when using your own CSS variables to define your colors.
4+
5+
The big secret is defining your colors in _just_ their RGB (or HSL if you prefer) _components_, and not as a full color string, like this:
6+
7+
```css
8+
:root {
9+
--color-primary: 37, 99, 235;
10+
--color-secondary: 253, 224, 71;
11+
}
12+
```
13+
14+
Then in your config file, define the colors using callbacks, taking this general form:
15+
16+
```js
17+
// tailwind.config.js
18+
module.exports = {
19+
theme: {
20+
colors: {
21+
primary: ({ opacityVariable, opacityValue }) => {
22+
if (opacityValue !== undefined) {
23+
return `rgba(var(--color-primary), ${opacityValue})`
24+
}
25+
if (opacityVariable !== undefined) {
26+
return `rgba(var(--color-primary), var(${opacityVariable}, 1))`
27+
}
28+
return `rgb(var(--color-primary))`
29+
},
30+
},
31+
},
32+
// ...
33+
}
34+
```
35+
36+
This way the plugins (like text color, gradients, etc.) that need to inject some opacity data into the color can do so, while still giving you a valid RGB color string at the end of the day.
37+
38+
The `opacityValue` argument should be respected first if it exists (the gradient plugin uses this for Safari compatibility when fading to transparent), and the `opacityVariable` argument should be respected next if `opacityValue` is undefined. If neither are passed in, just don't set any opacity at all.

0 commit comments

Comments
 (0)