Lesson 34 / الدرس 34
Grid 1 — rows, columns & gap / الشبكة 1 — الصفوف والأعمدة و gap
Turn any element into a grid with display: grid, size its columns and rows with grid-template-columns/grid-template-rows, the fr unit and repeat(), and space cells apart with gap.
حوّل أي عنصر إلى شبكة بخاصية display: grid، وحدد أحجام أعمدتها وصفوفها بـ grid-template-columns و grid-template-rows ووحدة fr ودالة repeat()، وباعد بين خلاياها بـ gap.
Flexbox arranges items along one line — even when it wraps, you are still thinking in one dimension at a time. Grid arranges in two dimensions at once: you lay out rows and columns together, up front, and every item simply lands into a cell. Turn any element into a grid the same way you turned one into a flex container: with one line of display.
display..gallery {
display: grid;
grid-template-columns: 100px 100px 100px;
}
display: grid turns the container into a grid, and grid-template-columns defines exactly three columns, 100px each. Every item lands into the next empty cell automatically, row after row.
display: grid الحاوية إلى شبكة، وتحدد grid-template-columns ثلاثة أعمدة بعرض 100px لكل منها. ويسقط كل عنصر تلقائيًا في الخلية الفارغة التالية، صفًا بعد صف.
Columns and rows: grid-template-columns and grid-template-rows
grid-template-columns و grid-template-rows
Each value in grid-template-columns creates one column track, in order — three values make three columns. grid-template-rows does the same for rows, though most of the time you let rows size themselves automatically and only fix the columns. Fixed units like px work, but the fr unit — a fraction of whatever space is left after gaps and fixed tracks are subtracted — is what you will use in almost every real grid, because it is the one unit that never causes overflow.
grid-template-columns تنشئ مسار عمود واحد، بالترتيب — فثلاث قيم تعني ثلاثة أعمدة. وتفعل grid-template-rows الشيء نفسه للصفوف، رغم أنك غالبًا تترك الصفوف تحدد حجمها تلقائيًا وتضبط الأعمدة فقط. الوحدات الثابتة مثل px تعمل، لكن وحدة fr — وهي جزء من المساحة المتبقية بعد طرح الفجوات والمسارات الثابتة — هي ما ستستعمله في كل شبكة حقيقية تقريبًا، لأنها الوحدة الوحيدة التي لا تسبب فيضانًا خارج الحاوية أبدًا..gallery {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
2fr against its neighbors' 1fr each. Resize nothing — fr does the math for you.
2fr مقابل 1fr لكل من جاريه. لا تغيّر شيئًا يدويًا — فوحدة fr تحسب ذلك عنك.
repeat() and gap
repeat() والخاصية gap
Writing 1fr six times to make six equal columns is exactly the kind of repetition repeat() exists to remove: repeat(3, 1fr) means exactly the same as 1fr 1fr 1fr. gap works exactly as it did in Flexbox — it adds space between rows and columns only, never on the outer edge — except that in Grid it spaces in both directions at once.
1fr ست مرات لصنع ستة أعمدة متساوية هي بالضبط نوع التكرار الذي وُجدت repeat() لتزيله: فـ repeat(3, 1fr) تعني تمامًا نفس 1fr 1fr 1fr. وتعمل gap تمامًا كما عملت في Flexbox — تضيف مسافة بين الصفوف والأعمدة فقط، لا على الحافة الخارجية أبدًا — إلا أنها في الشبكة تباعد في الاتجاهين معًا دفعة واحدة..gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
}
repeat(), and a real gap between every row and column. The fourth cell wraps onto a second row automatically — you never wrote grid-template-rows at all.
repeat()، وفجوة حقيقية بين كل صف وعمود. تنتقل الخلية الرابعة إلى صف ثانٍ تلقائيًا — دون أن تكتب grid-template-rows إطلاقًا.grid-template-columns: 33% 33% 33% يبدو مطابقًا لـ fr حتى تضيف gap: فالنسب المئوية تُحسب من عرض الحاوية الكامل، فلا يبقى للفجوة مصدر، ويفيض الصف خارج الحاوية. أما وحدات fr فتُحسب بعد طرح الفجوات، فتناسب repeat(3, 1fr) مع gap تمامًا دائمًا. فضّل fr على النسب المئوية لهذا السبب وحده.| Property | Does what |
|---|---|
| display: grid | Turns the element into a grid container |
| grid-template-columns | Defines the column tracks, one value per column |
| grid-template-rows | Defines the row tracks (often left automatic) |
| fr unit | A share of the space left after gaps and fixed tracks |
| repeat(n, size) | Repeats a track size n times |
| gap | Space between rows and columns, both directions |
Every property from this lesson, side by side.
Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does grid-template-columns: 1fr 1fr 1fr create?
grid-template-columns: 1fr 1fr 1fr؟Each value creates one column track; three equal fr values split the row into three equal columns.
fr تقسّم الصف إلى ثلاثة أعمدة متساوية.
2.
Why is fr usually better than % for grid-template-columns once you add a gap?
fr عادة على % في grid-template-columns بمجرد إضافة gap؟Percentages are a share of the full container width and know nothing about gap; fr is calculated from what is left after gaps are removed.
gap؛ أما fr فتُحسب من المساحة المتبقية بعد طرح الفجوات.
3.
What does repeat(4, 1fr) mean?
repeat(4, 1fr)؟repeat() just writes a repeated pattern for you — repeat(4, 1fr) is shorthand for four 1fr tracks in a row.
repeat() تكتب نمطًا متكررًا نيابة عنك فقط — repeat(4, 1fr) اختصار لأربعة مسارات 1fr متتالية.Score / النتيجة: 0 / 3
Your task / مهمتك
Turn the six photo thumbnails below into a real grid: three equal columns using fr, a gap between every cell, and use repeat() instead of writing 1fr three times.
حوّل الصور المصغّرة الست أدناه إلى شبكة حقيقية: ثلاثة أعمدة متساوية باستخدام fr، وفجوة بين كل خلية، واستعمل repeat() بدل كتابة 1fr ثلاث مرات.
-
The container has
display: gridالحاوية بهاdisplay: grid -
The columns use the
frunit الأعمدة تستخدم وحدةfr -
repeat()defines the three columnsrepeat()تحدد الأعمدة الثلاثة -
gapspaces the cells apartgapتباعد بين الخلايا