目录

Flexbox - Flex容器( Flex Containers)

要在应用程序中使用Flexbox,您需要使用display属性创建/定义Flex容器。

Usage -

display: flex | inline-flex

此属性接受两个值

  • flex - 生成块级弹性容器。

  • inline-flex - 生成内联flex容器框。

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

Flex

将此值传递给display属性时,将创建块级Flex容器。 它占用父容器(浏览器)的整个宽度。

以下示例演示如何创建块级Flex容器。 在这里,我们创建了六个不同颜色的盒子,我们使用了flex容器来容纳它们。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      .container{
         display:flex;
      }
      .box{
         font-size:35px;
         padding:15px;
      }
   </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赋予display属性,因此容器使用容器的宽度(浏览器)。

您可以通过向容器添加边框来观察此情况,如下所示。

.container {
   display:inline-flex;
   border:3px solid black;
}

它会产生以下结果 -

新页面打开

内联flex

将此值传递给display属性时,将创建一个内联级别的flex容器。 它只需要内容所需的位置。

以下示例演示如何创建内联Flex容器。 在这里,我们创建了六个具有不同颜色的盒子,我们使用了内联 - 柔性容器来容纳它们。

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      .container{
         display:inline-flex;
         border:3px solid black;
      }
      .box{
         font-size:35px;
         padding:15px;
      }
   </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容器,因此它只占用了包装其元素所需的空间。

↑回到顶部↑
WIKI教程 @2018