التحكم في العناصر: دليلك الشامل لفهم CSS Grid Items

في الدرس السابق، تعلمنا كيفية إنشاء شبكة (Grid Container) وتقسيمها إلى أعمدة وصفوف. لكن السؤال الآن: كيف نتحكم في كل عنصر داخل هذه الشبكة؟ هذا هو بالضبط ما سنتعلمه اليوم: Properties Grid Items.

Grid Items هي العناصر المباشرة داخل الـ Container الشبكية (Grid Container). كل <div>، أو <p>، أو أي عنصر آخر يصبح "عنصر شبكي" (Grid Item) يمكننا التحكم في موقعه وحجمه بشكل مستقل.

فهم العناصر الأساسية في الشبكة

لنبدأ بمثال بسيط. تخيل أن لدينا Container شبكية (Grid Container) تحتوي على 6 عناصر:

<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
  <div class="item">Item 4</div>
  <div class="item">Item 5</div>
  <div class="item">Item 6</div>
</div>
.container {
  display: grid;
  grid-template-columns: repeat(3, 100px);
  grid-template-rows: repeat(2, 80px);
  gap: 10px;
}
.item {
  background-color: lightblue;
  border: 2px solid darkblue;
  padding: 10px;
}

في هذا المثال، كل عنصر .item سيشغل خلية واحدة تلقائياً، وسيتم ترتيبها من اليسار لليمين ومن أعلى لأسفل. ولكن ماذا لو أردنا تغيير هذا الترتيب؟

تحديد موقع العنصر: grid-column و grid-row

الخاصيتان الأكثر قوة للتحكم في العناصر هما grid-column و grid-row. تسمحان لك بتحديد بداية ونهاية العنصر في الأعمدة والصفوف.

كيف تعمل؟

  • الشبكة لها خطوط (Grid Lines) مرقمة. بين كل خطين توجد مسار (Track).
  • يبدأ الترقيم من 1 من الجهة اليسرى (للأعمدة) أو من الأعلى (للصفوف).
/* Example: Make the first element span two columns */
.item-1 {
  grid-column-start: 1; /* Starts from the first line */
  grid-column-end: 3;   /* Ends before the third line (occupies columns 1 and 2) */
}

/* Common shorthand method */
.item-1 {
  grid-column: 1 / 3; /* start / end */
}
/* Another example: making the element span two rows */
.item-2 {
  grid-row: 1 / 3; /* Starts from the first row line and ends before the third */
}

الكلمة السحرية: span

غالباً لا نريد حساب الخطوط يدوياً. هنا تأتي الكلمة السحرية span والتي تعني "يمتد على".

.item-special {
  /* Starts from column 2 and spans two columns */
  grid-column: 2 / span 2;

  /* Alternative: starts automatically and spans 3 columns */
  /* grid-column: span 3; */
}

مثال عملي: لنصنع رأس موقع (Header) يمتد على عرض الشبكة بالكامل.

.header {
  grid-column: 1 / span 3; /* Or simply: 1 / 4 */
  background-color: #333;
  color: white;
  text-align: center;
  padding: 20px;
}

محاذاة عنصر واحد: justify-self و align-self

تتحكم هذه الـ Properties في محاذاة العنصر داخل الخلية التي يشغلها، وليس محاذاة كل العناصر في الـ Container (وهو ما تفعله justify-items و align-items على مستوى الـ Container).

  • justify-self: تحكم في المحاذاة على الـ Axis الأفقي (من اليسار إلى اليمين).
  • align-self: تحكم في المحاذاة على الـ Axis العمودي (من الأعلى إلى الأسفل).

القيم الشائعة: start، end، center، stretch (الافتراضي).

.item-center {
  justify-self: center; /* Horizontally center the element in the cell */
  align-self: center;   /* Vertically center the element in the cell */
  /* Use both to center the element perfectly */
}

.item-end {
  justify-self: end; /* Align the element to the end of the cell */
}

الـ Property الشاملة: grid-area

هذه Property مختصرة قوية يمكنها أن تجمع كل شيء! لها استخدامان رئيسيان:

1. التسمية (للإشارة لاحقاً في grid-template-areas):

.header { grid-area: header-area; }
.sidebar { grid-area: sidebar-area; }

(سنتعمق في هذا في درس لاحق).

2. التحديد المختصر للموقع:

.item {
  /* Order: grid-row-start / grid-column-start / grid-row-end / grid-column-end */
  grid-area: 2 / 1 / 4 / 3;
  /* Meaning: starts from row 2 and column 1, ends before row 4 and before column 3 */
}

مثال شامل: بناء لوحة تحكم مصغرة

لنطبق ما تعلمناه في مثال واقعي بسيط:

<div class="dashboard">
  <div class="card profile">Profile</div>
  <div class="card stats">Statistics</div>
  <div class="card main">Main Content</div>
  <div class="card notifications">Notifications</div>
</div>
.dashboard {
  display: grid;
  grid-template-columns: 150px 1fr;
  grid-template-rows: 100px 200px;
  gap: 15px;
  padding: 20px;
}

.card {
  background: #f0f0f0;
  border-radius: 8px;
  padding: 15px;
}

.profile {
  grid-row: 1; /* Occupies the first row */
  align-self: start; /* Sticks to the top of its cell */
}

.stats {
  grid-column: 2; /* Occupies the second column */
  grid-row: span 2; /* Spans two rows */
  justify-self: center;
}

.main {
  grid-column: 1 / span 2; /* Starts from column 1 and spans to include column 2 */
  grid-row: 2; /* In the second row */
}

.notifications {
  /* Takes its automatic position (remaining cell) */
}

في هذا المثال، تحكمنا في حجم وموقع كل بطاقة (Card) بشكل مستقل داخل الشبكة.

الخلاصة

تذكر أن:

  1. grid-column و grid-row تحدد موقع العنصر ومدى امتداده.
  2. span هي حليفك لتمديد العنصر دون حساب خطوط معقدة.
  3. justify-self و align-self تتحكمان في محاذاة العنصر داخل مساحته المحددة.
  4. grid-area Property مختصرة قوية للتسمية أو تحديد الموقع.