Flexbox
Flexbox is a CSS mechanism that allows you to control the size, order, and alignment of items along several axes, the distribution of free space between items, and much more.
In order to enable the flexbox, you need to assign the display: flex; property to the item. Then:
- The item containing display: flex; is transformed into a flex container.
- The immediate children of this item will be transformed into flex items, and they will start to be arranged according to the new rules.
Main and Cross Axes
Axes represent one of the basic concepts in Flexbox.
When a document flows normally, blocks and text are arranged from left to right and from top to bottom.
Under the traditional block model, the directions “left”, “right”, “top”, and “bottom” cannot be changed. But within a flex container you can modify these concepts, since you can change the typical direction of the flow.
-
The main axis of the flex container is the direction along which all of its child items are arranged. The flex items flow along the main axis from the start to the end of it.
- We call the direction that is perpendicular to the main axis the cross axis. The “vertical” alignments are made along this axis.
By default, the main axis flows from left to right, but it can be rotated in all directions using the flex-direction property, which is assigned for the flex container. Value properties:
- The following are the default values: row – The main axis flows from left to right.
- column — The main axis flows from top to bottom.
- row-reverse — The main axis flows from right to left.
- column-reverse — The main axis flows from bottom to top.
The cross axis is always perpendicular to the main axis, and when the latter is rotated, the former rotates too to stay perpendicular:
- If the main axis flows horizontally, then the cross axis will flow vertically down.
- If the main axis flows vertically, then the cross axis will flow to the right.
Thus, the cross axis will never flow up or to the left, and there is no property that rotates the cross axis.
Arrangement of Flex Items
Main axis alignment
The justify-content CSS property determines how items are aligned along the main axis. Available values for justify-content:
- The following are the default values: flex-start – Items are arranged at the start of the main axis.
- flex-end — Items are arranged at the end of the main axis.
- center — Items are arranged in the center of the main axis.
- space-between — Items are arranged so that the distances between adjacent ones are uniform, and there are no margins between the items and the edges of the flex container.
- space-around — Items are arranged in such a way that the distances between neighboring items are uniform, and there is a margin between the items and the edges of the flex container that is equal to half of the distance between neighboring items.
Alignment along the cross axis
The align-items CSS property determines how items are aligned along the cross axis. Available values for align-items:
- Default value of stretch – Items are stretched to fill the entire “height” of the flex container.
- flex-start — Items are arranged at the start of the cross axis.
- flex-end — Items are arranged at the end of the cross axis.
- center — Items are arranged in the center of the cross axis.
- baseline — Items are aligned along the baseline of the text inside them.
The arrangement of items along the main axis is assigned for the entire flex container, and the arrangement properties apply to all flex items equally. You are not allowed to assign an arrangement to one particular item along the main axis that differs from the one assigned to the other items.
The cross alignment can be assigned to each item separately. To do this, you can use the align-self property, which is assigned for the flex items themselves and not for the flex container. The align-self property has the very same values as align-items.
Flex Item Overflow
If you add more flex items to a container than can fit in a single row, then
- they will be shrunk to the smallest possible width.
- Even if you assign a width to them, the flexbox mechanism can override this setting and shrink it.
- If they can no longer be fit in a container after it is shrunk, they will overflow its boundaries, but they will continue to be placed in a single row.
To avoid this, you need to use the flex-wrap flex container property. It has two values:
- The default value nowrap — Flex items cannot overflow onto a new line.
- wrap — The overflow of flex items to a new line is allowed. Rows of items are arranged along the cross axis, where the first row is at the start of the cross axis and the last is at the end.
- wrap-reverse — The overflow of flex items onto a new line is also allowed. Rows of items are arranged in reverse order: the first row is placed at the end of the cross axis, and the last is placed at the start.
Aligning Lines in the Flex Container
The align-content property controls the alignment of rows of flex items along the cross axis. It along with the justify-content property have very similar values:
- The default value stretch stretches the rows of flex items, while the remaining free space between them is distributed equally. How lines are displayed depends on the value of the stretch
- If align-items has been assigned the value stretch, then the items in the lines will be stretched to the entire height of their respective lines.
- If the value differs from stretch, then the items in the rows are compressed to the size of their own contents and are aligned in the rows depending on the value of align-items.
- flex-start — Arranges rows of flex items at the start of the cross axis,
- flex-end — Arranges rows of flex items at the end of the cross axis,
- center - Arranges rows of flex items in the middle of the cross axis so that there are no margins between adjacent rows, and the distance between the first row and the edge of the flex container is equal to the distance between the last row and the other edge.
- space-between - Evenly distributes the rows of flex items along the cross axis. The distances between adjacent rows are the same, but there are no margins at the edges.
- space-around - Evenly distributes the rows of flex items along the cross axis. The distances between adjacent rows are the same, but the margins at the edges are equal to half of the distance between neighboring rows.
The align-content property overrides any other assigned value of align-items, which controls the alignment of the flex items along the cross axis. This happens when there is only one row of flex items and where there are several such rows.
Previously the specification prescribed a different behavior:
- If there is only one row of flex items, then align-items takes precedence.
- If there are several rows, then align-content takes precedence.
In early 2019, the behavior was updated in all modern browsers according to the specification. The older behavior can only be found now in older browser versions.
The Flex Item Sequence Number
The order in which flex items are displayed in the stream can be changed using the order property, which is used to assign a sequence number to the flex item without having to change the HTML code.
By default, the sequence number of the flex items is equal to 0, and the items are sorted in ascending order starting from this value. The sequence number is an integer, which can be either positive or negative.
Using Flexboxes
Perfect Alignment
You can use a flexbox to center an item vertically and horizontally so that the alignment is maintained when the item or container is resized.
To do this, you need to set the flexbox layout for the container and margin: auto for the child flex item. In this case, the flex item will shrink in size to match the content and will be centered along the vertical and horizontal axes.
Flexible Menus
Flexbox is useful if you need to create a layout in which the items are evenly distributed across the menu block, where the first item is aligned to the left side of the menu block, and the last item is aligned to the right side, with small internal margins.
To do this, you need to set the flexbox layout menu. Then the items will become flex items. Then you can use the justify-content: space-around; property for arranging items to achieve the desired result.
If you add another item to the menu, the margins between the menu items will change “flexibly” and adapt to the new conditions.
Sorting Items in CSS
By using Flexbox and the checkbox selector :checked ~ at the same time, you can control the order of the flex items using this selector by changing the direction of the main axis using flex-direction.
The effect works best when the direction of the main axis changes from “top to bottom” to “bottom to top”. In this case, the flex container should be on the same level in the markup as the checkbox.
Blocks with Uniform Height
The usual block model has a fundamental drawback: the neighboring blocks are totally unaware of each other, so there is no way to control their heights as a group. It should be borne in mind that the contents of the blocks may differ and their heights may vary.
You can implement a layout using blocks of the same height using Flexbox. The flex items are stretched by default to the entire height of their respective containers. To do this, just assign display: flex; to the parent block.
Continue
<!DOCTYPE html><html lang="en" class="no-js"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><script>var b=document.documentElement.classList;b.remove('no-js');if(!window.Promise||!window.sessionStorage||!!sessionStorage.getItem('muller.v2')){b.add('muller')}</script><link rel="dns-prefetch" href="https://assets.htmlacademy.org"><script async src="https://www.googletagmanager.com/gtag/js?id=G-MXPCRXM48C"></script><script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-MXPCRXM48C');
</script><script type="text/javascript">
(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement("script")
;r.type="text/javascript"
;r.integrity="sha384-d/yhnowERvm+7eCU79T/bYjOiMmq4F11ElWYLmt0ktvYEVgqLDazh4+gW9CKMpYW"
;r.crossOrigin="anonymous";r.async=true
;r.src="https://cdn.amplitude.com/libs/amplitude-5.2.2-min.gz.js"
;r.onload=function(){if(!e.amplitude.runQueuedFunctions){
console.log("[Amplitude] Error: could not load SDK")}}
;var i=t.getElementsByTagName("script")[0];i.parentNode.insertBefore(r,i)
;function s(e,t){e.prototype[t]=function(){
this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}
var o=function(){this._q=[];return this}
;var a=["add","append","clearAll","prepend","set","setOnce","unset"]
;for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[]
;return this}
;var l=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
;for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","groupIdentify","onInit","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
;function v(e){function t(t){e[t]=function(){
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){
e=(!e||e.length===0?"$default_instance":e).toLowerCase()
;if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};v(n._iq[e])}return n._iq[e]}
;e.amplitude=n})(window,document);
amplitude.getInstance().init("df11525b6880a3c5e2af14f9b6238408", null,{
includeUtm: true,
includeGclid: true,
includeReferrer: true,
deviceIdFromUrlParam: true
}, function (instance) {
window.amplitudeLoaded = true;
});
</script><link rel="stylesheet" href="https://assets.htmlacademy.org/css/core.v284.css"><link rel="stylesheet" href="https://assets.htmlacademy.org/css/profile.v236.css"><link rel="stylesheet" href="https://assets.htmlacademy.org/css/course.v246.css"><link rel="stylesheet" href="https://assets.htmlacademy.org/css/course-interface-light.v20.css"><link rel="stylesheet" href="https://assets.htmlacademy.org/css/course-interface-en.v2.css"><script src="https://assets.htmlacademy.org/js/sentry.js" data-sentry="3774884cc81746ed84c0ba7c5cd4ac7b" data-project="26" data-version="2"></script><link rel="stylesheet" href="/css/custom.css"><link rel="stylesheet" href="/css/cookies.css"><link rel="preload" as="script" href="https://assets.htmlacademy.org/js/general.v274.js"><title>Summary of “Flexbox, Part 1” — Flexbox, part 1 — HTML Academy</title><meta name="csrf-token" content="47864caf322823086fca37a7326b9bd2a54"><meta property="og:type" content="website"><meta property="og:site_name" content="HTML Academy"><meta name="twitter:url" property="og:url" content="https://htmlacademy.org"><meta name="twitter:title" property="og:title" content="Interactive online courses HTML Academy"><meta name="twitter:description" property="og:description" content="Together we’ll learn how to work with real code, solve true-to-life problems, use cutting edge technologies. Minimum of boring theory and lots of practical tasks."><meta name="twitter:image" property="og:image" content="https://htmlacademy.org/og/htmlacademy.png"><meta name="twitter:card" content="summary_large_image"><link rel="canonical" href="https://htmlacademy.org/courses/layout/flexbox-1/summary"><meta name="theme-color" content="#2f358f"></head><body class="course-interface course-interface--light course-interface--full" data-base="/assets/courses/26/"><header class="page-header page-header--course"><div class="page-header__inner"><div class="page-header__top"><a class="page-header__logo" href="/" aria-label="HTML Academy Home"><img src="https://assets.htmlacademy.org/img/logo--small.svg?cs=1218aec0be4a5f23db79ad29a14e30f7f9fb9a25" width="24" height="36" alt="HTML Academy"></a><nav class="main-nav main-nav--mini" role="navigation"><div class="main-nav__course-nav"><a class="main-nav__course-item main-nav__course-button main-nav__course-button--prev" href="/courses/layout/flexbox-1/comples-palettes">Test: Complex palettes</a><div class="main-nav__course-item main-nav__course-list main-nav__course-list--collapsed"><b class="main-nav__course-title">Flexbox, part 1</b><span class="main-nav__course-stat">32/32</span><div class="main-nav__course-contents"><a class="main-nav__course-contents-link" href="/courses/layout/flexbox-1">Back to the list of tasks</a><ul class="main-nav__course-contents-list"><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/display-flex">1. The display: flex property, flex item</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flex-direction">2. The flex-direction property, main axis</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/cross-axis">3. The flexbox cross axis</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/justify-content">4. The justify-content property, alignment along the main axis</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/justify-content-flex-start-flex-end">5. The justify-content property: flex-start | flex-end</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/justify-content-space-between-space-around">6. The justify-content property: space-between | space-around</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-items">7. The align-items property, alignment along the cross axis</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-items-flex-start-flex-end">8. The align-items property: flex-start | flex-end</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-items-baseline">9. The align-items: baseline property</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-self">10. The align-self property, or selfish alignment</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-self-baseline">11. The align-self: baseline property</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/creating-simple-palette">12. Test: Creating a simple palette</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flex-wrap">13. The flex-wrap property, overflow of flex items</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flex-wrap-reverse">14. The flex-wrap property, reverse overflow</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-content">15. The align-content property, alignment of flex container lines</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-content-stretch">16. The align-content property: stretch and align-items</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-content-non-stretch">17. The align-content property: non-stretch and align-items</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/align-content-values">18. The align-content property, remaining values</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flexbox-order">19. The order property, the sequence number of the flex item</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/creating-more-complicated-palette">20. Test: Creating a more complicated palette</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flexbox-center-margin-auto">21. Perfect centering by assigning margin: auto to the flex container</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flexbox-center-properties">22. Perfect centering, flex alignment</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/adaptive-menu">23. The adaptive horizontal menu, part 1</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/adaptive-menu-2">24. The adaptive horizontal menu, part 2</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/adaptive-menu-3">25. The adaptive horizontal menu, part 3</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/vertical-row-icons">26. The vertical row of icons</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/vertical-row-icons-2">27. The vertical row of icons, part 2</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/vertical-row-icons-3">28. The vertical row of icons, part 3</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/order-elements-css">29. Ordering elements with CSS</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/flexbox-vertical-align">30. Vertically aligning blocks using a flexbox</a></li><li class="main-nav__course-contents-item"><a href="/courses/layout/flexbox-1/comples-palettes">31. Test: Complex palettes</a></li><li class="main-nav__course-contents-item main-nav__course-contents-item--current"><a href="/courses/layout/flexbox-1/summary">32. Summary of “Flexbox, Part 1”</a></li></ul></div></div><a class="main-nav__course-item main-nav__course-button main-nav__course-button--next" href="/courses/layout/flexbox-1">List of tasks</a></div><ul class="main-nav__list main-nav__list--user main-nav__list--user-guest"><li class="main-nav__item" itemprop="name"><a class="main-nav__link" href="/signup?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" title="Sign up" data-modal="open" data-value="register" itemprop="url"><span class="main-nav__icon" aria-hidden="true"><svg aria-hidden="true"><use xlink:href="/img/sprites/general.svg#user"></use></svg></span>Sign up</a></li><li class="main-nav__item main-nav__item--login" itemprop="name"><a class="main-nav__link" href="/login?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" title="Log in" data-modal="open" data-value="login" itemprop="url"><span class="main-nav__icon" aria-hidden="true"><svg aria-hidden="true"><use xlink:href="/img/sprites/general.svg#login"></use></svg></span>Log in</a></li></ul></nav></div></div></header><main class="course-container"><div class="course-theory"><div class="course-theory__inner"><section class="course-theory__content"><h1 class="course-theory__content-heading course-theory__content-heading--synopsis">Summary of “Flexbox, Part 1”</h1><div class="course-theory__content-text"><h2>Flexbox</h2><p>Flexbox is a CSS mechanism that allows you to control the size, order, and alignment of items along several axes, the distribution of free space between items, and much more.</p><p>In order to enable the flexbox, you need to assign the <code>display: flex;</code> property to the item. Then:</p><ol><li>The item containing <code>display: flex;</code> is transformed into a <i>flex container</i>.</li><li>The immediate children of this item will be transformed into <i>flex items</i>, and they will start to be arranged according to the new rules.</li></ol><h2>Main and Cross Axes</h2><p>Axes represent one of the basic concepts in Flexbox. </p><p>When a document flows normally, blocks and text are arranged from left to right and from top to bottom.</p><p>Under the traditional block model, the directions “left”, “right”, “top”, and “bottom” cannot be changed. But within a flex container you can modify these concepts, since you can change the typical direction of the flow.</p><ul><li><b>The main axis</b> of the flex container is the direction along which all of its child items are arranged. The flex items flow along the main axis from the start to the end of it.</li><li>We call the direction that is perpendicular to the main axis the <b>cross axis</b>. The “vertical” alignments are made along this axis.</li></ul><p><img src="/assets/courses/26/img/flexbox-2.png" width="920" alt="The cross axis in the flexbox"></p><p>By default, the main axis flows from left to right, but it can be rotated in all directions using the <code>flex-direction</code> property, which is assigned for the flex container. Value properties:</p><ul><li>The following are the default values: <code>row</code> – The main axis flows from left to right.</li><li><code>column</code> — The main axis flows from top to bottom.</li><li><code>row-reverse</code> — The main axis flows from right to left.</li><li><code>column-reverse</code> — The main axis flows from bottom to top.</li></ul><p>The cross axis is always perpendicular to the main axis, and when the latter is rotated, the former rotates too to stay perpendicular:</p><ul><li>If the main axis flows horizontally, then the cross axis will flow vertically down.</li><li>If the main axis flows vertically, then the cross axis will flow to the right.</li></ul><p>Thus, the cross axis will never flow up or to the left, and there is no property that rotates the cross axis.</p><h2>Arrangement of Flex Items</h2><p><i>Main axis alignment</i></p><p>The <code>justify-content</code> CSS property determines how items are aligned along the <b>main</b> axis. Available values for <b>justify-content</b>:</p><ul><li>The following are the default values: <code>flex-start</code> – Items are arranged at the start of the main axis.</li><li><code>flex-end</code> — Items are arranged at the end of the main axis.</li><li><code>center</code> — Items are arranged in the center of the main axis.</li><li><code>space-between</code> — Items are arranged so that the distances between adjacent ones are uniform, and there are no margins between the items and the edges of the flex container.</li><li><code>space-around</code> — Items are arranged in such a way that the distances between neighboring items are uniform, and there is a margin between the items and the edges of the flex container that is equal to half of the distance between neighboring items.</li></ul><p><i>Alignment along the cross axis</i></p><p>The <code>align-items</code> CSS property determines how items are aligned along the cross axis. Available values for <code>align-items</code>:</p><ul><li>Default value of <code>stretch</code> – Items are stretched to fill the entire “height” of the flex container.
</li><li><code>flex-start</code> — Items are arranged at the start of the cross axis.</li><li><code>flex-end</code> — Items are arranged at the end of the cross axis.</li><li><code>center</code> — Items are arranged in the center of the cross axis.</li><li><code>baseline</code> — Items are aligned along the baseline of the text inside them.</li></ul><p>The arrangement of items along the main axis is assigned for the entire flex container, and the arrangement properties apply to all flex items equally. You are not allowed to assign an arrangement to one particular item along the main axis that differs from the one assigned to the other items. </p><p>The cross alignment can be assigned to each item separately. To do this, you can use the <code>align-self</code> property, which is assigned for the flex items themselves and not for the flex container. The <code>align-self</code> property has the very same values as <code>align-items</code>.</p><h2>Flex Item Overflow</h2><p>If you add more flex items to a container than can fit in a single row, then </p><ul><li>they will be shrunk to the smallest possible width.</li><li>Even if you assign a width to them, the flexbox mechanism can override this setting and shrink it.</li><li>If they can no longer be fit in a container after it is shrunk, they will overflow its boundaries, but they will continue to be placed in a single row.</li></ul><p>To avoid this, you need to use the <code>flex-wrap</code> flex container property. It has two values: </p><ul><li>The default value <code>nowrap</code> — Flex items cannot overflow onto a new line.</li><li><code>wrap</code> — The overflow of flex items to a new line is allowed. Rows of items are arranged along the cross axis, where the first row is at the start of the cross axis and the last is at the end.</li><li><code>wrap-reverse</code> — The overflow of flex items onto a new line is also allowed. Rows of items are arranged in reverse order: the first row is placed at the end of the cross axis, and the last is placed at the start.</li></ul><h2>Aligning Lines in the Flex Container</h2><p>The <code>align-content</code> property controls the alignment of rows of flex items along the cross axis. It along with the <code>justify-content</code> property have very similar values: </p><ul><li>The default value <code>stretch</code> stretches the rows of flex items, while the remaining free space between them is distributed equally. How lines are displayed depends on the value of the <code>stretch</code><ol><li>If <code>align-items</code> has been assigned the value <code>stretch</code>, then the items in the lines will be stretched to the entire height of their respective lines.</li><li>If the value differs from <code>stretch</code>, then the items in the rows are compressed to the size of their own contents and are aligned in the rows depending on the value of <code>align-items</code>.
</li></ol></li><li><code>flex-start</code> — Arranges rows of flex items at the start of the cross axis,</li><li><code>flex-end</code> — Arranges rows of flex items at the end of the cross axis,</li><li><code>center</code> - Arranges rows of flex items in the middle of the cross axis so that there are no margins between adjacent rows, and the distance between the first row and the edge of the flex container is equal to the distance between the last row and the other edge.</li><li><code>space-between</code> - Evenly distributes the rows of flex items along the cross axis. The distances between adjacent rows are the same, but there are no margins at the edges.</li><li><code>space-around</code> - Evenly distributes the rows of flex items along the cross axis. The distances between adjacent rows are the same, but the margins at the edges are equal to half of the distance between neighboring rows.</li></ul><p>The <code>align-content</code> property overrides any other assigned value of <code>align-items</code>, which controls the alignment of the flex items along the cross axis. This happens when there is only one row of flex items and where there are several such rows.</p><p>Previously the specification prescribed a different behavior:</p><ul><li>If there is only one row of flex items, then <code>align-items</code> takes precedence.</li><li>If there are several rows, then <code>align-content</code> takes precedence.</li></ul><p>In early 2019, the behavior was updated in all modern browsers according to the specification. The older behavior can only be found now in older browser versions.</p><h2>The Flex Item Sequence Number</h2><p>The order in which flex items are displayed in the stream can be changed using the <code>order</code> property, which is used to assign a sequence number to the flex item without having to change the HTML code.</p><p>By default, the sequence number of the flex items is equal to <code>0</code>, and the items are sorted in ascending order starting from this value. The sequence number is an integer, which can be either positive or negative.</p><h2>Using Flexboxes</h2><h3>Perfect Alignment</h3><p>You can use a flexbox to center an item vertically and horizontally so that the alignment is maintained when the item or container is resized.</p><p>To do this, you need to set the flexbox layout for the container and <code>margin: auto</code> for the child flex item. In this case, the flex item will shrink in size to match the content and will be centered along the vertical and horizontal axes. </p><h3>Flexible Menus</h3><p>Flexbox is useful if you need to create a layout in which the items are evenly distributed across the menu block, where the first item is aligned to the left side of the menu block, and the last item is aligned to the right side, with small internal margins.</p><p>To do this, you need to set the flexbox layout menu. Then the items will become flex items. Then you can use the <code>justify-content: space-around;</code> property for arranging items to achieve the desired result.</p><p>If you add another item to the menu, the margins between the menu items will change “flexibly” and adapt to the new conditions.</p><h3>Sorting Items in CSS</h3><p>By using Flexbox and the checkbox selector <code>:checked ~</code> at the same time, you can control the order of the flex items using this selector by changing the direction of the main axis using <code>flex-direction</code>. </p><p>The effect works best when the direction of the main axis changes from “top to bottom” to “bottom to top”. In this case, the flex container should be on the same level in the markup as the checkbox.</p><h3>Blocks with Uniform Height</h3><p>The usual block model has a fundamental drawback: the neighboring blocks are totally unaware of each other, so there is no way to control their heights as a group. It should be borne in mind that the contents of the blocks may differ and their heights may vary.</p><p>You can implement a layout using blocks of the same height using Flexbox. The flex items are stretched by default to the entire height of their respective containers. To do this, just assign <code>display: flex;</code> to the parent block.</p><br><a class="button button--green button--large button--wide button--icon" href="/courses/layout/flexbox-1"><svg aria-hidden="true"><use xlink:href="/img/sprites/general.svg#icon-check-bold"></use></svg>
Continue
</a></div></section></div></div><script
src="https://assets.htmlacademy.org/scripts/courses-spa/htmlacademy-task.v43.js"
data-assets-path="https://assets.htmlacademy.org/scripts/courses-spa/"
data-task-type="theory"
data-lang="en"
></script><script>HtmlacademyTask.setup(function(){});</script></main><footer class="page-footer page-footer--tiny"><div class="page-footer__inner"><p><a href="/docs/cookies">Cookies</a> ∙
<a href="/docs/privacy">Privacy</a> ∙
<a href="/docs/agreement">License Agreement</a> ∙
<a href="/docs/about">About</a> ∙
<a href="/contacts">Contacts</a> ∙
© HTML Academy OÜ, 2019−2026
</p><div class="page-footer__financial"><img src="https://assets.htmlacademy.org/img/visa-white.svg?cs=96e54ec8c587db9d4b1d8d328ffd87c2ebfd9555" alt="VISA" title="VISA" width="35" height="35"><img src="https://assets.htmlacademy.org/img/mastercard-horizontal.v2.svg" alt="Mastercard" title="Mastercard" width="35" height="35"></div></div></footer><div class="modal"><div class="modal__inner"><div class="modal__wrapper js-login hidden"><button class="modal__close icon-close" type="button" title="Close" data-modal="close"></button><h4 class="modal__header">Log in</h4><ul class="modal__social"><li class="modal__social-link modal__social-link--fb"><a href="/login/fb?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" rel="nofollow" title="Log in via Facebook"><svg aria-hidden="true"><use xlink:href="/img/sprites/general.svg#facebook"></use></svg></a></li><li class="modal__social-link modal__social-link--google"><a href="/login/google?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" rel="nofollow" title="Log in via Google"><svg height="30" width="30" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><path fill="#ffffff" d="M457.732 216.625c2.628 14.041 4.063 28.743 4.063 44.098C461.796 380.688 381.481 466 260.204 466c-116.023 0-210-93.977-210-210s93.977-210 210-210c56.704 0 104.077 20.867 140.44 54.73l-59.204 59.197v-.135c-22.046-21.002-50-31.762-81.236-31.762-69.297 0-125.604 58.537-125.604 127.841 0 69.29 56.306 127.968 125.604 127.968 62.87 0 105.653-35.965 114.46-85.312h-114.46v-81.902h197.528z"/></svg></a></li></ul><div class="modal__or"><span>or</span></div><form class="modal__form form" action="/login?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" autocomplete="off" method="post" data-submit="o"><input type="hidden" name="csrf_name" value="csrf69aed534a0915"><input type="hidden" name="csrf_value" value="c8f9bc3748b4b8230402a47cd8c579bb"><div class="form__group"><label class="sr-only" for="login-email">Email</label><input class="field field--text field--full-width" type="email" name="email" placeholder="Email" value="" id="login-email"></div><div class="form__group"><label class="sr-only" for="login-password">Password</label><input class="field field--text field--full-width ym-disable-keys" type="password" name="password" placeholder="Password" id="login-password"></div><input class="button button--full-width" type="submit" data-submit-text="Logging in…" value="Log in"></form><p class="modal__forgot-password"><a href="/recover" data-modal="open" data-value="recover">Forgot your password?</a></p><a class="modal__bottom-link" href="/signup" data-modal="open" data-value="register">Sign up</a></div><div class="modal__wrapper js-register hidden"><button class="modal__close icon-close" type="button" title="Close" data-modal="close"></button><h4 class="modal__header">Sign up</h4><ul class="modal__social"><li class="modal__social-link modal__social-link--fb"><a href="/login/fb?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" rel="nofollow" title="Log in via Facebook"><svg aria-hidden="true"><use xlink:href="/img/sprites/general.svg#facebook"></use></svg></a></li><li class="modal__social-link modal__social-link--google"><a href="/login/google?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" rel="nofollow" title="Log in via Google"><svg height="30" width="30" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><path fill="#ffffff" d="M457.732 216.625c2.628 14.041 4.063 28.743 4.063 44.098C461.796 380.688 381.481 466 260.204 466c-116.023 0-210-93.977-210-210s93.977-210 210-210c56.704 0 104.077 20.867 140.44 54.73l-59.204 59.197v-.135c-22.046-21.002-50-31.762-81.236-31.762-69.297 0-125.604 58.537-125.604 127.841 0 69.29 56.306 127.968 125.604 127.968 62.87 0 105.653-35.965 114.46-85.312h-114.46v-81.902h197.528z"/></svg></a></li></ul><div class="modal__or"><span>or</span></div><form class="modal__form form" action="/signup?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" autocomplete="off" method="post"><input type="hidden" name="csrf_name" value="csrf69aed534a0915"><input type="hidden" name="csrf_value" value="c8f9bc3748b4b8230402a47cd8c579bb"><div class="form__group"><label class="sr-only" for="email">
Email
<span class="required"><span class="sr-only">Required field</span><span class="required__star">*</span></span></label><div class="form__group-fields"><input class="field field--text field--full-width" type="email" name="email" value="" id="email" required placeholder="Email"></div></div><div class="form__group"><label class="sr-only" for="password">
Password
<span class="required"><span class="sr-only">Required field</span><span class="required__star">*</span></span></label><div class="form__group-fields"><input class="field field--text field--full-width" type="password" name="password" value="" id="password" required placeholder="Password"></div></div><div class="form__group"><label class="checkbox"><input class="checkbox__input" type="checkbox" name="agreement" value="1" required><span class="checkbox__text"><span>By signing up, you agree to our <a href="/docs/agreement" target="_blank">License Agreement</a> and <a href="/docs/privacy" target="_blank">Privacy Policy</a>.</span></span></label></div><input class="button button--full-width" type="submit" data-submit-text="Signing up…" value="Sign up"></form><a class="modal__bottom-link" href="/login?redirect_url=%2Fcourses%2Flayout%2Fflexbox-1%2Fsummary" data-modal="open" data-value="login">Log in</a></div><div class="modal__wrapper modal__wrapper--no-btn-bottom js-recover hidden"><button class="modal__close icon-close" type="button" title="Close" data-modal="close"></button><h4 class="modal__header">Restore access</h4><p class="modal__text-accent">Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.</p><form class="modal__form form" action="/recover" autocomplete="off" method="post" data-submit="o"><input type="hidden" name="csrf_name" value="csrf69aed534a0915"><input type="hidden" name="csrf_value" value="c8f9bc3748b4b8230402a47cd8c579bb"><div class="form__group"><label class="sr-only" for="recovery-email">Email</label><input class="field field--text field--full-width" type="email" name="email" placeholder="Email" value="" id="recovery-email"></div><script src='https://www.google.com/recaptcha/api.js'></script><div class="form__group"><div class="g-recaptcha" data-sitekey="6LetCTEqAAAAANROWtPzfC7Rfg9iIRiRt2k2FPn7"></div></div><input class="button button--full-width" type="submit" data-submit-text="Sending…" value="Send"></form><p class="modal__text">Forgot to connect your email to the profile? Email us and we’ll help.</p></div></div></div><script async src="https://assets.htmlacademy.org/js/general.v274.js" data-assets="https://assets.htmlacademy.org" data-require="toggle,navigation-courses,modal,form,nav"></script></body></html>