CSS - grid-row-start 屬性



CSS grid-row-start 屬性透過指定行、跨度或依賴自動放置來定義網格專案在網格行中的起始位置。它定義了網格區域的塊起始邊緣。

語法

grid-row-start: auto | span n | row-line;

屬性值

描述
auto 它自動確定網格專案在網格佈局中的位置。預設跨度為 1。
span n 它指定元素佔據的行數空間。
row-line 它指定元素顯示必須開始的行。

CSS Grid row Start 屬性示例

以下示例解釋了使用不同值的grid-row-start 屬性。

使用 Auto 值的 Grid Row Start 屬性

為了允許網格專案根據專案內容和網格佈局在預設位置開始,我們使用auto 值。它會根據專案需要的空間和它在網格中的當前位置自動調整起始行。這在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

<head>

   <style>
      .grid-container {
         display: grid;
         grid-template-columns: repeat(4, 1fr);
         gap: 10px;
         padding: 10px;
         background-color: #f0f0f0;
      }

      .grid-item {
         background-color: lightcoral;
         border: 3px solid blue;
         padding: 20px;
         text-align: center;
         color: white;
         grid-row-start: auto;
      }
   </style>
</head>

<body>
   <h2>
      CSS grid-row-start property
   </h2>
   <h4>
      grid-row-start: auto
   </h4>
   <div class="grid-container">
      <div class="grid-item item1 items">
         Item 1
      </div>
      <div class="grid-item">
         Item 2
      </div>
      <div class="grid-item item3 items">
         Item 3
      </div>
      <div class="grid-item">
         Item 4
      </div>
      <div class="grid-item item5 items">
         Item 5
      </div>
   </div>
</body>

</html>

使用 Span 值的 Grid Row Start 屬性

為了使網格專案從其起始行開始並跨越 n 行,我們使用 span 指定行數(例如 span 2 - 元素從起始行跨越 2 行)。這在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

<head>

   <style>
      .grid-container {
         display: grid;
         grid-template-columns: repeat(4, 1fr);
         gap: 10px;
         padding: 10px;
         background-color: #f0f0f0;
      }

      .grid-item {
         background-color: lightcoral;
         border: 2px solid #ff6b6b;
         padding: 20px;
         text-align: center;
         color: white;
      }

      .items {
         border: 3px solid blue;
      }

      .item1 {
         grid-row-start: span 2;
      }

      .item3 {
         grid-row-start: span 3;
      }

      .item4 {
         grid-row-start: span 4;
      }
   </style>
</head>

<body>
   <h2>
      CSS grid-row-start property
   </h2>
   <h4>
      grid-row-start: span 2 (item1),
      span 3 (item3), span 4 (item5)
   </h4>
   <p>
      item1- the element takes 
      2 rows space
   </p>
   <p>
      item3- the element takes 
      3 rows space
   </p>
   <p>
      item4- the element takes 
      4 rows space
   </p>
   <div class="grid-container">
      <div class="grid-item item1 items">
         Item 1
      </div>
      <div class="grid-item">
         Item 2
      </div>
      <div class="grid-item item3 items">
         Item 3
      </div>
      <div class="grid-item item4 items">
         Item 4
      </div>
      <div class="grid-item">
         Item 5
      </div>
   </div>
</body>

</html>

使用行號的 Grid Row Start 屬性

為了顯式地將網格專案的起始行設定為某個行號,我們指定行號(例如 4 - 元素必須從第 4 行開始顯示)。這在以下示例中顯示。

示例

<!DOCTYPE html>
<html>

<head>

   <style>
      .grid-container {
         display: grid;
         grid-template-columns: repeat(4, 1fr);
         gap: 10px;
         padding: 10px;
         background-color: #f0f0f0;
      }

      .grid-item {
         background-color: lightcoral;
         border: 2px solid #ff6b6b;
         padding: 20px;
         text-align: center;
         color: white;
      }

      .items {
         border: 3px solid blue;
      }

      .item1 {
         grid-row-start: 2;
      }

      .item3 {
         grid-row-start: 3;
      }

      .item5 {
         grid-row-start: 4;
      }
   </style>
</head>

<body>
   <h2>
      CSS grid-row-start property
   </h2>
   <h4>
      grid-row-start: 2 (item1),
      3 (item3), 4 (item5)
   </h4>
   <p>
      item1- the element starts 
      showing from row-line 2
   </p>
   <p>
      item3- the element starts 
      showing from row-line 3
   </p>
   <p>
      item5- the element starts 
      showing from row-line 4
   </p>
   <div class="grid-container">
      <div class="grid-item item1 items">
         Item 1
      </div>
      <div class="grid-item">
         Item 2
      </div>
      <div class="grid-item item3 items">
         Item 3
      </div>
      <div class="grid-item">
         Item 4
      </div>
      <div class="grid-item item5 items">
         Item 5
      </div>
   </div>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
grid-row-start 57 16 52 10 44
css_properties_reference.htm
廣告
© . All rights reserved.