tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

Use a `content` utility with `before`/`after` variants adds the `content` CSS property twice

Open rozsazoltan opened this issue 5 months ago • 2 comments

What version of Tailwind CSS are you using?

v4.1.8

What build tool (or framework if it abstracts the build tool) are you using?

Tailwind Play

Reproduction URL

https://play.tailwindcss.com/rO3bQlDyWh

Describe your issue

The content utility adds the content property. Since the content property is required to display ::before and ::after pseudo-elements, the CSS rule content: var(--tw-content); is automatically added to the class when using before or after variants.

However, if I apply a custom content utility together with the before or after variants, the content property is added to the class twice:

/* Generated Utilities */

.content-\[\'custom\'\] {
  --tw-content: 'custom';
  content: var(--tw-content);
}
.before\:content-\[\'custom\'\] {
  &::before {
    content: var(--tw-content);
    --tw-content: 'custom';
    content: var(--tw-content);
  }
}
.after\:content-\[\'custom\'\] {
  &::after {
    content: var(--tw-content);
    --tw-content: 'custom';
    content: var(--tw-content);
  }
}

In this case, it would be sufficient to keep only one of them.

/* Expected Generated Utilities */

.content-\[\'custom\'\] {
  --tw-content: 'custom';
  content: var(--tw-content);
}
.before\:content-\[\'custom\'\] {
  &::before {
    --tw-content: 'custom';
    content: var(--tw-content);
  }
}
.after\:content-\[\'custom\'\] {
  &::after {
    --tw-content: 'custom';
    content: var(--tw-content);
  }
}

rozsazoltan avatar May 29 '25 16:05 rozsazoltan

I haven't checked, but I assume minification takes care of the duplication? Either way, the reason for the duplication is understandable, and it's not a big deal - I just wanted to leave a note.

rozsazoltan avatar May 29 '25 16:05 rozsazoltan

Lightning CSS doesn't appear to remove the duplicate content property. There being an extra one isn't really a big deal but maybe this is a micro optimization we can make when doing our internal optimize pass.

thecrypticace avatar May 29 '25 17:05 thecrypticace

I've implemented an optimization that should clean this up. Will be in the next release. 👍

thecrypticace avatar Aug 29 '25 15:08 thecrypticace