The Wayback Machine - https://web.archive.org/web/20240909185225/https://www.geeksforgeeks.org/css-flex-wrap-property/
Open In App

CSS flex-wrap property

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS flex-wrap property is used to specify whether flex items are forced into a single line or wrapped onto multiple lines. The flex-wrap property allows enabling the control direction in which lines are stacked. It is used to designate a single line or multi-line format to flex items inside the flex container.

Syntax:  

flex-wrap: nowrap | wrap | wrap-reverse | initial;

Default Value: nowrap

1. Using CSS flex-wrap: wrap Property

wrap: This property is used to break the flex item into multiples lines. It makes flex items wrap to multiple lines according to flex item width. 

Syntax: 

flex-wrap: wrap;

Example:  In this example, we are using the CSS flex wrap property.

html
<!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>GeeksforGeeks</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

2. Using CSS flex-wrap: nowrap Property

nowrap: The default value of wrap-flex is nowrap. It is used to specify that the item has no wrap. It makes item wrap in single lines. 

Syntax: 

flex-wrap: nowrap;

Example:  In this example, we are using the CSS flex wrap property.

html
<!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>GeeksforGeeks</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

3. Using CSS flex-wrap: wrap-reverse Property

wrap-reverse: This property is used to reverse the flow of the flex items when they wrap to new lines. 

Syntax: 

flex-wrap: wrap-reverse;

Example:  In this example, we are using the CSS flex wrap property.

html
<!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>GeeksforGeeks</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


4. Using CSS flex-wrap: initial Property

initial: This property is used to set it as default value. 

Syntax: 

flex-wrap: initial;

Example: In this example, we are using the CSS flex wrap property.

html
<!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>GeeksforGeeks</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 browsers supported by CSS flex-wrap property are listed below: 

  • 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?

The flex-wrap property accepts three values: nowrap, wrap, and wrap-reverse. “Nowrap” forces items to stay on one line, “wrap” allows items to flow onto multiple lines, and “wrap-reverse” wraps items onto multiple lines in reverse order.

How does flex-wrap: wrap affect the layout?

When using flex-wrap: wrap, the flex items will automatically move to the next line if they don’t fit in the current line. This is especially useful for responsive layouts, where content should adjust fluidly to varying screen sizes.

What happens with flex-wrap: nowrap?

With flex-wrap: nowrap, all flex items are forced to stay on a single line, even if they overflow the container. This can lead to horizontal scrolling or content being cut off.

Can I control the order of wrapped items?

Yes, using flex-wrap: wrap-reverse, you can control the order of wrapped items, causing them to wrap in the opposite direction, placing the last items at the top and the first items at the bottom.



Previous Article
Next Article

Similar Reads

