如何在Bootstrap的方形網格中指定分割線間距?


Bootstrap是一個廣泛用於網頁設計的CSS框架,因為它提供了各種預構建元件,從而加快了開發速度。Bootstrap 為開發者提供了多種功能,其中網格系統允許開發者快速輕鬆地建立響應式佈局。

在構建任何 Web 應用程式時,網格佈局非常常用,它用於將內容組織成不同的列和行。Bootstrap 中的網格系統設計得更靈活且可定製,以便開發者可以建立滿足不同需求的佈局。

本文介紹如何使用 Bootstrap 在方形網格中指定分割線間距。Bootstrap 提供了多種方法來在方形網格中新增分割線間距。本文將探討一些常用的方法,例如網格系統、自定義 CSS 和 Flexbox 工具。

方法一:使用網格系統

Bootstrap 的網格系統由一系列容器、行和列組成,用於排列和對齊內容。它使用 Flexbox 構建,並且完全響應式。我們將使用網格系統來指定分割線間距。在這裡,我們可以使用 gutter 屬性來在列之間新增空間。請注意,預設情況下,gutter 值設定為 30px,但我們可以根據需要調整它以獲得所需的分割線間距。

語法

以下語法表示使用網格系統指定分割線間距。

<div class="container">
   <div class="row">
      <div class="col-md-4" style="margin-bottom: 10px;">
         <!-- your content goes here  -->
      </div>
      <div class="col-md-4" style="margin-bottom: 10px;">
         <!-- your content goes here -->
      </div>
      …
   </div>
   <div class="row">
      <div class="col-md-4" style="margin-bottom: 10px;">
         <!-- your content goes here  -->
      </div>
      <div class="col-md-4" style="margin-bottom: 10px;">
         <!-- your content goes here  -->
      </div>
      …
   </div>
</div>

示例

在此示例中,使用 Bootstrap 網格系統建立了一個方形網格佈局,每行包含兩行三列。為了在網格之間建立分割線間距,我們在每一列中應用了 15px 的底部邊距,並在內部 div 元素中設定了背景顏色和高度,以獲得更好的使用者介面。

<html>
<head>
   <title>Specifying Divider Gap in Square Grid using Bootstrap</title>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css">
</head>
<body>
   <div class="container">
      <div class="row">
         <div class="col-md-4" style="margin-bottom: 15px;">
            <div style="background-color: green; height: 80px;"></div>
         </div>
         <div class="col-md-4" style="margin-bottom: 15px;">
            <div style="background-color: green; height: 80px;"></div>
         </div>
         <div class="col-md-4" style="margin-bottom: 15px;">
            <div style="background-color: green; height: 80px;"></div>
         </div>
         <div class="col-md-4" style="margin-bottom: 15px;">
            <div style="background-color: green; height: 80px;"></div>
         </div>
      </div>
   </div>
</body>
</html>

方法二:使用自定義 CSS

在這種方法中,我們將使用自定義 CSS 來使用 Bootstrap 指定方形網格中的分割線間距。為此,我們將建立一個新類並將其應用於我們想要新增間距的列。

語法

以下語法表示使用自定義 CSS 指定分割線間距。

<!-- Custom CSS styles -->
<style>
   .square-grid-container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-gap: 40px;
    }
    .square-grid-container-item {
      background-color: color;
      height: heightpx;
    }
</style>
<!-- HTML body content -->
<div class="square-grid-container">
   <div class="square-grid-container-item"></div>
   …
</div>

示例

在此示例中,我們使用網格系統建立了一個包含三列兩行的方形網格佈局。在這裡,我們還將 grid-gap 屬性設定為 40px,以便在方形網格專案之間建立分割線間距。

此外,還有兩個 CSS 類:`.square-grid-container` 用於方形網格容器,`.square-grid-container-item` 用於方形網格專案,我們在其中為 `.square-grid-container-item` 類應用了背景顏色和高度以用於UI。

<html>
<head>
   <title>Specifying Divider Gap in Square Grid using Bootstrap</title>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css">
   <style>
      .square-grid-container {
         display: grid;
         grid-template-columns: repeat(3, 1fr);
         grid-gap: 40px;
      }
      .square-grid-container-item {
         background-color: green;
         height: 80px;
      }
   </style>
</head>
<body>
   <div class="container">
      <div class="square-grid-container">
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
         <div class="square-grid-container-item"></div>
      </div>
   </div>
</body>
</html>

方法三:使用 FlexBox 工具

使用 Bootstrap 指定方形網格中分割線間距的另一種方法是使用 Flexbox 工具。靈活的盒子工具更易於使用,並且可以設計靈活的響應式佈局結構,而無需使用浮動或定位,例如,要建立 Flexbox 容器並將直接子元素轉換為 Flex 專案,我們可以使用 `d-flex` 類。

語法

以下語法表示使用 Flexbox 工具指定分割線間距。

<div class="d-flex flex-wrap justify-content-between">
   <div class="square-grid-container mb-4">...</div>
   <div class="square-grid-container mb-4">...</div>
   …
</div>

示例

在此示例中,Bootstrap 中提供的 Flexbox 工具用於建立具有列和行的方形網格佈局。在這裡,我們在容器元素中使用了 `d-flex` 類來應用 flex 顯示屬性,並使用了 `flex-wrap` 和 `justify-content-between` 類來建立分割線間距。

<html>
<head>
   <title>Square Grid with Divider Gap using Bootstrap Flexbox</title>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css">  
   <style>
      .square-grid-container {
         background-color: green;
         height: 100px;
      }
   </style>
</head>
<body>
   <div class="container">
      <div class="d-flex flex-wrap justify-content-between">
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
         <div class="square-grid-container mb-4">Square Grid Item</div>
      </div>
   </div>
</body>
</html>

結論

Bootstrap 的網格系統是 Web 開發中建立響應式佈局的非常好的工具。使用 Bootstrap,我們可以輕鬆建立靈活且易於定製的方形網格。在本文中,我們學習了三種在方形網格中指定分割線間距的不同方法,包括網格系統、自定義 CSS 和 Flexbox 工具。藉助提供的示例,我們現在可以更輕鬆地實現這些方法,以建立視覺上吸引人且功能強大的方形網格。

更新於:2023年5月2日

266 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.