Skip to content

How to change bg or text opacity independently of color in v4? #16674

Closed Answered by wongjn
alexchexes asked this question in Help
Discussion options

You must be logged in to vote

You could do for all the color utilities you'll use the <prefix>-opacity-* pattern for:

@utility bg-* {
  background-color: color-mix(in oklch, --value(--color-*, [color]) var(--tw-opacity, 100%), transparent);
}

@utility bg-opacity-* {
  --tw-opacity: calc(--value(integer) * 100%);
}

Though do note this will cause duplicate bg-<color> classes in the output CSS (since we can't disable the built-in bg-<color> utilites:

.bg-red-500 {
  background-color: var(--color-red-500);
}
.bg-red-500 {
  background-color: color-mix(in oklch, var(--color-red-500) var(--tw-opacity, 100%), transparent);
}

https://play.tailwindcss.com/5vJ3ka5AVO

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@alexchexes
Comment options

@wongjn
Comment options

wongjn Feb 19, 2025
Collaborator

@alexchexes
Comment options

@wongjn
Comment options

wongjn Feb 19, 2025
Collaborator

Answer selected by alexchexes
@alexchexes
Comment options

@wongjn
Comment options

wongjn Feb 20, 2025
Collaborator

@alexchexes
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants