Tailwind CSS V3网格项高度不均:如何利用断点实现统一高度
解决Tailwind CSS网格项断点下等高问题
问题原因
你当前把高度类(h-96、md:h-80)直接加在了卡片内层的<div>上,但网格容器的子元素(.grid-item)没有占满网格单元格的高度,导致不同卡片因内容长度差异,在断点切换时出现高度不均。
方案1:利用网格自动等高(推荐,无需固定高度)
让网格单元格自动统一高度,卡片填充整个单元格,无需手动设置固定高度:
- 给每个
.grid-item添加h-full,确保它占满所在网格单元格的高度 - 移除卡片上的固定高度类,替换为
h-full
修改后的完整代码:
<!--! Service Section --> <section class="bg-slate-900 text-white py-16"> <div> <h2 class="text-3xl font-bold text-center text-gray-100 mb-4">Our Services</h2> </div> <div class="grid md:grid-flow-col lg:grid-rows-2 lg:grid-cols-3 gap-4"> <div class="grid-item h-full"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <i class="text-5xl mb-4 fas fa-laptop-code"></i> <h3 class="text-xl font-bold mb-4">Web Development</h3> <p class="text-gray-400 leading-loose">We develop custom, responsive websites tailored to your business needs using the latest technologies and best practices.</p> </div> </div> <div class="grid-item h-full"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <i class="text-5xl mb-4 fas fa-palette"></i> <h3 class="text-xl font-bold mb-4">Graphic Design</h3> <p class="text-gray-400 leading-loose">Our team of designers creates stunning visual assets, including logos, branding materials, brochures, and more, that help your business stand out from the competition. </p> </div> </div> <div class="grid-item h-full"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <i class="text-5xl mb-4 fas fa-mobile-alt"></i> <h3 class="text-xl font-bold mb-4">Mobile Development</h3> <p class="text-gray-400 leading-loose">We build mobile apps for iOS and Android that are not only visually stunning but also provide an intuitive and engaging user experience.</p> </div> </div> <div class="grid-item h-full"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <i class="text-5xl mb-4 fas fa-search"></i> <h3 class="text-xl font-bold mb-4">SEO Optimization</h3> <p class="text-gray-400 leading-loose">Our SEO experts ensure that your website ranks higher in search engine results, driving more traffic and potential customers to your business.</p> </div> </div> <div class="grid-item h-full"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <i class="text-5xl mb-4 fas fa-server"></i> <h3 class="text-xl font-bold mb-4">Server Administration</h3> <p class="text-gray-400 leading-loose">We provide server administration services to ensure that your website is running smoothly and efficiently, with minimal downtime and maximum security.</p> </div> </div> <div class="grid-item h-full"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <i class="text-5xl mb-4 fas fa-cogs"></i> <h3 class="text-xl font-bold mb-4">IT Consulting</h3> <p class="text-gray-400 leading-loose">Our team of IT experts provides consulting services to help your business make the best use of technology, from identifying opportunities for automation to implementing security protocols.</p> </div> </div> </div> </section>
方案2:断点下固定统一高度
如果你需要在不同断点设置固定高度,同时保持等高:
- 给每个
.grid-item添加断点高度类(比如md:h-80 lg:h-96),同时加上h-full确保基础高度 - 卡片内层
<div>添加h-full,填充整个.grid-item
单网格项示例:
<div class="grid-item h-full md:h-80 lg:h-96"> <div class="p-8 bg-slate-800 rounded-lg shadow-lg h-full m-4"> <!-- 卡片内容 --> </div> </div>
关键原理
CSS Grid布局中,同一行的网格单元格默认会自动统一高度。让.grid-item占满单元格高度,再让卡片填充.grid-item,就能保证所有卡片在同一行高度一致。断点下的高度类只需作用在.grid-item上,就能实现全行列统一的固定高度。
内容的提问来源于stack exchange,提问作者AmanK1226
相关产品推荐
相关产品推荐