Tailwind CSS Flex Wrap
The CSS flexbox is a vital feature to develop the frontend, there are three wraps available in CSS so in tailwind CSS all the properties are covered as in class form. It is the alternative of CSS flex-wrap Property for fast development of front-end. Note: To activate the flex-wrap you have to include the flex class in your element before the flex-w
2 min read
What is the difference between display:inline-flex and display:flex in CSS ?
In this article, we will see the display property &amp; its values, i.e. the inline-flex &amp; flex, in order to specify the display behavior of an element, along with understanding their basic differences &amp; will illustrate them through the examples. Both CSS display: inline-flex and display: flex properties are used to create flexible layouts.
3 min read
Bulma Flex Wrap
In this article, we will be learning about flex-wrap property in Bulma. In Bulma, the flex-wrap property is used as a helper. The flex-wrap property in Bulma allows the user to wrap the flex items either into a single line or wrapped onto multiple lines in a container. There are three classes available in Bulma that are used to wrap the flex items.
3 min read
Bootstrap 5 Flex Wrap
Bootstrap 5's flex-wrap utility enables flexible layout control, allowing items to wrap as needed within a flex container. It offers options like nowrap, wrap, and wrap-reverse for responsive design. Bootstrap 5 Flex Wrap Classes: CSS ClassDescriptionflex-wrapChanges how flex items wrap in a flex container.flex-wrap-reversePerforms reverse wrapping
3 min read
Bulma Flex grow and flex shrink
Bulma is an Open source CSS framework developed by Jeremy Thomas. This framework is based on the CSS Flexbox property. It is highly responsive, minimizing the use of media queries for responsive behavior. In this article, we will learn about Bulma flex-grow and flex-shrink properties. The Bulma framework allows the users to use CSS flex-shrink and
2 min read
Bootstrap 5 Flex Enable Flex Behaviors
Bootstrap 5 Enable Flex Behaviors are used to apply display utilities to create a flexbox container and transform direct children elements into flex items. With more flex properties, flex items and containers can be further modified. Utility classes: .d-flex: It displays an element as a flex container..d-inline-flex: It displays an element as an in
2 min read
How to Center a Flex Container but Left-Align Flex Items?
When we use CSS Flexbox, we might encounter situations where we want to center a flex container within its parent while keeping the flex items inside it left-aligned. This interface is very common in web design to create balanced and visually appealing interfaces. Below are different approaches to center a flex container but left-align flex items:
4 min read
CSS | overflow-wrap Property
The overflow-wrap property in CSS is used to specify that the browser may break lines of text inside any targeted element to prevent overflow when the original string is too long to fit. This property was earlier known as word-wrap that still supported by some browsers but it was renamed to overflow-wrap in a CSS3 draft. Syntax: .box{ overflow-wrap
1 min read
Explain the use of the word-wrap Property in CSS
The word-wrap property in CSS is used to control how long words should behave when they exceed the width of their container. It is especially useful for preventing long words or strings of text from breaking the layout by overflowing their container. Syntax:The word-wrap property can take two values: normal: The default value, where long words may
1 min read
CSS word-wrap Property
The word-wrap property in CSS is used to manage the breaking and wrapping of long words within an element, ensuring that the content fits within its container. This property is essential for maintaining readability and layout integrity when dealing with lengthy words or strings without spaces. Syntaxword-wrap: normal|break-word|initial|inherit;Prop
2 min read
CSS flex Property
The CSS flex property allows setting the length of flexible items. This shorthand property combines the flex-grow, flex-shrink, and flex-basis properties, making it more responsive and mobile-friendly. It simplifies the positioning of child elements within the main container, ensuring margins do not collapse with content margins. Additionally, it a
3 min read
CSS flex-basis Property
The flex-basis property in CSS is used to specify the initial size of the flexible item. The flex property is not used if the element is not flexible item. Syntax: flex-basis: number | auto | initial | inherit;Default Value: auto Property Values: Property Value Description number It is a length unit that define the initial length of that item. auto
3 min read
CSS flex-direction Property
The flex-direction property is a sub-property of the flexible box layout module. It established the main axis of the flexible item. the main axis of the flex item is the primary axis. It's not necessarily horizontal all the time it depends on the flex-direction property. Note: The flex property is useless when the element is not a flexible item. Sy
4 min read
CSS flex-flow Property
The flex-flow property is a sub-property of the flexible box layout module and also a shorthand property for flex-wrap and flex-direction. Note: The flex property is useless when the element is not a flexible item. Syntax:flex-flow: flex-direction flex-wrap;Values of flex-flow property:row nowrap: It arranges the row the same as the text direction
8 min read
CSS flex-shrink Property
The CSS flex-shrink property specifies how the item will shrink relative to the remaining flexible items inside the same container. It is essential for responsive design, allowing elements to reduce their size based on available space. This property does not affect non-flexible items. Note: If the item in the container is not flexible then the flex
3 min read
CSS flex-grow Property
The CSS flex-grow property specifies how much the item will grow relative to the rest of the flexible items inside the same container. It is essential for responsive design, allowing elements to expand based on available space, and ensuring a dynamic and adaptable layout. Note: If the item in the container is not flexible then the flex-grow propert
3 min read
What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. Difference between the "word-break: break-all;" and "word-wrap: break-word;" word-break: break-all; It is used to break the words at any ch
2 min read
How to wrap an element with more content in flexbox container to have the same width as other elements in CSS ?
In a flexbox container, we might have a scenario where you want to wrap an element with more content, such as text or an image, to have the same width as other elements. In this article, we will see how to wrap an element with more content in a flexbox container to have the same width as other elements using CSS. The flexbox or flexible box model i
3 min read
How to wrap the text around an image using HTML and CSS ?
Wrapping the text around an image is quite attractive for any kind of website. By using HTML and CSS wrapping an image with the text is possible and there are many ways to do so because the shape of any image is not constant. In HTML : You can also use CSS shape-outside Property depending on the shape of your image.Align images to the right, left,
4 min read
How to wrap text inside and outside box using CSS ?
In CSS, text wrapping inside and outside a box is managed using the word-wrap, word-break, and overflow-wrap properties. These properties control how text breaks and wraps within an element, ensuring it fits within the container while maintaining readability and design aesthetics. Approach: Using the overflow-wrap propertyWe will be using the "over
2 min read
How to wrap a text in a &lt;pre&gt; tag using CSS ?
In this article, we will learn how to wrap a text in a &lt;pre&gt; tag. The &lt;pre&gt; tag is used to display the block of preformatted text which preserves the text spaces, line breaks, tabs, and other formatting characters which are ignored by web browsers. By default, the &lt;pre&gt; tag does not support the &lt;pre&gt; tag. In the case of rend
2 min read
How to wrap table cell &lt;td&gt; content using CSS ?
Tables in HTML are absurd elements. It is not an easy task to apply CSS properties to a table element. Being a highly structured element, CSS properties are sometimes lost in the hierarchy down the structure. It is also very likely that the contents of the table might change the structure or dimensions of the table. For example, long words residing
4 min read
How to Specify an Element After which to Wrap in CSS Flexbox?
Flexbox is a powerful CSS layout tool that helps create flexible and responsive designs easily. One of its features is the ability to control the wrapping of items within a flex container. In this article, we are going to learn how to specify an element after which the wrapping should occur in a CSS Flexbox layout. When using Flexbox, items are typ
3 min read
HTML | DOM Textarea wrap Property
The DOM Textarea wrap Property is used to set or return the value of the wrap attribute of the textarea. It describes the way the text to be wrapped while submitting a form. Syntax: It is used to return the wrap property.textareaObject.wrapIt is used to set the wrap property.textareaObject.wrap = soft|hard Property Values: soft: It defines that the
2 min read
Primer CSS Flexbox Flex
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Flex classes are used to define the ability of a flex item to alter
3 min read
Primer CSS Flexbox Flex Direction
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox flex-direction is used to set the direction of the flex items in the
2 min read
Primer CSS Flexbox Flex Container
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Flex Container is used to make an element layout it's content using
2 min read
What is the difference between inline-flex and inline-block in CSS?
The display property specifies how an element should be displayed on a webpage. There can be many values, related to this property in CSS. Inline-block and inline-flex are two such properties. Although there are several values that this property can have, to understand the aforementioned, let us first look into three other values: inline, block, an
3 min read
CSS | Ordering Flex Items
The order property of CSS can be used for ordering flex items. It specifies the order of a flex item with respect to the other flex items. The element has to be a flexible item for the order property to work. The elements are displayed in ascending order of their order values. If two elements have the same order value then they are displayed on the
2 min read
Tailwind CSS Flex
The CSS flexbox is a vital feature to develop the frontend, there is four flex available in tailwind CSS all the properties are covered as in class form. It is the alternative of CSS flex Property for fast development of front-end. It is used to set the length of flexible items. The flex class is much responsive and mobile-friendly. It is easy to p
5 min read