Text Opacity

Tailwind CSS version

v1.4.0+

Utilities for controlling the opacity of an element’s text color.

Class reference

Class
Properties
.text-opacity-0—text-opacity: 0;
.text-opacity-25—text-opacity: 0.25;
.text-opacity-50—text-opacity: 0.5;
.text-opacity-75—text-opacity: 0.75;
.text-opacity-100—text-opacity: 1;

Usage

Control the opacity of an element’s text color using the .text-opacity-{amount} utilities.

Text Opacity - 图1

  1. <p class="text-purple-700 text-opacity-100">The quick brown fox ...</p>
  2. <p class="text-purple-700 text-opacity-75">The quick brown fox ...</p>
  3. <p class="text-purple-700 text-opacity-50">The quick brown fox ...</p>
  4. <p class="text-purple-700 text-opacity-25">The quick brown fox ...</p>
  5. <p class="text-purple-700 text-opacity-0">The quick brown fox ...</p>

Note that because these utilities are implemented using CSS custom properties, a .text-{color} utility must be present on the same element for them to work.

Text Opacity - 图2Don’t try to use text opacity utilities on an inherited text color

  1. <div class="text-black">
  2. <div class="text-opacity-50">...</div>
  3. </div>

Text Opacity - 图3Do make sure to add a text color utility to the same element explicitly

  1. <div class="text-black">
  2. <div class="text-black text-opacity-50">...</div>
  3. </div>

Responsive

To control an element’s text color opacity at a specific breakpoint, add a {screen}: prefix to any existing text color opacity utility. For example, use md:text-opacity-50 to apply the text-opacity-50 utility at only medium screen sizes and above.

  1. <div class="text-blue-500 text-opacity-75 md:text-opacity-50">
  2. <!-- ... -->
  3. </div>

For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.

Customizing

To customize the opacity values for all opacity-related utilities at once, use the opacity section of your tailwind.config.js theme configuration:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. opacity: {
  6. + '10': '0.1',
  7. + '20': '0.2',
  8. + '95': '0.95',
  9. }
  10. }
  11. }
  12. }

If you want to customize only the text opacity utilities, use the textOpacity section:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. extend: {
  5. textOpacity: {
  6. + '10': '0.1',
  7. + '20': '0.2',
  8. + '95': '0.95',
  9. }
  10. }
  11. }
  12. }

Learn more about customizing the default theme in the theme customization documentation.

Responsive and pseudo-class variants

By default, only responsive, hover and focus variants are generated for text opacity utilities.

You can control which variants are generated for the text opacity utilities by modifying the textOpacity property in the variants section of your tailwind.config.js file.

For example, this config will also generate active and group-hover variants:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - textOpacity: ['responsive', 'hover', 'focus'],
  6. + textOpacity: ['responsive', 'hover', 'focus', 'active', 'group-hover'],
  7. }
  8. }

Disabling

If you don’t plan to use the text opacity utilities in your project, you can disable them entirely by setting the textOpacity property to false in the corePlugins section of your config file:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + textOpacity: false,
  6. }
  7. }

← Text Color   Text Decoration →