Tailwind: dynamic grid columns with no gaps

The documentation is not very clear about all the grid options there.

I have this grid template in alpinejs styling it with tailwind:

<div x-show="multiple" class="w-auto grid mt-3" :style="`grid-template-columns: 
 repeat(${images.length < 6 ? images.length : 6}, minmax(0, 1fr))`">
    <template x-for="image in images" :key="image.name">
        <div
            x-data="{
                imgUrl: null
            }"
            x-init="() => {
                let reader = new FileReader();

                reader.onload = e => {
                    imgUrl = e.target.result;
                };

                console.log('image');
                console.log(image);

                reader.readAsDataURL(image);
            }"
        >
        <img :src="imgUrl" alt="image.name" class="h-24 w-auto">
        </div>
    </template>
</div>

The problem I’m facing is that the grid container seems to be keeping the full width of its parent and the grid elements get these huge gaps between:

What I want is that each image to be nicely aligned on the left next to each other.

Snippet here

<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>

<style>
[x-cloak] { display: none !important }
</style>

<div
    x-data="{
        inputId: 'blabla',
        selectedFileName: '',
        fileInput: null,
        multiple: true,
        images: [],
        imgCols: 0
    }"
    x-init="() => {
        fileInput = document.getElementById(inputId);
    }"
    class="w-full"
    x-on:drop="$event => {
        $event.preventDefault();
        const dataTransfer = new DataTransfer();

        if (multiple) {
            Array.from(fileInput.files).forEach(file => {
            dataTransfer.items.add(file);
            });
        }

        Array.from($event.dataTransfer.files).forEach(file => {
            dataTransfer.items.add(file);
        });

        fileInput.files = dataTransfer.files;

        if (!multiple) {
            selectedFileName = $event.dataTransfer.files[0].name;
        } else {
            images = Array.from(fileInput.files);
        }

        fileInput.dispatchEvent(new Event('change', { bubbles: true }));
    }"
    x-on:dragover.prevent="() => console.log('drag over prevented')"
>
    <label class="flex justify-center w-full h-32 px-4 transition bg-white border-2 border-gray-300 border-dashed rounded-md appearance-none cursor-pointer hover:border-gray-400 focus:outline-none"
    >
        <span class="flex items-center space-x-2">
            <x-icon name="upload" />
            <span class="font-medium text-gray-600">
                drop or
                <span class="text-blue-600 underline">browse</span>
            </span>
        </span>
        <input id="blabla" type="file" name="file_upload" >
    </label>
    <div x-cloak class="w-full text-center" x-show="selectedFileName">
        files selected: <span x-text="selectedFileName"></span>
    </div>
    <div x-show="multiple" class="w-auto grid mt-3" :style="`grid-template-columns: repeat(${images.length < 6 ? images.length : 6}, minmax(0, 1fr))`">
        <template x-for="image in images" :key="image.name">
            <div
                x-data="{
                    imgUrl: null
                }"
                x-init="() => {
                    let reader = new FileReader();

                    reader.onload = e => {
                        imgUrl = e.target.result;
                    };

                    reader.readAsDataURL(image);
                }"
            >
            <img :src="imgUrl" alt="image.name" class="h-24 w-auto">
            </div>
        </template>
    </div>
</div>

Thanks.

2

The white space is caused by the grid track columns being wider than the images they contain. This is due to the minmax(0, 1fr) value inside the repeat(). This value means that the grid column tracks are allowed to increase in width to 1 “share” of space.

To prevent this behavior, you could consider using a different value inside the repeat(), like max-content:

<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>

<style>
  [x-cloak] {
    display: none !important;
  }
</style>

<div
  x-data="{
    inputId: 'blabla',
    selectedFileName: '',
    fileInput: null,
    multiple: true,
    images: [],
    imgCols: 0
  }"
  x-init="() => {
    fileInput = document.getElementById(inputId);
  }"
  class="w-full"
  x-on:drop="$event => {
    $event.preventDefault();
    const dataTransfer = new DataTransfer();

    if (multiple) {
      Array.from(fileInput.files).forEach(file => {
        dataTransfer.items.add(file);
      });
    }

    Array.from($event.dataTransfer.files).forEach(file => {
      dataTransfer.items.add(file);
    });

    fileInput.files = dataTransfer.files;

    if (!multiple) {
        selectedFileName = $event.dataTransfer.files[0].name;
    } else {
        images = Array.from(fileInput.files);
    }

    fileInput.dispatchEvent(new Event('change', { bubbles: true }));
  }"
  x-on:dragover.prevent="() => console.log('drag over prevented')"
>
  <label
    class="flex justify-center w-full h-32 px-4 transition bg-white border-2 border-gray-300 border-dashed rounded-md appearance-none cursor-pointer hover:border-gray-400 focus:outline-none"
  >
    <span class="flex items-center space-x-2">
      <x-icon name="upload" />
      <span class="font-medium text-gray-600">
        drop or
        <span class="text-blue-600 underline">browse</span>
      </span>
    </span>
    <input id="blabla" type="file" name="file_upload" />
  </label>
  <div x-cloak class="w-full text-center" x-show="selectedFileName">
    files selected: <span x-text="selectedFileName"></span>
  </div>
  <div
    x-show="multiple"
    class="w-auto grid mt-3"
    :style="`grid-template-columns: repeat(${images.length < 6 ? images.length : 6}, max-content)`"
  >
    <template x-for="image in images" :key="image.name">
      <div
        x-data="{
          imgUrl: null
        }"
        x-init="() => {
          let reader = new FileReader();

          reader.onload = e => {
            imgUrl = e.target.result;
          };

          reader.readAsDataURL(image);
        }"
      >
        <img :src="imgUrl" alt="image.name" class="h-24 w-auto" />
      </div>
    </template>
  </div>
</div>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật