当前位置: 首页 > 文档资料 > Flexbox 入门教程 >

证明内容(Justifying Contents)

优质
小牛编辑
133浏览
2023-12-01

通常,您可以在排列弹性项目后观察容器中剩余的额外空间,如下所示。

使用属性justify-content ,您可以通过按预期分配额外空间来沿主轴对齐内容。 您还可以调整 flexitems 的对齐方式,以防它们溢出线条。

usage -

justify-content: flex-start | flex-end | center | space-between | space-around| space-evenly;

此属性接受以下值 -

  • flex-start - flex-items放置在容器的开头。

  • flex-end - flex-items放置在容器的末尾。

  • center - flex-items放置在容器的中心,其中额外的空间在flex-items的开始和结束处均匀分布。

  • space-between - 额外的空间在flex-items之间平均分配。

  • space-around - 额外的空间在柔性物品之间均匀分布,使得容器边缘与其内容物之间的空间是柔性物品之间的空间的一半。

现在,我们将看到如何使用justify-content属性和示例。

flex-start

将此值传递给属性justify-content ,flex-items 将放置在容器的开头。

证明Flex Start

以下示例演示将值flex-start传递给justify-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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         border:3px solid black;
         justify-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

将此值传递给属性justify-content ,flex-items将放置在容器的末尾。

证明Flex End

以下示例演示将值flex-end传递给justify-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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         border:3px solid black;
         justify-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>

center

将此值传递给属性justify-content ,flex-items将放置在容器的中心,其中额外空间在flex-items的开始和结尾处均匀分布。

证明Flex Center

以下示例演示将值center传递给justify-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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         border:3px solid black;
         justify-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>

space-between

在将此值传递给属性justify-content ,额外空间在flex项之间均匀分布,使得任意两个flex项之间的空间相同,并且flex-items的开始和结束触及容器的边缘。

证明之间的Flex空间

以下示例演示了将值space-between传递给justify-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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         border:3px solid black;
         justify-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>

space-around

在将此值传递给属性justify-content ,额外空间在flex-items之间平均分配,使得任何两个flex-items之间的空间相同。 但是,容器边缘与其内容之间的空间(弹性项目的开始和结束)是弹性项目之间空间的一半。

证明Flex Space周围

以下示例演示了将值space-around传递给justify-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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         border:3px solid black;
         justify-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-evenly

在将此值传递给属性justify-content ,额外空间在flex-items之间均匀分布,使得任意两个flex-items之间的空间相同(包括边缘的空间)。

均匀地证明Flex空间

以下示例演示了将值space-evenly传递给justify-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:35px;
         padding:15px;
      }
      .container{
         display:flex;
         border:3px solid black;
         justify-content:space-evenly;
      }
   </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>