I have an unordered list. I want to append the word "and" at the end of each item, except the last.
<ul className="ml-4">
{
categories.map((category, Idx) => (
<li key={category.id} className="last:text-red-400">
{category.description} <span className="last:hidden">; AND</span>
</li>
)
}
</ul>
Actual output:
Red
Blue
Green
Desired output:
Red; AND
Blue; AND
Green
The "last" decorator on the "text-red-400" works. It was just for testing.
The one for "hidden" doesn’t do what I want. All the "AND" s are hidden, presumably because they are all actually the last child of each of the li items, so the decorator is always active(?).
My current solution is
<ul className="ml-4">
{
categories.map((category, Idx, arr) => (
<li key={category.id}>
{category.description} <span className={`${Idx == arr.length - 1 && "hidden"}`}>; AND</span>
</li>
)
}
</ul>
But i wondered if there was a better way apply the class so that only the word "and" on the last child of ul is hidden?
2
Answers
Not a solution by tailwind but the same result
Adding a condition on the
; AND
spanSomething like this maybe?
If there are multiple spans inside an
li
, you can specify something like this:Another solution might be to use groups:
If there are nested groups (i.e. one of the parents already has group), you can name them:
Feel free to change it to JSX. Playground: https://play.tailwindcss.com/XqG11h8pWD