jsx code:
<div className="[&>button]:text-white,px-6 flex gap-2">
<button>Right</button>
<button>Left</button>
<button>Circle</button>
<button>Square</button>
<button>Stop</button>
</div>
I was wondering if there was a way in tailwind that would allow me to use multiple classes on all the buttons at once in oneline in react?
so all the btns can take classes
"text-white px-6
and also give the parent div
his own classes "flex gap-2"
rather than typing [&>button]:text-white [&>button]:px-6
2
Answers
Yes, you can apply multiple classes to children elements at once in Tailwind CSS. Here’s an example:
Yes, you can use the
classNames
utility from theclassnames
library in React to apply multiple classes to your buttons in a single line. Here’s an example of how you can use it:In the above code, the
classNames
function takes multiple class names as arguments and returns a string with all the class names concatenated together. You can pass each individual class name as a separate argument to the function.Alternatively, you can also pass an array of class names to the
classNames
function:Both approaches will produce the same result, allowing you to apply multiple classes to your buttons in a single line.