صفوف Grid والتوزيع التلقائي
حدّد صفوفًا صريحة باستخدام grid-rows-*، وتحكّم في التوزيع التلقائي باستخدام grid-flow-*، ودَع المتصفح يضع العناصر بذكاء.
صفوف Grid والتوزيع التلقائي درس مجاني في Tailwind CSS Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Tailwind CSS Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Tailwind CSS Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Explicit vs Implicit Grid Rows
When you place items in a grid, CSS Grid creates rows in two ways: explicitly (you define them with grid-rows-*) or implicitly (the browser generates them automatically when items overflow). Understanding this distinction helps you control heights precisely when you need it, or let the browser handle it when you have variable content.
<!-- Explicit 3-row grid -->
<div class="grid grid-rows-3 grid-cols-2 gap-4 h-48">
<div class="bg-blue-100 p-3">Row 1, Col 1</div>
<div class="bg-blue-100 p-3">Row 1, Col 2</div>
<div class="bg-blue-200 p-3">Row 2, Col 1</div>
<div class="bg-blue-200 p-3">Row 2, Col 2</div>
<div class="bg-blue-300 p-3">Row 3, Col 1</div>
<div class="bg-blue-300 p-3">Row 3, Col 2</div>
</div>The grid-rows-N Utilities
Tailwind provides grid-rows-1 through grid-rows-6 to define explicit row tracks. Under the hood, grid-rows-3 compiles to grid-template-rows: repeat(3, minmax(0, 1fr)). These rows share the available height of the container equally. This is most useful when the container has a fixed or percentage-based height.
<!-- 4 equal rows in a fixed-height dashboard -->
<div class="grid grid-rows-4 gap-2 h-64">
<div class="bg-emerald-100 p-2 rounded">Section 1</div>
<div class="bg-emerald-200 p-2 rounded">Section 2</div>
<div class="bg-emerald-300 p-2 rounded">Section 3</div>
<div class="bg-emerald-400 p-2 rounded">Section 4</div>
</div>Implicit Rows and grid-auto-rows
When more items exist than your explicit rows can hold, the browser creates implicit rows. By default these implicit rows are sized to fit their content. You can control their height using custom CSS or Tailwind's arbitrary value syntax: for example, setting the row height via inline style or using the auto-rows-* utilities available in newer Tailwind versions.
<!-- Implicit rows kick in for items beyond grid-rows-2 -->
<div class="grid grid-cols-2 grid-rows-2 gap-3" style="grid-auto-rows: 80px">
<div class="bg-purple-100 p-3">Explicit row 1</div>
<div class="bg-purple-100 p-3">Explicit row 1</div>
<div class="bg-purple-200 p-3">Explicit row 2</div>
<div class="bg-purple-200 p-3">Explicit row 2</div>
<!-- These create implicit rows at 80px height -->
<div class="bg-purple-300 p-3">Implicit row</div>
<div class="bg-purple-300 p-3">Implicit row</div>
</div>Understanding grid-flow
The grid-flow property controls the auto-placement algorithm — the order in which items fill available grid cells. Tailwind's grid-flow-row (default) fills left-to-right across each row before moving down. grid-flow-col fills top-to-bottom down each column before moving right. This is like the difference between reading a book left-to-right versus reading a newspaper column by column.
<!-- Row flow (default): items fill horizontally -->
<div class="grid grid-cols-3 grid-flow-row gap-3">
<div class="bg-sky-100 p-3">1</div>
<div class="bg-sky-100 p-3">2</div>
<div class="bg-sky-100 p-3">3</div>
<div class="bg-sky-100 p-3">4</div>
</div>grid-flow-col for Column-First Layout
Applying grid-flow-col changes the auto-placement algorithm to fill columns first. Combined with grid-rows-* to limit how tall each column can grow, this creates a newspaper or Pinterest-style vertical card flow. Items fill downward in the first column, then overflow into the next column automatically.
<!-- Column-first flow: items fill downward in each column -->
<div class="grid grid-rows-3 grid-flow-col gap-3">
<div class="bg-rose-100 p-3">Col 1, Row 1</div>
<div class="bg-rose-100 p-3">Col 1, Row 2</div>
<div class="bg-rose-100 p-3">Col 1, Row 3</div>
<div class="bg-rose-200 p-3">Col 2, Row 1</div>
<div class="bg-rose-200 p-3">Col 2, Row 2</div>
<div class="bg-rose-200 p-3">Col 2, Row 3</div>
</div>Dense Auto Placement
grid-flow-dense activates the dense packing algorithm, which allows the browser to fill in holes left by large spanning items. Without dense packing, a grid with spanning items leaves gaps because placement is strictly ordered. Dense packing is ideal for image galleries where you want a tight mosaic with no wasted space.
<!-- Dense packing fills holes left by spanning items -->
<div class="grid grid-cols-3 grid-flow-dense gap-3">
<div class="col-span-2 bg-amber-200 p-3">Wide (spans 2)</div>
<div class="bg-amber-100 p-3">B</div>
<div class="bg-amber-100 p-3">C</div>
<!-- D would normally go after C, but dense fills the hole before B -->
<div class="bg-amber-100 p-3">D</div>
</div>Row and Column Flow Dense Together
You can combine flow direction and density with grid-flow-row-dense or grid-flow-col-dense. The row-dense variant fills rows first but backfills any holes, while col-dense fills columns first and backfills. These are the go-to utilities for gallery-style layouts with mixed card sizes where aesthetics matter more than strict ordering.
<!-- Row-dense: fills rows first, packs holes -->
<div class="grid grid-cols-4 grid-flow-row-dense gap-3">
<div class="col-span-3 bg-teal-300 p-3">Spans 3</div>
<div class="bg-teal-100 p-3">A</div>
<div class="bg-teal-100 p-3">B</div>
<div class="col-span-2 bg-teal-200 p-3">Spans 2</div>
<div class="bg-teal-100 p-3">C</div>
</div>Combining Grid Rows With Columns
Defining both grid-rows-* and grid-cols-* together gives you a fully specified grid template, similar to a spreadsheet. Each cell in this template has a predictable position. This approach works great for dashboard layouts where you know exactly how many sections you need and want precise control over their heights and widths.
<!-- 3-col x 2-row dashboard shell -->
<div class="grid grid-cols-3 grid-rows-2 gap-4 h-64">
<div class="col-span-3 bg-slate-700 text-white p-3 rounded">Header</div>
<div class="bg-slate-100 p-3 rounded">Widget 1</div>
<div class="bg-slate-100 p-3 rounded">Widget 2</div>
<div class="bg-slate-100 p-3 rounded">Widget 3</div>
</div>Custom Row Heights With Arbitrary Values
Tailwind's arbitrary value syntax lets you write any CSS value inside brackets. For grid rows, you can use grid-rows-[100px_1fr_auto] to define a 100px fixed header, a flexible content area, and an auto-sized footer. This is more concise than writing inline styles and benefits from JIT compilation into your production CSS.
<!-- Custom row heights: 60px header, flexible main, auto footer -->
<div class="grid h-screen" style="grid-template-rows: 60px 1fr auto">
<header class="bg-gray-900 text-white flex items-center px-6">
App Header
</header>
<main class="bg-gray-50 p-6 overflow-y-auto">
Main scrollable content
</main>
<footer class="bg-gray-800 text-white p-4 text-center">
Footer
</footer>
</div>Auto Rows in Practice
When building card grids with dynamic content, you often want uniform minimum row heights so short cards do not look dwarfed. Using grid-auto-rows via inline style or Tailwind's JIT arbitrary variant [grid-auto-rows:200px], you can ensure every row is at least as tall as needed. This prevents jarring height inconsistencies in data-driven lists.
<!-- Cards with minimum row height for visual consistency -->
<div class="grid grid-cols-3 gap-4" style="grid-auto-rows: minmax(120px, auto)">
<div class="bg-indigo-100 p-4 rounded">Short card</div>
<div class="bg-indigo-100 p-4 rounded">
<p>Longer card with</p>
<p>more content</p>
<p>filling it out</p>
</div>
<div class="bg-indigo-100 p-4 rounded">Short</div>
</div>Practical App Shell With Grid
Putting it all together, here is a full application shell using CSS Grid for the outer layout. The grid-rows define header, content, and footer heights. Inside the content row, a nested grid-cols-[sidebar_main] pattern divides the screen. This approach is clean, predictable, and requires no absolute positioning or flexbox hacks for the top-level structure.
<div class="grid h-screen" style="grid-template-rows: 56px 1fr 40px">
<header class="bg-indigo-700 text-white px-6 flex items-center">
My App
</header>
<div class="grid overflow-hidden" style="grid-template-columns: 240px 1fr">
<nav class="bg-indigo-50 p-4 overflow-y-auto border-r">
Sidebar nav
</nav>
<main class="p-6 overflow-y-auto">
Page content
</main>
</div>
<footer class="bg-gray-200 text-center text-sm flex items-center justify-center">
Copyright 2024
</footer>
</div>Quick Check
Test your understanding of Tailwind CSS Mastery concepts from this lesson.
Lesson Recap
In this lesson you learned: grid-rows-* defines explicit row tracks with equal heights, grid-flow-row and grid-flow-col control placement direction, and grid-flow-dense backfills holes left by spanning items. Next up we explore gap utilities and alignment controls inside grid containers.
الأسئلة الشائعة
هل درس «صفوف Grid والتوزيع التلقائي» مجاني؟
نعم — نص درس «صفوف Grid والتوزيع التلقائي» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Tailwind CSS Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Tailwind CSS Academy 4 دروس في المجموع.
ماذا ستتعلم في «صفوف Grid والتوزيع التلقائي»؟
حدّد صفوفًا صريحة باستخدام grid-rows-*، وتحكّم في التوزيع التلقائي باستخدام grid-flow-*، ودَع المتصفح يضع العناصر بذكاء. تتمرن على Tailwind CSS Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Tailwind CSS Academy؟
لا تُشترط خبرة سابقة. Tailwind CSS Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «صفوف Grid والتوزيع التلقائي»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Tailwind CSS Academy هذا؟
نعم. كل درس في Tailwind CSS Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحديد أعمدة Grid
- امتداد الأعمدة والصفوف
- صفوف Grid والتوزيع التلقائي
- الفجوة والتوزيع والمحاذاة في Grid