You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django模板中Tailwind CSS Grid动态项无法按列数排成单行问题

问题根因

布局失效和动态数据渲染无关,核心错误是Grid容器被写在了Django模板的for循环内部:每遍历一条房间数据,就会生成一个新的带grid类的父容器,每个Grid容器内仅包含1个卡片子元素,无论设置多少列,单个子元素必然独占一整行。你加的红色边框其实已经能暴露这个问题:页面上会出现和房间数量相等的多个红框容器,每个框里只有1张卡片。

修复方法

将Grid容器移动到for循环外层,保证所有循环生成的卡片都是同一个Grid容器的直接子元素,即可按预设的响应式列规则自动排列。

修正后的模板代码如下:

<!-- Grid容器仅在循环外生成1次 -->
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-10 border-4 border-red-400">
    {% for room in rooms %}
    <!-- 卡片作为Grid容器的直接子项参与布局 -->
    <div class="rounded overflow-hidden border-4 border-red-400 shadow-lg">
        <a href="/room/{{room.id}}">
            <div class="relative">
                <img class="w-full"
                    src="https://images.pexels.com/photos/196667/pexels-photo-196667.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
                    alt="房间封面">
                <div
                    class="hover:bg-transparent transition duration-300 absolute bottom-0 top-0 right-0 left-0 bg-gray-900 opacity-25">
                </div>
                <div
                    class="absolute bottom-0 left-0 bg-indigo-600 px-4 py-2 text-white text-sm hover:bg-white hover:text-indigo-600 transition duration-500 ease-in-out">
                    {{room.id}}
                </div>
                <div
                    class="text-sm absolute top-0 right-0 bg-indigo-600 px-4 text-white rounded-full h-16 w-16 flex flex-col items-center justify-center mt-3 mr-3 hover:bg-white hover:text-indigo-600 transition duration-500 ease-in-out">
                    <span class="font-bold">27</span>
                    <small>March</small>
                </div>
            </div>
        </a>
        <div class="px-6 py-4">
            <a href="/room/{{room.id}}"
                class="font-semibold text-lg inline-block hover:text-indigo-600 transition duration-500 ease-in-out">{{room.name}}</a>
            <p class="text-gray-500 text-sm">
                {{room.desc}}
            </p>
        </div>
        <div class="px-6 py-4 flex flex-row items-center">
            <span class="py-1 text-sm font-regular text-gray-900 mr-1 flex flex-row items-center">
                <svg height="13px" width="13px" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
                    xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512"
                    style="enable-background:new 0 0 512 512;" xml:space="preserve">
                    <g>
                        <g>
                            <path d="M256,0C114.837,0,0,114.837,0,256s114.837,256,256,256s256-114.837,256-256S397.163,0,256,0z M277.333,256
      c0,11.797-9.536,21.333-21.333,21.333h-85.333c-11.797,0-21.333-9.536-21.333-21.333s9.536-21.333,21.333-21.333h64v-128
      c0-11.797,9.536-21.333,21.333-21.333s21.333,9.536,21.333,21.333V256z" />
                        </g>
                    </g>
                </svg>
                <span class="ml-1">6 mins ago</span>
            </span>
        </div>
    </div>
    {% endfor %}
</div>
注意事项
  • Tailwind的Grid布局规则只和DOM结构有关,和内部元素是静态编写还是后端/JS动态渲染没有任何关系,只要子元素是Grid容器的直接子节点,就会自动按列规则排布。
  • 代码中把响应式类按屏幕尺寸从小到大排序,符合Tailwind移动端优先的编写习惯,不会改变原有样式效果。
  • 修复后可通过红色边框验证:页面只会出现1个外层红框Grid容器,内部卡片会随屏幕宽度自动调整每行显示数量。

内容的提问来源于stack exchange,提问作者zendrixate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 10:27:18