Flexbox教程

Flexbox 对齐内容

通常您可以观察到如下所示排列弹性项目后容器中剩余的额外空间。
使用属性 justify-content,您可以通过按预期分配额外空间来沿主轴对齐内容。您还可以调整 flexitems 的对齐方式,以防它们溢出行。
使用-
justify-content: flex-start | flex-end | center | space-between | space-around| space-evenly;
flex-start-弹性项目放置在容器的开头。 flex-end-弹性项目放置在容器的末尾。 center-弹性项目放置在容器的中心,额外的空间在弹性项目的开始和结束处平均分布。 space-between-额外的空间在弹性项目之间平均分配。 space-around-额外空间在弹性项目之间平均分配,使得容器边缘与其内容之间的空间是弹性项目之间空间的一半。
现在,我们将通过示例了解如何使用 justify-content 属性。

flex-start

将此值传递给属性 justify-content 时,弹性项目将放置在容器的开头。
Justify 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 时,弹性项目被放置在容器的末尾。
Justify 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>

中心

将此值传递给属性 justify-content 时,弹性项目被放置在容器的中心,其中额外的空间在开始和结束时平均分配弹性项目。
Justify 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>

间距

将此值传递给属性 justify-content 时,额外的空间在弹性项目之间平均分配,这样任何两个弹性项目之间的空间都是相同的,并且开始和结束弹性项目接触容器的边缘。
Justify Flex Space between
以下示例演示了将值 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>

空间

将此值传递给属性 justify-content 时,额外的空间在弹性项目之间平均分配,这样任何两个弹性项目之间的空间都是相同的。然而,容器边缘与其内容(弹性项目的开始和结束)之间的空间是弹性项目之间空间的一半。
Justify Flex Space around
以下示例演示了将值 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>

空间均匀

将此值传递给属性 justify-content 时,额外的空间在弹性项目之间平均分配,这样任何两个弹性项目之间的空间都是相同的(包括到边缘)。
Justify Flex Space Evenly
以下示例演示了将值 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>
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4