使用 CSS3 包裝彈性項


要使用 CSS3 包裝彈性項,請使用 flex-wrap 屬性。將值包裝設定成能夠包裝。

包裝彈性項

在這個示例中,我們使用 flex-wrap: wrap 包裝彈性項。以下是我們的彈性容器:-

<div class="container">
   <div class="first">First Div</div>
   <div class="second">Second Div</div>
   <div class="third">Third Div</div>
</div>

我們對彈性容器進行了如下樣式設定。flex-wrap 被設定成包裝彈性項 -

.container {
   height: 300px;
   display: flex;
   width: 300px;
   border: 2px solid red;
   flex-wrap: wrap;
}

示例

以下是使用 CSS3 包裝彈性項的程式碼 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      .container {
         height: 300px;
         display: flex;
         width: 300px;
         border: 2px solid red;
         flex-wrap: wrap;
      }
      div {
         width: 150px;
         height: 100px;
         color: white;
         text-align: center;
         font-size: 20px;
      }
      .first {
         background-color: rgb(55, 0, 255);
      }
      .second {
         background-color: red;
      }
      .third {
         background-color: rgb(140, 0, 255);
      }
   </style>
</head>
<body>
   <h1>Flex wrap example</h1>
   <div class="container">
      <div class="first">First Div</div>
      <div class="second">Second Div</div>
      <div class="third">Third Div</div>
   </div>
</body>
</html>

包裝彈性項並在項的周圍設定相等的空間

在這個示例中,我們將使用 flex-wrap 包裝彈性項 -

flex-wrap: wrap;

為了在項周圍設定相等的空間,我們使用了 justify-content 屬性 -

justify-content: space-evenly;

示例

讓我們看下示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      .container {
         display: flex;
         flex-wrap: wrap;
         justify-content: space-evenly;
         background-color: lightblue;
      }
      .container1 {
         align-self: flex-start;
         display: flex;
         background-color: rgb(71, 30, 255);
         width: 200px;
         margin: 20px;
      }
      .container1 > div {
         background-color: #f1f1f1;
         margin: 10px;
         padding: 10px;
         font-size: 30px;
      }
      .container2 {
         display: flex;
         background-color: rgb(14, 126, 79);
         width: 200px;
         justify-content: center;
         align-self: flex-start;
         margin: 20px;
      }
      .container2 > div {
         background-color: #f1f1f1;
         margin: 10px;
         padding: 10px;
         font-size: 30px;
      }
      .container3 {
         display: flex;
         flex-direction: column;
         background-color: rgb(168, 60, 10);
         width: 200px;
         align-items: center;
         margin: 20px;
      }
      .container3 > div {
         background-color: #f1f1f1;
         margin: 10px;
         padding: 10px;
         width: 20px;
         font-size: 30px;
      }
   </style>
</head>
<body>
   <h1>Flex layout example</h1>
   <div class="container">
      <div class="container1">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
      <div class="container2">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
      <div class="container3">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
      <div class="container1">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
   </div>
</body>
</html>

更新於: 2023 年 11 月 2 日

94 次瀏覽

開啟您的 事業

完成課程並取得認證

開始
廣告
© . All rights reserved.