I had the same problem, but with flexbox.
<div class="flex gap-6">
<div class="basis-1/3">Test</div>
<div class="basis-1/3">Test</div>
<div class="basis-1/3">Test</div>
<div class="basis-1/3">Test</div>
</div>
In this case the gap would not allow me to have 3 columns, so I needed to remove some spacing. Inside my tailwind.config.ts file, I extended the theme for flexBasis. Here is the full code:
flexBasis({ theme }) {
const spacing = theme("spacing");
const size = theme("size");
// Filter percentage sizes and generate custom flex-basis values
const percentageSizes = Object.keys(size).filter((key) => key.includes('/'));
// Map over percentage sizes and spacing to generate CSS custom properties
const flexBasisValues = percentageSizes.flatMap((sizeKey) =>
Object.entries(spacing).map(([spacingKey, spacingValue]) => {
const [numerator, denominator] = sizeKey.split('/').map(Number);
const calcValue = `calc((100% - ${(denominator - 1)} * ${spacingValue}) / ${denominator} * ${numerator})`;
return [`${sizeKey}-calc-${spacingKey}`, calcValue];
})
);
return Object.fromEntries(flexBasisValues);
},
The end result made it so I can simply write:
<div class="flex gap-6">
<div class="basis-1/3-calc-6">Test</div>
<div class="basis-1/3-calc-6">Test</div>
<div class="basis-1/3-calc-6">Test</div>
<div class="basis-1/3-calc-6">Test</div>
</div>
Instead of writing this for each child:
basis-[calc((100%_-_2_*_theme(spacing.6))_/_3)]
Also my approach made it so I can easily change the gap size and update the children easily.
I believe this answer gives another approach that wasn't mentioned.
@utilitydirective for create a specially100vh - h-{number}value ash-screen-minus-{number}class. Show utilities here.