Contacts

Layout using CSS Flexbox. What is Flexbox? Description of all css properties, basic principles, advantages and disadvantages of Inline flex container

In this article, we'll introduce CSS Flexbox technology, designed to create flexible web page layouts.

Purpose of CSS Flexbox

CSS Flexbox is for creating flexible layouts. Using this technology, you can very simply and flexibly arrange elements in a container, distribute the available space between them, and align them in one way or another, even if they do not have specific dimensions.

CSS Flexbox makes creating responsive designs much easier than using Float and positioning.

Flexbox can be used for both CSS markup the whole page and its individual blocks.

Browser support for CSS Flexbox

CSS Flexbox is supported by all currently used modern browsers(using prefixes: IE10+, Edge12+, Firefox 2+, Chrome 4+, Safari 3.1+, Opera 12.1+, iOS Safari 3.2, Opera mini, Android 2.1+, Blackberry 7+).

CSS Flexbox Basics

Creating CSS markup using Flexbox starts with setting the necessary HTML element CSS display properties with the value flex or flex-inline.

After that this element becomes a flex container, and all of it direct child elements are flex elements. Moreover, when we talk about flexbox, we mean only an element with display:flex or display:flex-inline and all elements directly located in it. Thus, in CSS Flexbox there are only two types of elements: a flex container and a flex element.


Prefix and max-width properties have been added to CSS to support layout in most browsers.

To “turn” a block into a flex container, use the row class. Setting the width of the .col__article and .col__aside flex elements inside a flex container is done using the flex CSS property.

As an example, let's mark up another footer using flexbox and create a block consisting of three elements in the .col__article element (the minimum width of one element is 300px). We will place four blocks in the footer (the minimum width of one block is 200px).


CSS flexbox (Flexible Box Layout Module)- flexible container layout module - is a way of arranging elements, based on the idea of ​​an axis.

Flexbox consists of flexible container And flexible items. Flexible elements can be arranged in a row or column, and the remaining free space is distributed between them in various ways.

The flexbox module allows you to solve the following tasks:

  • Arrange elements in one of four directions: left to right, right to left, top to bottom, or bottom to top.
  • Override the display order of elements.
  • Automatically size elements so that they fit into the available space.
  • Solve the problem with horizontal and vertical centering.
  • Move items inside a container without overflowing it.
  • Create columns of the same height.
  • Create pressed to the bottom of the page.

Flexbox solves specific problems - creating one-dimensional layouts, for example, a navigation bar, since flex elements can only be placed along one of the axes.

You can read a list of current module problems and cross-browser solutions for them in the article by Philip Walton.

What is flexbox

Browser support

IE: 11.0, 10.0 -ms-
Firefox: 28.0, 18.0 -moz-
Chrome: 29.0, 21.0 -webkit-
Safari: 6.1 -webkit-
Opera: 12.1 -webkit-
iOS Safari: 7.0 -webkit-
Opera Mini: 8
Android Browser: 4.4, 4.1 -webkit-
Chrome for Android: 44

1. Basic concepts

Rice. 1. Flexbox model

A specific set of terms is used to describe a Flexbox module. The flex-flow value and recording mode determine the correspondence of these terms to the physical directions: top / right / bottom / left, axes: vertical / horizontal, and dimensions: width / height.

Main axis— the axis along which flex elements are laid out. It extends into the main dimension.

Main start and main end— lines that define the start and end sides of the flex container, relative to which flex elements are laid out (starting from main start towards main end).

Main size) - the width or height of the flex container or flex items, depending on which of them is in the main dimension, determines the main size of the flex container or flex item.

Cross axis- axis perpendicular to the main axis. It extends in the transverse dimension.

Cross start and cross end— lines that define the starting and ending sides of the transverse axis, relative to which flex elements are laid out.

Cross size— the width or height of a flex container or flex items, whichever is in the cross dimension, is their cross dimension.


Rice. 2. Row and column mode

2. Flex container

