Enable Wrapping of Flex Items using CSS3


To enable wrapping of Flex Items using CSS3, the flex-wrap property is used. Set the value wrap to enable wrapping.

Enable Wrapping of Flex Items

In this example, we enable wrapping of flex items using the flex-wrap: wrap. The following is our flex container −

<div class="container">
   <div class="first">First Div</div>
   <div class="second">Second Div</div>
   <div class="third">Third Div</div>
</div>

We have styled the flex container like the following. The flex-wrap is set to wrap the flex items −

.container {
   height: 300px;
   display: flex;
   width: 300px;
   border: 2px solid red;
   flex-wrap: wrap;
}

Example

The following is the code for enabling wrapping of flex items using CSS3 −

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      .container {
         height: 300px;
         display: flex;
         width: 300px;
         border: 2px solid red;
         flex-wrap: wrap;
      }
      div {
         width: 150px;
         height: 100px;
         color: white;
         text-align: center;
         font-size: 20px;
      }
      .first {
         background-color: rgb(55, 0, 255);
      }
      .second {
         background-color: red;
      }
      .third {
         background-color: rgb(140, 0, 255);
      }
   </style>
</head>
<body>
   <h1>Flex wrap example</h1>
   <div class="container">
      <div class="first">First Div</div>
      <div class="second">Second Div</div>
      <div class="third">Third Div</div>
   </div>
</body>
</html>

Wrap Flex Items and set Equal Space Around Items

In this example, we will wrap flex items using the flex-wrap −

flex-wrap: wrap;

To set equal space around items, we have used the justify-content property −

justify-content: space-evenly;

Example

Let us see the example −

<!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;
         background-color: lightblue;
      }
      .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 class="container1">
         <div>1</div>
         <div>2</div>
         <div>3</div>
      </div>
   </div>
</body>
</html>

Updated on: 02-Nov-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements