Flexbox - 对齐项目



align-items 属性与 justify-content 属性类似。但在这里,项目是在交叉轴线(垂直方向)上对齐的。

用法 -

align-items: flex-start | flex-end | center | baseline | stretch;

此属性接受以下值 -

  • flex-start - 弹性项目在容器顶部垂直对齐。

  • flex-end - 弹性项目在容器底部垂直对齐。

  • flex-center - 弹性项目在容器中心垂直对齐。

  • stretch - 弹性项目垂直对齐,以填充容器的整个垂直空间。

  • baseline - 弹性项目对齐,使它们的文本基线沿水平线对齐。

flex-start

将此值传递给 align-items 属性时,弹性项目将在容器顶部垂直对齐。

Align Start

以下示例演示了将 flex-start 值传递给 align-items 属性的结果。

<!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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         align-items: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-items 属性时,弹性项目将在容器底部垂直对齐。

Align End

以下示例演示了将 flex-end 值传递给 align-items 属性的结果。

<!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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         align-items: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>

它将产生以下结果 -

center

将此值传递给 align-items 属性时,弹性项目将在容器中心垂直对齐。

Align Center

以下示例演示了将 flex-center 值传递给 align-items 属性的结果。

<!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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         align-items: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>

它将产生以下结果 -

stretch

将此值传递给 align-items 属性时,弹性项目将垂直对齐,以填充容器的整个垂直空间。

Align Stretch

以下示例演示了将 stretch 值传递给 align-items 属性的结果。

<!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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         align-items: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>

它将产生以下结果 -

baseline

将此值传递给 align-items 属性时,弹性项目将对齐,使它们的文本基线沿水平线对齐。

以下示例演示了将 baseline 值传递给 align-items 属性的结果。

<!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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         align-items:baseline;
      }
   </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>

它将产生以下结果 -

广告