Understanding the Flex Layout Model in CSS3


CSS3 provides a layout mode Flexible Box, commonly called as Flexbox. Flexbox (flexible box) is a layout mode of CSS3. Using this mode, you can easily create layouts for complex applications and web pages. It includes the container, flex items, etc. The container has the following properties −

  • flex-direction − Set the direction of the flex items

  • flex-wrap − Set whether the flex items should wrap or not

  • flex-flow − It is a shorthand property for flex-wrap and flex-direction

  • justify-content − Align the flex items

  • align-items − Vertically align the flex items

  • align-content − Align flex lines

Set the parent container

First, set a div container and within that three child divs: container1, container2, and container3 −

<div class="container">
   <div class="container1">
      <div>1</div>
      <div>2</div>
      <div>3</div>
   </div>
   <div class="container2">
      <div>1</div>
      <div>2</div>
      <div>3</div>
   </div>
   <div class="container3">
      <div>1</div>
      <div>2</div>
      <div>3</div>
   </div>
</div>

Set the container as flexible

The parent container as flexible using the display property with the value flex −

.container {
   display: flex;
   flex-wrap: wrap;
   justify-content: space-evenly;
}

Place the first child container on the top

To set the child container on the top, use the align-self property and set it to flex-start −

.container1 {
   align-self: flex-start;
   display: flex;
   background-color: rgb(71, 30, 255);
   width: 200px;
   margin: 20px;
}

Place the second child container on the top

The 2nd child container is also set on the top using the align-self property with the value flex-start. The justify-content property with the center value aligns the flex items at the center −

.container2 {
   display: flex;
   background-color: rgb(14, 126, 79);
   width: 200px;
   justify-content: center;
   align-self: flex-start;
   margin: 20px;
}

Vertical placement of the third child container

The flex-direction property is set with the value column to display the flex item vertically. The flex items are aligned in the middle of the container using the align-items property with the value center −

.container3 {
   display: flex;
   flex-direction: column;
   background-color: rgb(168, 60, 10);
   width: 200px;
   align-items: center;
   margin: 20px;
}

Example

The following is the code depicting the flex layout model in CSS3 −

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      .container {
         display: flex;
         flex-wrap: wrap;
         justify-content: space-evenly;
      }
      .container1 {
         align-self: flex-start;
         display: flex;
         background-color: rgb(71, 30, 255);
         width: 200px;
         margin: 20px;
      }
      .container1 > div {
         background-color: #f1f1f1;
         margin: 10px;
         padding: 10px;
         font-size: 30px;
      }
      .container2 {
         display: flex;
         background-color: rgb(14, 126, 79);
         width: 200px;
         justify-content: center;
         align-self: flex-start;
         margin: 20px;
      }
      .container2 > div {
         background-color: #f1f1f1;
         margin: 10px;
         padding: 10px;
         font-size: 30px;
      }
      .container3 {
         display: flex;
         flex-direction: column;
         background-color: rgb(168, 60, 10);
         width: 200px;
         align-items: center;
         margin: 20px;
      }
      .container3 > div {
         background-color: #f1f1f1;
         margin: 10px;
         padding: 10px;
         width: 20px;
         font-size: 30px;
      }
   </style>
</head>
<body>
   <h1>Flex layout example</h1>
   <div class="container">
      <div class="container1">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
      <div class="container2">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
      <div class="container3">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
   </div>
</body>
</html>

Updated on: 02-Jan-2024

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements