How to properly manage duplication (@apply is evil) #19195
Replies: 4 comments
-
| The Tailwind paradigm is to keep the class names and don't copy anything to CSS. If you need to reuse this, consider converting it to a component in your templating language. | 
Beta Was this translation helpful? Give feedback.
-
| Related: | 
Beta Was this translation helpful? Give feedback.
-
| Hello! You're asking the right questions about CSS architecture. The core issue is understanding when to use utilities vs. custom CSS. Here's a practical approach: Recommended Approach: Component Extraction <!-- Instead of duplicating complex utility combinations -->
<dl class="divided-list">
  <div class="list-row">
    <dt>...</dt>
    <dd class="value">...</dd>
  </div>
</dl>CSS Implementation: /* Use Tailwind's design tokens for consistency */
.divided-list {
  font-size: var(--text-xs);
  line-height: var(--text-xs--line-height);
  
  /* Use semantic CSS custom properties */
  > :not(:last-child) {
    border-bottom: 1px solid var(--color-border-color);
  }
}
.list-row {
  display: flex;
  justify-content: space-between;
  padding-inline: var(--spacing-5);
  padding-block: var(--spacing-2-5);
}
.value {
  color: var(--color-secondary);
  font-weight: var(--font-weight-medium);
}When to Copy Utility Internals: 
 Better Mindset: 
 Example with Design Tokens: /* Leverage Tailwind's system without @apply */
.data-list {
  font-size: var(--text-xs);
  line-height: var(--leading-5);
  
  .row {
    display: flex;
    justify-content: space-between;
    padding: var(--spacing-2-5) var(--spacing-5);
    
    &:not(:last-child) {
      border-bottom: 1px solid var(--color-gray-200);
    }
  }
  
  .value {
    color: var(--color-gray-600);
    font-weight: 500;
  }
}The key is using Tailwind's design system as a foundation while creating semantic, maintainable CSS for your specific components. | 
Beta Was this translation helpful? Give feedback.
-
| I don't understand why upper answer is downvoted. That exactly what I want to hear, but can not to formulate well. Thank you very much! | 
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello. So, using
@applyis not recommended. Lets assume I want to prefer this rule. I read docs Using custom CSS. Then, for example I have this tw code:I can see utility generated style, how much of them I should copy to my css? Can I just copy entire style from hint to my css and be happy?
For example
divide-ygives me:then in my css:
--tw-divide-y-reverse: 0;?line-height: var(--tw-leading, var(--text-xs--line-height));from text size utility? If I not, then I lose height in my dl ?padding-inline: 1.25rem? Or use--spacing(5)?I hope you understand my confusion and what I want to ask. How to understand what to copy? What mind set should be? thanks!
Beta Was this translation helpful? Give feedback.
All reactions