A Flex container sets a new flexible formatting context for its content. A flex container is not a block container, so CSS properties such as float , clear , vertical-align do not work for child elements. Also, the flex container is not affected by the column-* properties that create columns in the text and the pseudo-elements::first-line and::first-letter .

The flexbox markup model is associated with a specific value of the CSS display property of the parent HTML element containing child blocks within it. To control elements using this model, you need to set the display property as follows:

Flex-container ( /*generates a block-level flex container*/ display: -webkit-flex; display: flex; ) .flex-container ( /*generates a line-level flex container*/ display: -webkit-inline-flex; display: inline-flex)

After setting these property values, each child element automatically becomes a flex element, lining up in one row (along the main axis). In this case, block and inline child elements behave the same, i.e. The width of the blocks is equal to the width of their content, taking into account the internal margins and borders of the element.


Rice. 3. Aligning elements in the flexbox model

If the parent block contains text or images without wrappers, they become anonymous flex items. The text is aligned to the top edge of the container block, and the height of the image becomes equal to the height of the block, i.e. it is deformed.

3. Flex elements

Flex items are blocks that represent the contents of a flex container in a flow. The Flex container establishes a new formatting context for its content, which causes the following features:

  • For flex items, their display property value is locked. Display value: inline-block; and display: table-cell; evaluated in display: block; .
  • The white space between items disappears: it does not become its own flex item, even if the interitem text is wrapped in an anonymous flex item. An anonymous flex item's content cannot be styled, but will inherit styles (such as font options) from the flex container.
  • An absolutely positioned flex element does not participate in the flex layout layout.
  • The margins of adjacent flex elements do not collapse.
  • Percentage margin and padding values ​​are calculated from the internal size of the block containing them.
  • margin: auto; expand, absorbing additional space in the corresponding dimension. They can be used to align or push adjacent flex items.
  • The default automatic minimum size of flex items is the minimum size of its content, i.e. min-width: auto; . For scrollable containers, the automatic minimum size is usually zero.

4. Flex Item Display Order and Orientation

The contents of a flex container can be laid out in any direction and in any order (rearranging flex items within the container only affects visual rendering).

4.1. Main axis direction: flex-direction

The property refers to the flex container. Controls the direction of the main axis along which flex items are stacked, in accordance with the current writing mode. Not inherited.

flex-direction
Values:
row The default value is left to right (in rtl, right to left). Flex elements are laid out in a row. The start (main-start) and end (main-end) of the main axis direction correspond to the start (inline-start) and end (inline-end) of the line axis (inline-axis).
row-reverse The direction is from right to left (in rtl from left to right). Flex elements are laid out in a line relative to the right edge of the container (in rtl - the left).
column Direction from top to bottom. Flex elements are laid out in a column.
column-reverse A column with elements in reverse order, from bottom to top.
initial
inherit

Rice. 4. Flex-direction property for left-to-right languages

Syntax

Flex-container ( display: -webkit-flex; -webkit-flex-direction: row-reverse; display: flex; flex-direction: row-reverse; )

4.2. Managing multi-line flex container: flex-wrap

The property determines whether the flex container will be single-line or multi-line, and also sets the direction of the cross axis, which determines the direction in which new lines of the flex container will be laid.

By default, flex items are stacked on one line, along the main axis. If they overflow, they will extend beyond the bounding box of the flex container. The property is not inherited.


Rice. 5. Multi-line control using the flex-wrap property for LTR languages

Syntax

Flex-container ( display: -webkit-flex; -webkit-flex-wrap: wrap; display: flex; flex-wrap: wrap; )

4.3. A short summary of direction and multi-line: flex-flow

The property allows you to define the directions of the main and transverse axes, as well as the ability to move flex elements, if necessary, onto several lines. This is a shorthand for the flex-direction and flex-wrap properties. Default value flex-flow: row nowrap; . the property is not inherited.

Syntax

Flex-container ( display: -webkit-flex; -webkit-flex-flow: row wrap; display: flex; flex-flow: row wrap; )

4.4. Display order of flex items: order

