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.
<style>.flex-container {display: flex;}</style><div class="flex-container"><div>Element 1</div><div>Element 2</div><div>Element 3</div></div>
Other Essentially Properties:
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.
<style>.flex-container2 {display: flex;flex-direction: row;}/*to make flexed item cleared to view we styled themWe don't need to described again and againThis CSS will be used for following allelement 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>
<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>
<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>
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".
<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>
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>
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.
- row nowrap (default value)
- row wrap
- column nowrap
- 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.
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>
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>
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>
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>
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>
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.
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.
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>
<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.
center: Elements are centered along the cross axis.
space-between: Elements are evenly distributed along the cross axis, with no space at the start or end.
space-around: Elements are evenly distributed along the cross axis, with equal space at the start and end.
stretch: Elements are stretched 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.
<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).
flex-end: Elements are aligned to the end of the cross axis (bottom for a row, right for a column).
center: Elements are centered along the cross axis.
baseline: Elements are aligned to their baselines.
stretch: Elements are stretched to fill the entire cross axis (default value).
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.
<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>
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.
- auto: This is the default value and it means the element will use the align-items value of the container.
- flex-start: The element is aligned at the top (or left for a column) of the container.
- flex-end: The element is aligned at the bottom (or right for a column) of the container.
- center: The element is aligned in the middle of the container.
- baseline: The element is aligned along the baseline of the text.
- stretch: The element is stretched to fill the entire height (or width for a column) of the container.
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.
<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>
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.
0 Comments