2

Let's consider the following example:

<div class="text-xs text-slate-600 uppercase font-bold tracking-wider">Text</div>

I have this div so defined, subsequently in other parts of code I use the same classes.

Is there a way to define the name of a class, which contains all the classes?

For example pseudocode:

.myComponentName {
use .text-xs 
use .text-slate-600 
use .uppercase 
use .font-bold 
use .tracking-wider
}

For example in react js I could define a component and solve the problem.

function myComponentName(text) {
 return <div class="text-xs text-slate-600 uppercase font-bold tracking-wider">{text}</div>
}

But I was wondering are there other solutions?

I was reading about this, it could be the solution: https://tailwindcss.com/docs/reusing-styles#extracting-classes-with-apply

1 Answer 1

4

I would suggest reading the entire page about reusing styled

But for this specific problem read this

HTML:

<!-- Before extracting a custom class -->
<div class="standard-text">Text</div>

CSS:

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .standard-text {
    @apply text-xs text-slate-600 uppercase font-bold tracking-wider;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't that the same article I was referring to at the end?
@Paul yes, sorry, didn't see that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.