The property defines the order in which flex items are displayed and arranged within a flex container. Applies to flex items. The property is not inherited.

Initially all flex items have order: 0; . When you specify a value of -1 for an element, it is moved to the beginning of the timeline, and a value of 1 is moved to the end. If multiple flex items have the same order value, they will be rendered according to the original order.

Syntax

Flex-container ( display: -webkit-flex; display: flex; ) .flex-item ( -webkit-order: 1; order: 1; )
Rice. 6. Display order of flex items

5. Flexibility of flex elements

The defining aspect of a flex layout is the ability to "flex" flex items, changing their width/height (depending on what size is on the main axis) to fill the available space in the main dimension. This is done using the flex property. A flex container distributes free space among its children (proportional to their flex-grow ratio) to fill the container, or shrinks them (proportional to their flex-shrink ratio) to prevent overflow.

A flex element will be completely "inflexible" if its flex-grow and flex-shrink values ​​are zero, and "flexible" otherwise.

5.1. Setting flexible dimensions with one property: flex

The property is shorthand for the flex-grow, flex-shrink, and flex-basis properties. Default value: flex: 0 1 auto; . You can specify one or all three property values. The property is not inherited.

Syntax

Flex-container ( display: -webkit-flex; display: flex; ) .flex-item ( -webkit-flex: 3 1 100px; -ms-flex: 3 1 100px; flex: 3 1 100px; )

5.2. Growth factor: flex-grow

The property determines the growth rate of one flex element relative to other flex elements in the flex container when distributing positive free space. If the sum of the flex-grow values ​​of flex items in a row is less than 1, they take up less than 100% of the free space. The property is not inherited.


Rice. 7. Manage Navigation Bar Space with Flex-Grow

Syntax

Flex-container ( display: -webkit-flex; display: flex; ) .flex-item ( -webkit-flex-grow: 3; flex-grow: 3; )

5.3. Compression ratio: flex-shrink

The property specifies the compression ratio of a flex item relative to other flex items when distributing negative free space. Multiplied by the flex-basis size. Negative space is allocated in proportion to how much the item can shrink, so, for example, a small flex item won't shrink to zero until a larger flex item is noticeably shrunk. The property is not inherited.


Rice. 8. Narrowing flex items in a row

Syntax

Flex-container ( display: -webkit-flex; display: flex; ) .flex-item ( -webkit-flex-shrink: 3; flex-shrink: 3; )

5.4. Basic size: flex-basis

The property sets the initial base size of a flex item before allocating free space according to flex factors. For all values ​​except auto and content , the base flex size is defined the same as width in horizontal writing modes. Percentage values ​​are defined relative to the size of the flex container, and if no size is specified, the value used for flex-basis is the dimensions of its contents. Not inherited.

Syntax

Flex-container ( display: -webkit-flex; display: flex; ) .flex-item ( -webkit-flex-basis: 100px; flex-basis: 100px; )

6. Alignment

6.1. Main axis alignment: justify-content

The property aligns flex items along the main axis of the flex container, distributing the free space unoccupied by flex items. When an item is converted to a flex container, flex items are grouped together by default (unless they have margins set). Gaps are added after calculating the margin and flex-grow values. If any elements have a non-zero flex-grow or margin: auto; , the property will have no effect. The property is not inherited.

Values:
flex-start Default value. Flex elements are laid out in a direction starting from the starting line of the flex container.
flex-end Flex items are laid out in the direction from the end line of the flex container.
center Flex items are aligned to the center of the flex container.
space-between Flex elements are distributed evenly along the line. The first flex item is placed flush with the edge of the start line, the last flex item is placed flush with the edge of the end line, and the remaining flex items on the line are spaced so that the distance between any two adjacent items is the same. If the remaining free space is negative or there is only one flex item per line, this value is identical to the flex-start parameter.
space-around The flex items on the line are distributed so that the distance between any two adjacent flex items is the same, and the distance between the first/last flex items and the edges of the flex container is half the distance between the flex items.
initial Sets the property value to the default value.
inherit Inherits the property value from the parent element.

