Loading...
Please wait.

Join LetsLearnLights for expert coding and programming guidance. Study popular languages like Java, Python and JavaScript. Improve your skills today

Understand Flex Layout in CSS: A Comprehensive Guide with Flexbox Code Examples and Previews

Description:

The CSS property "display" allows you to control how an element is displayed on a web page. One of the possible values for this property is "flex", which creates a flex container and sets the element as a flex item. Flexbox allows layout elements to be connected and distributed within a container in a flexible and responsive way. This is especially useful for creating layouts that adapt to different screen sizes and orientations. When an element is set to "display: flex", it becomes the primary container for all of its direct children, which are then treated as flex items. These items can be aligned and distributed within the container using various flexbox properties such as "justify-content" and "align-items". Overall, the "flex" value for the "display" property is important for creating flexible and responsive layouts, making it easier to design for different screen sizes and orientations.


Headlines.

  • Flexbox and Flex Display: Understanding the Basics of CSS Flex Properties"
  • "Mastering Flexbox: A Comprehensive Guide to the CSS Flex Display Property"
  • "Flexing Your Design Skills: How to Use the CSS Flex Display Property to Create Responsive Layouts"
  • "Flexing Your Web Design Muscles: A Beginner's Guide to the CSS Flex Display Property"
  • "Flexing Your Design Skills: How to Use the CSS Flex Display Property to Create Dynamic Layouts"

Advertise...

Explain in Details.

"flex" value of css property "display"

The CSS display property "flex" is used to create a flexible container for elements within a web page. It allows elements to be aligned in a row or column, and their size can be adjusted to fit the available space.

Here is an example of using the display flex property to create a row of three elements:

<style>
    .flex-container {
        display: flex;
    }
</style>
<div class="flex-container">
    <div>Element 1</div>
    <div>Element 2</div>
    <div>Element 3</div>
</div>

Element 1
Element 2
Element 3

Other Essentially Properties:

The following properties are used within flex value of display property. These are required to customize flexbox.

flex-direction

The CSS property "flex-direction" is used to determine the direction of elements inside a flex container. The default value is row, which means that the elements will be arranged horizontally in a row.

Here is an example of using flex-direction to create a row of elements:
<style>
    .flex-container2 {
        display: flex;
        flex-direction: row;
    }
    /*
        to make flexed item cleared to view we styled them
        We don't need to described again and again
        This CSS will be used for following all
        element of class "flexed-item".
    */
    .flexed-item {
        border: 1px solid black;
        margin: 5px;
        padding: 5px;
    }
</style>
<div class="flex-container2">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
</div>

Element 1
Element 2
Element 3
If you want to change the direction of the elements so they are arranged vertically in a column, you can set flex-direction to column:
<style>
    .flex-container3 {
        display: flex;
      flex-direction: column;
    }
</style>
<div class="flex-container3">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
</div>

Element 1
Element 2
Element 3
You can also set flex-direction to row-reverse and column-reverse to reverse the order of the elements in the row or column.
<style>
    .flex-container4 {
        display: flex;
    flex-direction: row-reverse;
    }
</style>
<div class="flex-container4">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
</div>

Element 1
Element 2
Element 3
It is important to note that the default value is row, so if you don't specify a value for flex-direction, your elements will be arranged horizontally in a row.
Also, keep in mind that when you change the flex-direction, it can also affect other properties such as justify-content and align-items as they are based on the main and cross axis, which are determined by the flex-direction.

flex-wrap

The CSS property "flex-wrap" determines whether elements inside a flex container will wrap to a new line if they don't fit on one line. It can be set to either "nowrap" (the default) or "wrap".

Here is an example of using flex-wrap to allow elements to wrap to a new line:
<style>
    .flex-container5a {
        display: flex;
        flex-wrap: wrap;
    }
</style>
<div class="flex-container5">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9
In this example, the elements within the container will wrap to a new line if they don't fit on one line. This can be useful when creating responsive designs, as it allows elements to adjust to the available space on different screen sizes.
It's important to note that when using flex-wrap the child elements width or height should be fixed so that they don't overlap each other while wrapping. if the flex-wrap property is not set, the default value is "nowrap". This means that by default, elements within a flex container will not wrap to a new line if they don't fit on one line. This can be important in situations where you want to ensure that elements always stay on one line, such as in a horizontal navigation bar. However, if you want elements to adjust to the available space on different screen sizes, it's important to set flex-wrap to "wrap" to allow elements to wrap to a new line when necessary. Additionally, if you have a large number of elements within a container and they don't wrap to a new line, it can cause the elements to overlap or extend beyond the container, making it important to use flex-wrap to ensure that elements are properly contained and displayed. In summary, using flex-wrap is important for controlling how elements within a flex container adjust to the available space and ensuring that they are properly contained and displayed.

