I am currently working with Vue3 and TailwindCSS, attempting to create a grid using a dynamic grid-cols-{n}
class. While I am aware that TailwindCSS typically supports up to 12 columns by default, the challenge arises when the number of columns needed is entirely dynamic and cannot be customized within the theme.
For example, consider the following simple HTML / JS snippet:
const amountOfItemsPerRow = 16;
const container = document.getElementById("container");
for (let i = 0; i < amountOfItemsPerRow; i++) {
const item = document.createElement("div");
item.innerText = i;
container.appendChild(item);
}
container.classList.add(`grid-cols-${amountOfItemsPerRow}`); // this doesn't work if the value is greater than 12
<script src="https://cdn.tailwindcss.com"></script>
<div id="container" class="grid"></div>
In this scenario, the code functions correctly if the value of amountOfItemsPerRow
is less than or equal to 12, but breaks the CSS style when exceeding 12 columns.
Is there a way to address this issue without resorting to writing custom CSS styles, perhaps through a dynamic solution within Tailwind?
An Alternative Approach:
Following guidance from the TailwindCSS documentation, I attempted to modify the line
container.classList.add(`grid-cols-${amountOfItemsPerRow}`);
to
container.classList.add(`grid-template-columns:repeat(${amountOfItemsPerRow},minmax(0,1fr))`);
In an effort to develop a more "native" solution, however, this adjustment did not yield the desired outcome.