Rice. 9. Aligning elements and distributing free space using the justify-content property

Syntax

Flex-container ( display: -webkit-flex; -webkit-justify-content: flex-start; display: flex; justify-content: flex-start; )

6.2. Cross axis alignment: align-items and align-self

Flex items can be aligned along the transverse axis of the current row of the flex container. align-items sets the alignment for all flex container items, including anonymous flex items. align-self allows you to override this alignment for individual flex items. If any of the flex element's cross margins are set to auto , align-self has no effect.

6.2.1. Align-items

The property aligns flex elements, including anonymous flex elements, along the transverse axis. Not inherited.

Values:
flex-start
flex-end
center
baseline The baselines of all flex items involved in alignment are the same.
stretch
initial Sets the property value to the default value.
inherit Inherits the property value from the parent element.
Rice. 10. Aligning elements in a container vertically

Syntax

Flex-container ( display: -webkit-flex; -webkit-align-items: flex-start; display: flex; align-items: flex-start; )

6.2.2. Align-self

The property is responsible for aligning a single flex element to the height of the flex container. Overrides the alignment specified by align-items . Not inherited.

Values:
auto Default value. A flex element uses the alignment specified in the align-items property of the flex container.
flex-start The top edge of the flex element is placed adjacent to the flex line (or at a distance, taking into account the specified margins and borders of the element) passing through the beginning of the cross axis.
flex-end The bottom edge of the flex element is placed adjacent to the flex line (or at a distance, taking into account the specified margins and borders of the element) passing through the end of the cross axis.
center The margin of a flex element is centered along the cross axis within the flex line.
baseline The flex element is aligned to the baseline.
stretch If a flex item's cross-size is auto and none of the cross-margin values ​​are auto , the item is stretched. Default value.
initial Sets the property value to the default value.
inherit Inherits the property value from the parent element.

Rice. 11. Aligning individual flex items

Syntax

Flex-container ( display: -webkit-flex; display: flex; ) .flex-item ( -webkit-align-self: center; align-self: center; )

6.3. Aligning the lines of a flex container: align-content

The property aligns rows in a flex container when there is extra space on the cross axis, similar to justifying individual items on the main axis using the justify-content property. The property does not affect a single-line flex container. Not inherited.

Values:
flex-start Rows are stacked towards the beginning of the flex container. The edge of the first line is placed close to the edge of the flex container, each subsequent line is placed close to the previous line.
flex-end Rows are stacked towards the end of the flex container. The edge of the last line is placed close to the edge of the flex container, each previous line is placed close to the next line.
center Rows are stacked towards the center of the flex container. The rows are close together and aligned to the center of the flex container, with equal distance between the starting edge of the flex container's content and the first row, and between the ending edge of the flex container's content and the last row.
space-between The rows are evenly distributed in the flex container. If the remaining free space is negative or there is only one flex line in the flex container, this value is identical to flex-start . Otherwise, the edge of the first line is placed close to the start edge of the flex container's content, and the edge of the last line is placed close to the end edge of the flex container's content. The remaining lines are distributed so that the distance between any two adjacent lines is the same.
space-around The lines are evenly spaced in the flex container with half space on both ends. If the remaining free space is negative, this value is identical to center center . Otherwise, the lines are distributed so that the distance between any two adjacent lines is the same, and the distance between the first/last lines and the edges of the flex container's contents is half the distance between the lines.
stretch Default value. Rows of flex items stretch evenly to fill all available space. If the remaining free space is negative, this value is identical to flex-start . Otherwise, the free space will be divided equally between all rows, increasing their lateral size.
initial Sets the property value to the default value.
inherit Inherits the property value from the parent element.
Rice. 12. Multi-line alignment of flex elements

Syntax

Flex-container ( display: -webkit-flex; -webkit-flex-flow: row wrap; -webkit-align-content: flex-end; display: flex; flex-flow: row wrap; align-content: flex-end ; height: 100px )

Flexbox can rightfully be called a successful attempt to solve a huge range of problems when building layouts in CSS. But before we move on to its description, let's find out what's wrong with the layout methods we use now?

