Flexbox - 自对齐


此属性类似于align-items,但在这里,它应用于单个弹性项目。

用法-

align-self: auto | flex-start | flex-end | center | baseline | stretch;

该属性接受以下值 -

  • flex-start - Flex 项目将在容器的顶部垂直对齐。

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

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

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

  • Baseline - 弹性项目将在横轴的基线处对齐。

弹性启动

将此值传递给属性align-self后,特定的弹性项目将在容器的顶部垂直对齐。

Flex Align 自启动

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

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta; align-self:start;}
      .box5{background:yellow;}
      .box6{background:pink;}
      .box{
         font-size:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         border:3px solid black;
         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>

它将产生以下结果 -

柔性端

将此值传递给属性align-self时,特定的弹性项目将在容器的底部垂直对齐。

Flex 对齐自端

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

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta; align-self:flex-end;}
      .box5{background:yellow;}
      .box6{background:pink;}
      .box{
         font-size:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         border:3px solid black;
         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>

它将产生以下结果 -

中心

将值center传递给属性align-self时,特定的弹性项目将在容器的中心垂直对齐。

Flex 对齐自居中

以下示例演示了将值中心传递给align-self属性的结果。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta; align-self:center;}
      .box5{background:yellow;}
      .box6{background:pink;}
      .box{
         font-size:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         border:3px solid black;
         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>

它将产生以下结果 -

拉紧

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

Flex Align 自拉伸

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

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta; align-self:stretch;}
      .box5{background:yellow;}
      .box6{background:pink;}
      .box{
         font-size:35px;
         padding:15px;
      }
      .container{
         display:flex;
         height:100vh;
         border:3px solid black;
         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>

它将产生以下结果 -