Tillitsdone
down Scroll to discover
CSS Flex-Wrap Mastering Flexbox Layouts
The CSS flex-wrap property controls how flex items are arranged within a container.

It allows items to wrap onto multiple lines, with options like nowrap, wrap, and wrap-reverse.
thumbnail

Introduction

The CSS flex-wrap property is a crucial tool in modern web development, helping developers control how flex items are arranged within a flex container. It determines whether flex items are laid out in a single line or can wrap onto multiple lines. Introduced in September 2015, flex-wrap has become a well-established feature that works seamlessly across many devices and browser versions. It’s part of the CSS Flexible Box Layout Module (Flexbox), which provides a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic.

By mastering the flex-wrap property, developers can create more responsive and adaptable layouts, enhancing the user experience across different screen sizes and devices.

Specification

The flex-wrap property is defined in the CSS Flexible Box Layout Module Level 1 specification. This module outlines the behavior and syntax for the flex-wrap property, ensuring consistency across different browsers and platforms.

The specification can be found at the following link: CSS Flexible Box Layout Module Level 1 - flex-wrap property

This specification provides detailed information on how the flex-wrap property should be implemented and used in web development, making it a valuable resource for developers looking to understand the intricacies of flexbox layouts.

Description

The flex-wrap property in CSS is used to specify whether flex items within a flex container should be forced into a single line or allowed to wrap onto multiple lines. This property is particularly useful for creating responsive and flexible designs where the layout needs to adapt to different screen sizes and content lengths.

When flex-wrap is set to nowrap, all flex items are laid out in a single line. This is the default behavior and can result in the flex container overflowing if the items do not fit within the available space. When set to wrap, flex items can break into multiple lines, adjusting their layout to fit the container’s dimensions better. The wrap-reverse value behaves similarly to wrap but inverts the cross-start and cross-end directions, effectively reversing the order in which the lines are stacked.

By utilizing the flex-wrap property, developers can create more dynamic and adaptable layouts, ensuring that content is always well-presented and easily accessible, regardless of the device or screen size being used. This makes flex-wrap an essential tool in modern web design and development.

Syntax

The syntax for the flex-wrap property is straightforward and consists of a single keyword value. This property can be used to control the wrapping behavior of flex items within a flex container. The syntax is as follows:

flex-wrap: nowrap; /* Default value */
flex-wrap: wrap;
flex-wrap: wrap-reverse;
/* Global values */
flex-wrap: inherit;
flex-wrap: initial;
flex-wrap: revert;
flex-wrap: revert-layer;
flex-wrap: unset;

Explanation of Syntax

  • nowrap: This is the default value. Flex items are laid out in a single line, which may cause the flex container to overflow if the items do not fit within the available space.
  • wrap: Flex items break into multiple lines. The cross-start is equivalent to inline-start or block-start, depending on the writing mode and the flex-direction value.
  • wrap-reverse: Behaves the same as wrap, but the cross-start and cross-end directions are inverted, effectively reversing the order of the lines.

Global Values

  • inherit: Inherits the value from its parent element.
  • initial: Sets the property to its default value, which is nowrap.
  • revert: Reverts the property to the user agent’s default value.
  • revert-layer: Reverts the property to the value of the cascade layer that is one level up.
  • unset: Resets the property to its natural value, which means it behaves as though no value was set for the property.

By understanding and using the proper syntax for the flex-wrap property, developers can effectively control the layout of flex items, ensuring a responsive and adaptable design that caters to various screen sizes and content lengths.

Values

The flex-wrap property in CSS can take several values, each controlling how flex items are arranged within a flex container. These values determine whether items are forced into a single line or allowed to wrap onto multiple lines. Here are the possible values for flex-wrap:

nowrap

  • Description: This is the default value. Flex items are laid out in a single line. If the items do not fit within the available space, the flex container may overflow.
  • Usage: Use nowrap when you want to ensure that all flex items stay on a single line, regardless of the container’s size.

wrap

  • Description: Flex items break into multiple lines. The direction in which lines are stacked depends on the flex-direction property.
  • Usage: Use wrap when you want flex items to adjust their layout dynamically, wrapping onto new lines if they do not fit within the available space. This is particularly useful for creating responsive designs.

wrap-reverse

  • Description: Behaves the same as wrap, but the order of the lines is reversed. The cross-start and cross-end directions are inverted.
  • Usage: Use wrap-reverse when you want to reverse the order in which flex items wrap onto new lines. This can be useful for creating unique layouts where the last items appear at the top.

Global Values