Any layout designer knows several ways to align something vertically or make a 3-column layout with a rubbery middle column. But let's admit that all these methods are quite strange, look like a hack, are not suitable in all cases, are difficult to understand and do not work if certain magical conditions that have developed historically are not met.

This happened because html and css developed evolutionarily. At the beginning, web pages looked like single-threaded text documents, a little later, breaking the page into blocks was done with tables, then it became fashionable to layout with floats, and after the official death of ie6, inline-block techniques were also added. As a result, we inherited an explosive mixture of all these techniques, used to build layouts for 99.9% of all existing web pages.

Multi-line organization of blocks inside a flex container.

flex-wrap

All the examples that we gave above were built taking into account the single-line (single-column) arrangement of blocks. It must be said that by default, a flex container will always arrange the blocks inside itself in one line. However, the specification also supports multiline mode. The flex-wrap CSS property is responsible for multi-line content inside a flex container.

Available values flex-wrap:

  • nowrap (default value): blocks are arranged in one line from left to right (in rtl from right to left)
  • wrap: blocks are arranged in several horizontal rows (if they do not fit in one row). They follow each other from left to right (in rtl from right to left)
  • wrap-reverse: same as wrap, but the blocks are arranged in the reverse order.

flex-flow is a convenient shorthand for flex-direction + flex-wrap

In essence, flex-flow provides the ability to describe the direction of the main and multi-line transverse axis in one property. Default flex-flow: row nowrap .

flex-flow:<‘flex-direction’> || <‘flex-wrap’>

CSS

/* i.e. ... */ .my-flex-block( flex-direcrion:column; flex-wrap: wrap; ) /* this is the same as... */ .my-flex-block( flex-flow: column wrap)

align-content

There is also an align-content property, which determines how the resulting rows of blocks will be vertically aligned and how they will divide the entire space of the flex container.

Important: align-content only works in multi-line mode (i.e. in the case of flex-wrap:wrap; or flex-wrap:wrap-reverse;)

Available values align-content:

  • flex-start: rows of blocks are pressed to the beginning of the flex container.
  • flex-end: rows of blocks are pressed to the end of the flex container
  • center: rows of blocks are in the center of the flex container
  • space-between: the first row of blocks is located at the beginning of the flex container, the last row of blocks is at the end, all other rows are evenly distributed in the remaining space.
  • space-around: rows of blocks are evenly distributed from the beginning to the end of the flex container, dividing all available space equally.
  • stretch (default value): The rows of blocks are stretched out to take up all the available space.

The CSS properties flex-wrap and align-content should be applied directly to the flex container, not to its children.

Demo of multi-line properties in flex

CSS rules for child elements of a flex container (flex blocks)

flex-basis – the basic size of a single flex block

Sets the initial major axis size for a flex block before transformations based on other flex factors are applied to it. Can be specified in any length units (px, em, %, ...) or auto (default). If set as auto, the block dimensions (width, height) are taken as a basis, which, in turn, can depend on the size of the content, if not specified explicitly.

flex-grow – “greed” of a single flex block

Determines how much larger an individual flex block can be than adjacent elements, if necessary. flex-grow accepts a dimensionless value (default 0)

Example 1:

  • If all flex boxes inside a flex container have flex-grow:1 then they will be the same size
  • If one of them has flex-grow:2 then it will be 2 times larger than all the others

Example 2:

  • If all flex boxes inside a flex container have flex-grow:3 then they will be the same size
  • If one of them has flex-grow:12 then it will be 4 times larger than all the others

That is, the absolute value of flex-grow does not determine the exact width. It determines its degree of “greediness” in relation to other flex blocks of the same level.

flex-shrink – the “compressibility” factor of a single flex block

Determines how much the flex block will shrink relative to adjacent elements inside the flex container if there is not enough free space. Defaults to 1.

flex – shorthand for the flex-grow, flex-shrink and flex-basis properties

