Flexbox - 內容對齊



如果彈性容器包含多行(當 `flex-wrap: wrap` 時),`align-content` 屬性定義容器內每行的對齊方式。

用法

align-content: flex-start | flex-end | center | space-between | space-around | stretch;

此屬性接受以下值 −

  • stretch − 內容中的行將拉伸以填滿剩餘空間。

  • flex-start − 內容中的所有行都排列在容器的起始位置。

  • flex-end − 內容中的所有行都排列在容器的結束位置。

  • center − 內容中的所有行都排列在容器的中心位置。

  • space-between − 額外空間均勻分佈在各行之間。

  • space-around − 額外空間均勻分佈在各行之間,每行周圍(包括第一行和最後一行)都有相等的間距。

center

將此值傳遞給 `align-content` 屬性時,所有行都排列在容器的中心位置。

Flex Align Content - Center

以下示例演示了將 `center` 值傳遞給 `align-content` 屬性的結果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .box{
         font-size:25px;
         padding:15px;
         width:43%;
      }
      .container{
         display:flex;
         height:100vh;
         flex-wrap:wrap;
         align-content:center;
      }
   </style>
   
   <body>
      <div class = "container">
         <div class = "box box1">One</div>
         <div class = "box box2">two</div>
         <div class = "box box3">three</div>
         <div class = "box box4">four</div>
         <div class = "box box5">five</div>
         <div class = "box box6">six</div>
      </div>
   </body>
</html>

將產生以下結果 −

flex-start

將此值傳遞給 `align-content` 屬性時,所有行都排列在容器的起始位置。

Flex Align Content - Start

以下示例演示了將 `flex-start` 值傳遞給 `align-content` 屬性的結果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .box{
         font-size:25px;
         padding:15px;
         width:40%;
      }
      .container{
         display:flex;
         height:100vh;
         flex-wrap:wrap;
         align-content:flex-start;
      }
   </style>
   
   <body>
      <div class = "container">
         <div class = "box box1">One</div>
         <div class = "box box2">two</div>
         <div class = "box box3">three</div>
         <div class = "box box4">four</div>
         <div class = "box box5">five</div>
         <div class = "box box6">six</div>
      </div>
   </body>
</html>

將產生以下結果 −

flex-end

將此值傳遞給 `align-content` 屬性時,所有行都排列在容器的結束位置。

Flex Align Content - End

以下示例演示了將 `flex-end` 值傳遞給 `align-content` 屬性的結果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .box{
         font-size:25px;
         padding:15px;
         width:40%;
      }
      .container{
         display:flex;
         height:100vh;
         flex-wrap:wrap;
         align-content:flex-end;
      }
   </style>
   
   <body>
      <div class = "container">
         <div class = "box box1">One</div>
         <div class = "box box2">two</div>
         <div class = "box box3">three</div>
         <div class = "box box4">four</div>
         <div class = "box box5">five</div>
         <div class = "box box6">six</div>
      </div>
   </body>
</html>

將產生以下結果 −

stretch

將此值傳遞給 `align-content` 屬性時,行將拉伸以填滿剩餘空間。

Flex Align Content - Stretch

以下示例演示了將 `stretch` 值傳遞給 `align-content` 屬性的結果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .box{
         font-size:25px;
         padding:15px;
         width:40;
      }
      .container{
         display:flex;
         height:100vh;
         flex-wrap:wrap;
         align-content:stretch;
      }
   </style>
   
   <body>
      <div class = "container">
         <div class = "box box1">One</div>
         <div class = "box box2">two</div>
         <div class = "box box3">three</div>
         <div class = "box box4">four</div>
         <div class = "box box5">five</div>
         <div class = "box box6">six</div>
      </div>
   </body>
</html>

將產生以下結果 −

space-around

將此值傳遞給 `align-content` 屬性時,額外空間均勻分佈在各行之間,每行周圍(包括第一行和最後一行)都有相等的間距。

Flex Align Content - Space Around

以下示例演示了將 `space-around` 值傳遞給 `align-content` 屬性的結果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .box{
         font-size:25px;
         padding:15px;
         width:40%;
      }
      .container{
         display:flex;
         height:100vh;
         flex-wrap:wrap;
         align-content:space-around;
      }
   </style>
   
   <body>
      <div class = "container">
         <div class = "box box1">One</div>
         <div class = "box box2">two</div>
         <div class = "box box3">three</div>
         <div class = "box box4">four</div>
         <div class = "box box5">five</div>
         <div class = "box box6">six</div>
      </div>
   </body>
</html>

將產生以下結果 −

space-between

將此值傳遞給 `align-content` 屬性時,額外空間均勻分佈在各行之間,第一行位於容器頂部,最後一行位於容器底部。

Flex Align Content - Space Between

以下示例演示了將 `space-between` 值傳遞給 `align-content` 屬性的結果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .box{
         font-size:25px;
         padding:15px;
         width:40%;
      }
      .container{
         display:flex;
         height:100vh;
         flex-wrap:wrap;
         align-content:space-between;
      }
   </style>
   
   <body>
      <div class = "container">
         <div class = "box box1">One</div>
         <div class = "box box2">two</div>
         <div class = "box box3">three</div>
         <div class = "box box4">four</div>
         <div class = "box box5">five</div>
         <div class = "box box6">six</div>
      </div>
   </body>
</html>

將產生以下結果 −

廣告
© . All rights reserved.