Skip to content

Commit d2daf59

Browse files
authored
Skip color-mix(…) when opacity is 100% (#17815)
This PR improves colors with alpha values where the alpha value results in 100%. Before this change, a class like `bg-red-500/100` would be generated as: ```css .bg-red-500\/100 { background-color: #ef4444; } @supports (color: color-mix(in lab, red, red)) { .bg-red-500\/100 { background-color: color-mix(in oklab, var(--color-red-500) 100%, transparent); } } ``` But we don't need the `color-mix`, or the fallback styles at all in case the alpha value is 100%. After this change the `bg-red-500/100` class will generate as: ```css .bg-red-500\/100 { background-color: var(--color-red-500); } ``` Which is essentially the same as `bg-red-500`, but we can migrate that away in the future. At least the generated CSS is smaller. ## Test plan 1. Added a test to ensure the generated value doesn't include color-mix at all.
1 parent 3a1b27e commit d2daf59

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Upgrade: Bump all Tailwind CSS related dependencies during upgrade ([#17763](https://github.com/tailwindlabs/tailwindcss/pull/17763))
2222
- PostCSS: Ensure that errors in stylesheet dependencies are recoverable ([#17754](https://github.com/tailwindlabs/tailwindcss/pull/17754))
2323
- Upgrade: Correctly print variants starting with `@` ([#17814](https://github.com/tailwindlabs/tailwindcss/pull/17814))
24+
- Skip `color-mix(…)` when opacity is `100%` ([#17815](https://github.com/tailwindlabs/tailwindcss/pull/17815))
2425

2526
## [4.1.4] - 2025-04-14
2627

packages/tailwindcss/src/utilities.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -10695,6 +10695,8 @@ test('bg', async () => {
1069510695
'bg-red-500/2.75',
1069610696
'bg-red-500/[0.5]',
1069710697
'bg-red-500/[50%]',
10698+
'bg-red-500/100',
10699+
'bg-red-500/[100%]',
1069810700
'bg-blue-500',
1069910701
'bg-current',
1070010702
'bg-current/50',
@@ -10992,6 +10994,10 @@ test('bg', async () => {
1099210994
}
1099310995
}
1099410996

10997+
.bg-red-500\\/100 {
10998+
background-color: var(--color-red-500);
10999+
}
11000+
1099511001
.bg-red-500\\/\\[0\\.5\\] {
1099611002
background-color: #ef444480;
1099711003
}
@@ -11012,6 +11018,10 @@ test('bg', async () => {
1101211018
}
1101311019
}
1101411020

11021+
.bg-red-500\\/\\[100\\%\\] {
11022+
background-color: var(--color-red-500);
11023+
}
11024+
1101511025
.bg-transparent {
1101611026
background-color: #0000;
1101711027
}

packages/tailwindcss/src/utilities.ts

+5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ export function withAlpha(value: string, alpha: string): string {
173173
alpha = `${alphaAsNumber * 100}%`
174174
}
175175

176+
// No need for `color-mix` if the alpha is `100%`
177+
if (alpha === '100%') {
178+
return value
179+
}
180+
176181
return `color-mix(in oklab, ${value} ${alpha}, transparent)`
177182
}
178183

0 commit comments

Comments
 (0)