flex: none | [<"flex-grow"> <"flex-shrink">? || <"flex-basis"> ]

CSS

/* i.e. ... */ .my-flex-block( flex-grow:12; flex-shrink:3; flex basis: 30em; ) /* this is the same as... */ .my-flex-block( flex: 12 3 30em )

Demo for flex-grow, flex-shrink and flex-basis

align-self – alignment of a single flex block along the transverse axis.

Makes it possible to override the flex container's align-items property for an individual flex block.

Available align-self values ​​(same 5 options as align-items)

  • flex-start: the flex block is pressed to the beginning of the transverse axis
  • flex-end: the flex block is pressed against the end of the cross axis
  • center: flex block is located in the center of the cross axis
  • baseline: flex block is aligned to baseline
  • stretch (default value): The flex block is stretched to take up all available space along the cross axis, taking into account min-width / max-width if set.

order – the order of a single flex block inside a flex container.

By default, all blocks will follow each other in the order specified in the html. However, this order can be changed using the order property. It is specified as an integer and defaults to 0.

The order value does not specify the absolute position of the element in the sequence. It determines the weight of the element's position.

HTML

item1
item2
item3
item4
item5

In this case, the blocks will follow one after another along the main axis in the following order: item5, item1, item3, item4, item2

Demo for align-self and order

margin: auto vertically. Dreams Come True!

You can love Flexbox at least because the familiar horizontal alignment via margin:auto works here for verticals too!

My-flex-container ( display: flex; height: 300px; /* Or whatever */ ) .my-flex-block ( width: 100px; /* Or whatever */ height: 100px; /* Or whatever * / margin: auto; /* Magic! The block is centered vertically and horizontally */ )

Things to Remember

  1. You should not use flexbox where it is not necessary.
  2. Defining regions and changing the order of content in many cases is still useful to make dependent on the page structure. Think it through.
  3. Understand flexbox and know the basics. This makes it much easier to achieve the expected result.
  4. Don't forget about margins. They are taken into account when setting axis alignment. It is also important to remember that margins in flexbox do not “collapse” as they do in a normal flow.
  5. The float value of flex blocks is not taken into account and has no meaning. This can probably somehow be used for graceful degradation when switching to flexbox.
  6. Flexbox is very well suited for the layout of web components and individual parts of web pages, but has not shown its best side when laying out basic layouts (position of article, header, footer, navbar, etc.). This is still a controversial point, but this article shows the shortcomings quite convincingly xanthir.com/blog/b4580

Finally

I think that flexbox, of course, will not supplant all other layout methods, but, of course, in the near future it will occupy a worthy niche in solving a huge number of tasks. And of course, you need to try working with him now. One of the following articles will be devoted to specific examples of working with flex layout. Subscribe to news;)

Using the example of a form from a real test task, I will show you how to layout according to BEM using flexbox. You may ask: “Why is it necessary to typeset according to BEM + Flexbox?" This requirement comes from the employer. Quote from the statement of work: "Try to typesetting without frameworks (preferably on flexbox), simple and clear: avoid cumbersome structures and unnecessary code, use the methodology BEM."

Fragment of a section layout with a form

My layout rules

  1. Divide the layout into logical sections
  2. Start each section with a tag section
  3. Separate sections and CSS ruled from each other, empty line
  4. Assign a class to each tag
  5. The name of the class for a block or element, answers the question - What is this?
  6. The modifier name answers the question - Which?

HTML markup

First, I make the markup, determine the nesting of blocks and come up with names for classes. In the code below, we have two inline tags - h2 And input. Inline tags are a headache and a cause of stress for beginning layout designers. Why? They behave very badly - they try to take up the entire available width, and do not allow you to set the background color and dimensions of the entire block.



personal information











What does a bad layout designer do in this case? It wraps inline elements in block tags div and sets all the necessary properties to the wrapper tag. Child inline elements inherit these properties. Is it worth making a fuss of extra code? What would a competent layout designer do? It will redefine the inline element to block or inline-block, in CSS rules

Display: block; // for the input tag
display: inline-block; // for h2 tag