These values are not specific to flex-wrap but can be used with many CSS properties to control their behavior:

  • inherit: Inherits the value from its parent element.
  • initial: Sets the property to its default value, which is nowrap.
  • revert: Reverts the property to the user agent’s default value.
  • revert-layer: Reverts the property to the value of the cascade layer that is one level up.
  • unset: Resets the property to its natural value, which means it behaves as though no value was set for the property.

Summary of Values

  • nowrap: Default value; items stay on a single line.
  • wrap: Items wrap onto multiple lines.
  • wrap-reverse: Items wrap onto multiple lines in reverse order.
  • inherit: Inherits the value from the parent.
  • initial: Sets the property to its default value.
  • revert: Reverts to the user agent’s default.
  • revert-layer: Reverts to the cascade layer’s value.
  • unset: Resets the property to its natural value.

By selecting the appropriate value for the flex-wrap property, developers can fine-tune the layout of flex items, ensuring that their designs are both flexible and responsive to different screen sizes and content requirements.

Examples

Understanding the flex-wrap property becomes clearer with practical examples. Here are some illustrations to demonstrate how flex-wrap can be used to control the layout of flex items within a flex container.

Setting Flex Container Wrap Values

Example: Using flex-wrap: wrap

In this example, the flex-wrap property is set to wrap. This allows the flex items to break into multiple lines if they do not fit within the available space.

<!DOCTYPE html>
<html>
<head>
<title>flex-wrap property</title>
<style>
#main {
width: 400px;
height: 300px;
border: 5px solid black;
display: flex;
flex-wrap: wrap;
}
#main div {
width: 100px;
height: 50px;
}
h1 {
color:#009900;
font-size:42px;
margin-left:50px;
}
h3 {
margin-top:-20px;
margin-left:50px;
}
</style>
</head>
<body>
<h1>Website</h1>
<h3>The flex-wrap:wrap property</h3>
<div id="main">
<div style="background-color:#009900;">1</div>
<div style="background-color:#00cc99;">2</div>
<div style="background-color:#0066ff;">3</div>
<div style="background-color:#66ffff;">4</div>
<div style="background-color:#660066;">5</div>
<div style="background-color:#663300;">6</div>
</div>
</body>
</html>

Output:

flex-wrap property

Example: Using flex-wrap: nowrap

In this example, the flex-wrap property is set to nowrap. This forces all flex items to stay on a single line, even if they overflow the container.

<!DOCTYPE html>
<html>
<head>
<title>flex-wrap property</title>
<style>
#main {
width: 400px;
height: 300px;
border: 5px solid black;
display: flex;
flex-wrap: nowrap;
}
#main div {
width: 100px;
height: 50px;
}
h1 {
color:#009900;
font-size:42px;
margin-left:50px;
}
h3 {
margin-top:-20px;
margin-left:50px;
}
</style>
</head>
<body>
<h1>Website</h1>
<h3>The flex-wrap:nowrap property</h3>
<div id="main">
<div style="background-color:#009900;">1</div>
<div style="background-color:#00cc99;">2</div>
<div style="background-color:#0066ff;">3</div>
<div style="background-color:#66ffff;">4</div>
<div style="background-color:#660066;">5</div>
<div style="background-color:#663300;">6</div>
</div>
</body>
</html>

Output:

flex-wrap property

Example: Using flex-wrap: wrap-reverse

In this example, the flex-wrap property is set to wrap-reverse. This allows the flex items to break into multiple lines but in reverse order.

<!DOCTYPE html>
<html>
<head>
<title>flex-wrap property</title>
<style>
#main {
width: 400px;
height: 300px;
border: 5px solid black;
display: flex;
flex-wrap: wrap-reverse;
}
#main div {
width: 100px;
height: 50px;
}
h1 {
color:#009900;
font-size:42px;
margin-left:50px;
}
h3 {
margin-top:-20px;
margin-left:50px;
}
</style>
</head>
<body>
<h1>Website</h1>
<h3>The flex-wrap:wrap-reverse property</h3>
<div id="main">
<div style="background-color:#009900;">1</div>
<div style="background-color:#00cc99;">2</div>
<div style="background-color:#0066ff;">3</div>
<div style="background-color:#66ffff;">4</div>
<div style="background-color:#660066;">5</div>
<div style="background-color:#663300;">6</div>
</div>
</body>
</html>

Output:

flex wrap property

Example: Using flex-wrap: initial

In this example, the flex-wrap property is set to initial. This resets the property to its default value, which is nowrap.

