調整 CSS Grid 的間距


要調整間距,在 CSS 中使用 grid-column-gap、grid-row-gap 或 grid-gap 屬性。

grid-column-gap 屬性

使用 CSS 設定網格列之間的間距。你可以嘗試執行以下程式碼來實現 grid-column-gap 屬性。

示例

實際演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         .container {
            display: grid;
            background-color: green;
            grid-template-columns: auto auto;
            padding: 20px;
            grid-column-gap: 20px;
         }
         .ele {
            background-color: orange;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
         }
      </style>
   </head>
   <body>
      <h1>Game Board</h1>
      <div class = "container">
         <div class = "ele">1</div>
         <div class = "ele">2</div>
         <div class = "ele">3</div>
         <div class = "ele">4</div>
         <div class = "ele">5</div>
         <div class = "ele">6</div>
      </div>
   </body>
</html>

grid-row-gap 屬性

使用 CSS 設定網格行之間的間距。你可以嘗試執行以下程式碼來實現 grid-row-gap 屬性。

示例

實際演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         .container {
            display: grid;
            background-color: green;
            grid-template-columns: auto auto;
            padding: 20px;
            grid-column-gap: 20px;
            grid-row-gap: 20px;
         }
         .ele {
            background-color: orange;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
         }
      </style>
   </head>
   <body>
      <h1>Game Board</h1>
      <div class = "container">
         <div class = "ele">1</div>
         <div class = "ele">2</div>
         <div class = "ele">3</div>
         <div class = "ele">4</div>
         <div class = "ele">5</div>
         <div class = "ele">6</div>
      </div>
   </body>
</html>

grid-gap 屬性

使用 CSS 設定網格行和列之間的間距。你可以嘗試執行以下程式碼來實現 grid-gap 屬性。

示例

實際演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         .container {
            display: grid;
            background-color: green;
            grid-template-columns: auto auto;
            padding: 20px;
            grid-gap: 20px 20px;
         }
         .ele {
            background-color: orange;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
         }
      </style>
   </head>
   <body>
      <h1>Game Board</h1>
      <div class = "container">
         <div class = "ele">1</div>
         <div class = "ele">2</div>
         <div class = "ele">3</div>
         <div class = "ele">4</div>
         <div class = "ele">5</div>
         <div class = "ele">6</div>
      </div>
   </body>
</html>

更新時間:2020 年 6 月 25 日

3K+ 瀏覽

開啟您的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.