Nesting logic and block names

We see a section with personal information, so we call the section class - info. A section consists of three child elements:

Icon // class name info__icon
title // info__title
form // info__form

The point is in the names of the classes BEM, consists in the belonging of a child element to a parent. The element cannot be named icon. This is not just any icon, but an icon from the section info.

Daughter and parent rolled into one

Block info__form, we have a special one - it is nested in the section info, but at the same time, contains form fields. The name of this phenomenon is multi-level nesting. The block with the form has a purely wrapper function for inputs, so that you can easily set external margins. After all, lowercase inputs behave like children (which they are), they don’t obey at all. Moreover, the second input is shorter than all the others and differs only in width.

We set one class for all inputs with the same properties, except width - info__input. For regular inputs, add a modifier info__input_long, and the short input is a modifier info__input_short. Let me remind you that the modifier BEM, should answer the question - Which?









CSS rules for modifiers

.info__input_long(
width: 520px;
}

Info__input_short(
width: 320px;
}

CSS code

IN HTML markup creates a rough structure of the site, in CSS, we are already placing the elements according to the layout. Today, we won't touch appearance, but let's focus on the positioning of elements. In our section, there are two flex containers. It must be said that the use of flexes, when all elements are located one at a time on a line, is very doubtful. The only benefit received is the property justify-content, center aligned, flex elements. I can say in my defense that the idea of ​​flexes is pointless in the context of this section only. A real layout for layout usually has more variety.

Info (
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
margin-top: 77px;
height: 100%;
}

Info__form(
display: flex;
justify-content: center;
flex-direction: column;
height: 100%;
margin-top: 50px;

Nothing stands still, technologies and standards are developing, new techniques and methods for website layout are appearing, for example, layout with tabular elements, which we did not consider for objective reasons, has been replaced by layout with floating elements.

Many code editors have a convenient quick markup plugin built in or available for download by default, Emmet, which allows you to do the basic markup of this example as follows: section>div*3>lorem+ Tab (value lorem generates the text that appears in the image below).

Please note that without any special effort, we got the columns of our layout to be the same height regardless of their content, and this is great. Elements

are not flex elements by default and are located in the flow third element

like regular block elements. If there is a need for this, then to make them flex elements, as you can guess, you need to set their parent to be a block (flex) or inline (inline-flex) flex container, but more on that in the next example.

The result of our example:

Inline flex container

By analogy with block elements, flex containers can be inline. Let's look at the difference between inline flex containers and block ones. In the previous example, we looked at the use of a block-level container, like a regular block-level element it occupies the entire width of the screen, and behaves like a regular block-level element. As for inline flex containers, they become regular inline elements, while maintaining the flexibility of the elements.

In the next example we will look at this difference, since the previous example would not be indicative, for the reason that the size of the child flex elements was not explicitly given, and as a result, our container, whether it was inline or block, would behave the same and take up would be the entire width of the screen.

The difference between inline-flex containers and flex containers.

In this example we have placed two lowercase and two block flex containers, inside them we placed five elements

, which automatically became flex elements. In addition, for them we specified a width and height equal to 50 pixels.

To quickly generate a similar layout using Emmet type the following in the editor: div.inline-flex*2>div*5 + Tab , and the same thing only with another class div.flex*2>div*5 + Tab .

Taking a look at the result of our example, the difference between inline and block flex containers should now be obvious to you. An inline container behaves like an inline element and occupies the width required only by its child flex elements, while a block flex container, despite the size of its child flex elements, occupies the entire width of the screen.

The result of our example:

Rice. 205 An example of the difference between inline-flex containers and flex containers.

Direction? Which direction?

The direction of flex elements is formed based on the position of two axes: main axis flex container and its transverse axis, which is always located perpendicular to the main. Main axis in direction ltr (global HTML attribute dir , or the CSS direction property with the value ltr ) is positioned from left to right, and the cross one is from top to bottom (this is the default value), for the rtl value it is displayed in a mirror accordingly.

Did you like the article? Share it