<!DOCTYPE html>
<html>
<head>
<title>flex-wrap property</title>
<style>
#main {
width: 400px;
height: 300px;
border: 5px solid black;
display: flex;
flex-wrap: initial;
}
#main div {
width: 100px;
height: 50px;
}
h1 {
color:#009900;
font-size:42px;
margin-left:50px;
}
h3 {
margin-top:-20px;
margin-left:50px;
}
</style>
</head>
<body>
<h1>Website</h1>
<h3>The flex-wrap:initial property</h3>
<div id="main">
<div style="background-color:#009900;">1</div>
<div style="background-color:#00cc99;">2</div>
<div style="background-color:#0066ff;">3</div>
<div style="background-color:#66ffff;">4</div>
<div style="background-color:#660066;">5</div>
<div style="background-color:#663300;">6</div>
</div>
</body>
</html>

Output:

flex wrap property

Supported Browsers

The flex-wrap property is supported by the following browsers:

  • Google Chrome 29.0 and above
  • Edge 12 and above
  • Internet Explorer 11.0 and above
  • Firefox 28.0 and above
  • Opera 17.0 and above
  • Safari 9.0 and above

CSS flex-wrap Property – FAQs

What does the flex-wrap property do in CSS?

The flex-wrap property in CSS controls whether the flex container’s items should wrap onto multiple lines if they don’t fit in a single row or column. It determines how items are displayed when they exceed the container’s space.

What values can flex-wrap accept?

  • nowrap: Default value; items stay on a single line.
  • wrap: Items wrap onto multiple lines.
  • wrap-reverse: Items wrap onto multiple lines in reverse order.
  • inherit: Inherits the value from the parent.
  • initial: Sets the property to its default value.
  • revert: Reverts to the user agent’s default.
  • revert-layer: Reverts to the cascade layer’s value.
  • unset: Resets the property to its natural value.

flex-wrap Property Overview

The flex-wrap property in CSS controls how flex items wrap within a flex container. It accepts three values:

  • nowrap: Forces items to stay on one line, even if they overflow.
  • wrap: Allows items to flow onto multiple lines if they don’t fit.
  • wrap-reverse: Wraps items onto multiple lines in reverse order.

How flex-wrap: wrap Affects Layout

When you use flex-wrap: wrap, flex items automatically move to the next line if they don’t fit in the current line. This is great for responsive designs, where content needs to adjust to different screen sizes.

What Happens with flex-wrap: nowrap

With flex-wrap: nowrap, all flex items stay on a single line, which can cause horizontal scrolling or cut-off content if they overflow the container.

Controlling the Order of Wrapped Items

You can control the order of wrapped items using flex-wrap: wrap-reverse. This causes items to wrap in the opposite direction, with the last items at the top and the first items at the bottom.

Example

Here’s a simple example to demonstrate the different flex-wrap values:

HTML

<h4>This is an example for flex-wrap: wrap</h4>
<div class="content">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
<h4>This is an example for flex-wrap: nowrap</h4>
<div class="content1">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
<h4>This is an example for flex-wrap: wrap-reverse</h4>
<div class="content2">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>

CSS

/* Common Styles */
.content, .content1, .content2 {
color: #fff;
font: 100 24px/100px sans-serif;
height: 150px;
width: 897px;
text-align: center;
}
.content div, .content1 div, .content2 div {
height: 50%;
width: 300px;
}
.red {
background: orangered;
}
.green {
background: yellowgreen;
}
.blue {
background: steelblue;
}
/* Flexbox Styles */
.content {
display: flex;
flex-wrap: wrap;
}
.content1 {
display: flex;
flex-wrap: nowrap;
}
.content2 {
display: flex;
flex-wrap: wrap-reverse;
}

Browser Compatibility

The flex-wrap property is widely supported across modern browsers:

  • Google Chrome: Supported since version 29.0 (August 2013).
  • Firefox: Supported since version 28.0 (March 2014).
  • Internet Explorer/Edge: Supported since IE 11.0 (October 2013) and Edge 12.
  • Opera: Supported since version 17.0 (August 2013).
  • Safari: Supported since version 9.0 (September 2015).

Ensuring Cross-Browser Compatibility

To ensure your flexbox layouts work across all browsers:

  1. Use Prefixes: Add vendor prefixes to support older browser versions.
  2. Test Thoroughly: Always test on different browsers and devices.
  3. Fallbacks: Provide fallbacks for older browsers.

Conclusion

The flex-wrap property is a powerful tool for creating responsive layouts. It is widely supported across modern browsers, making it a reliable choice for web design. By understanding and using this property, you can create adaptable and visually appealing designs.

See also

For further learning, check out these resources:

Get in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.