Example with "nowrap"

<style>
    .flex-container5 {
        display: flex;
        flex-wrap: nowrap;
    }
</style>
<div class="flex-container5">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

flex-flow

The CSS property "flex-flow" is a shorthand property that combines the properties "flex-direction" and "flex-wrap" into a single line of code.

Here are the possible values for "flex-flow":
  1. row nowrap (default value)
  2. row wrap
  3. column nowrap
  4. column wrap

justify-content

The "justify-content" property is used to align elements within the container with the main axis. This axis is horizontal for row and vertical for column.

Here are the possible values for justify-content:
flex-start: Elements are aligned to the start of the main axis. This is the default value.
<style>
    .flex-container6 {
        display: flex;
        justify-content: flex-start;
    }
</style>
<div class="flex-container6">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

This example aligns elements left for a row and top for a column.

flex-end: Elements are aligned to the end of the main axis.
<style>
    .flex-container7 {
        display: flex;
        justify-content: flex-end;
    }
</style>
<div class="flex-container7">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

This example aligns elements to the right for a row and bottom for a column.

center: Elements are aligned to the center of the main axis.
<style>
    .flex-container8 {
        display: flex;
        justify-content: center;
    }
</style>
<div class="flex-container8">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

This example centers elements on a central axis.

space-between: Elements are evenly distributed along the main axis, with the first element at the start and the last element at the end.
<style>
    .flex-container9 {
        display: flex;
        justify-content: space-between;
    }
</style>
<div class="flex-container9">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

This example distributes the elements evenly along the central axis, with equal space between each element.

space-around: Elements are evenly distributed along the main axis, with equal space around each element.
<style>
    .flex-containerA {
        display: flex;
        justify-content: space-around;
    }
</style>
<div class="flex-containerA">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

This example distributes the elements evenly along the central axis, with equal space around each element.

It's worth noting that the "space-between", "space-around" and center values of the "justify-content" property are useful when the container has a fixed width and we want the elements to be spread out evenly while using the "flex-wrap" property.

It is also important to note that these values affect the element's positions along the main axis and not the size of the elements.

align-content

The align-content property is used with the display flex property to align elements within a container when there are multiple lines. It works with the cross axis (vertically for a row, horizontally for a column) and affects the space between lines.

The possible values for align-content are:
flex-start: Elements are aligned at the start of the cross axis.
<style>
    .flex-containerB {
        display: flex;
    align-content: flex-start;
    }
</style>
<div class="flex-containerB">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9
Here we are going to use inline CSS to shorten the study as much as possible.

  
<h3>flex-end: Elements are aligned at the end of the cross axis.</h3>
<div style="align-content: flex-end;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>center: Elements are centered along the cross axis.</h3>
<div style="align-content: center;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>space-between: Elements are evenly distributed along the cross axis, with no space at the start or end.</h3>
<div style="align-content: space-between;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>space-around: Elements are evenly distributed along the cross axis, with equal space at the start and end.</h3>
<div style="align-content: space-around;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>stretch: Elements are stretched to fill the available space along the cross axis.</h3>
<div style="align-content: stretch;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

flex-end: Elements are aligned at the end of the cross axis.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

center: Elements are centered along the cross axis.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

space-between: Elements are evenly distributed along the cross axis, with no space at the start or end.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

space-around: Elements are evenly distributed along the cross axis, with equal space at the start and end.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

stretch: Elements are stretched to fill the available space along the cross axis.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9
Note that align-content property is used to align elements within a container when there are multiple lines, and if there is only one line of elements, it will have no effect. In this case, if you want to align the elements within a container when there is only one line, you can use the align-items property. The align-items property works along the cross axis and aligns the elements within a container on the same line. By default, the value is set to stretch which stretches the element to fill the available space along the cross axis.

align-items

The CSS property "align-items" is used to align elements in a flex container along the cross axis (vertically for a row, horizontally for a column).This applies to all elements within the container, and can be overridden by the "self-self" property on individual elements.

Here are the possible values for align-items and their explanations:
<h3>flex-start: Elements are aligned to the start of the cross axis (top for a row, left for a column).</h3>
<div style="align-items: flex-start;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>flex-end: Elements are aligned to the end of the cross axis (bottom for a row, right for a column).</h3>
<div style="align-items: flex-end;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>center: Elements are centered along the cross axis.</h3>
<div style="align-items: center;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>baseline: Elements are aligned to their baselines.</h3>
<div style="align-items: baseline;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>
<h3>stretch: Elements are stretched to fill the entire cross axis (default value).</h3>
<div style="align-items: stretch;" class="flex-container">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

flex-start: Elements are aligned to the start of the cross axis (top for a row, left for a column).

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

flex-end: Elements are aligned to the end of the cross axis (bottom for a row, right for a column).

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

center: Elements are centered along the cross axis.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

baseline: Elements are aligned to their baselines.

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

stretch: Elements are stretched to fill the entire cross axis (default value).

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9
Note that align-items only affects the elements within the container when the container has at least two lines of items or when the content's cross size is larger than the container's cross size. When the container has only one line of items or when the content's cross size is less than or equal to the container's cross size, align-items does not have any effect. This means that if you want to align all the elements in the container in one line, you should use the align-self property instead of align-items to align the elements.

align-self

This "align-self" is used to align individual elements within a flex container. This is similar to the align-items property, but allows certain elements to be aligned differently from the rest of the elements within the container.

Here is an example of using align-self to align a specific element within a flex container:
<style>
    .flex-containerZ {
        display: flex;
        align-items: center;
    }
</style>
<div class="flex-containerZ">
    <div class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div style="align-self: flex-start;" class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div style="align-self: flex-start;" class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9

In this example, the "align-items property is set to center, so all elements inside the container will be centered vertically. However, in the special-element class, align-self is set to flex-start, so it will be attached to the top of the container instead of the center.

The possible values for align-self are:
  1. auto: This is the default value and it means the element will use the align-items value of the container.
  2. flex-start: The element is aligned at the top (or left for a column) of the container.
  3. flex-end: The element is aligned at the bottom (or right for a column) of the container.
  4. center: The element is aligned in the middle of the container.
  5. baseline: The element is aligned along the baseline of the text.
  6. stretch: The element is stretched to fill the entire height (or width for a column) of the container.
It is important to note that the align-self property only works on elements that are direct children of the flex container and the elements must be flex items.

order

This property "order" is used to specify the order of elements within a flex container. This allows elements to be visually rearranged without changing the HTML code. The default value for order is 0. Possible values for order are integers, positive or negative. A higher value means that an element will appear later in the visual sequence than an element with a lower value.

Here is an example of using the order property to rearrange elements within a flex container:
<div class="flex-container">
    <div style="order: 4;" class="flexed-item">Element 1</div>
    <div class="flexed-item">Element 2</div>
    <div style="order: 5;" class="flexed-item">Element 3</div>
    <div class="flexed-item">Element 4</div>
    <div class="flexed-item">Element 5</div>
    <div class="flexed-item">Element 6</div>
    <div style="order: 3;" class="flexed-item">Element 7</div>
    <div class="flexed-item">Element 8</div>
    <div style="order: 1;" class="flexed-item">Element 9</div>
</div>

Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9
In above example elements normal order is changed due to "order" property.

Note that when elements have the same order value, the visual order will be determined by their position in the HTML.
Also it can be used with negative numbers as well. When some elements have negative numbers, these elements will appear earlier in the visual sequence than elements with positive numbers.

The CSS property "order" is supported by all modern web browsers, including Internet Explorer 11 and above. It is part of the Flexible Box Layout Module, which is widely supported across all major web browsers. However, it is important to note that some older versions of web browsers may not fully support all the features of the Flexible Box Layout Module.

Final Lines.

We have almost discuss complete "flex" (flexbox) value of display property. Above you may see many results same of different codes it is because above Properties and values are used individually for only study purpose. A complete flexbox achieved from flex container conjected with flex-direction, flex-wrap, justify-content, align-items, align-content, order and align-self. The "grid" value of the "display" property will be explained in the next tutorial since this post is already quite long.
In conclusion, the CSS display property's "flex" value allows for flexible and efficient layout of elements within a container. It allows for the alignment, direction, and wrapping of elements within a container, making it a powerful tool for creating responsive and dynamic web pages. Additionally, the use of flexbox can simplify the CSS code needed for layout, making it easier to maintain and update. Overall, the display: flex property is a valuable tool for any web developer looking to create modern and responsive web designs.
Posted by Mnzr chauhan
««« Thanks again for reading our blog. If you have any questions, feel free to ask. Your support is enough for us to share this post with your friends. And a good comment for our hard work. »»»
Thank you ツ

Post a Comment

0 Comments