Complex layouts using flex css examples. What is Flexbox? Description of all css properties, basic principles, advantages and disadvantages

Complex layouts using flex css examples.  What is Flexbox?  Description of all css properties, basic principles, advantages and disadvantages
Complex layouts using flex css examples. What is Flexbox? Description of all css properties, basic principles, advantages and disadvantages

Flexboxes are ideal for creating commonly used website layouts, such as the three-column, so-called “Holy Grail” layout, where all columns must be full height, regardless of their content. At the same time, in source code the main content comes before the navigation, and on the page itself the main content comes after the navigation.

Like this.

Example

A cap
Article
Basement

Before Flexbox, this layout was quite difficult to achieve without using some hacks. Developers often had to do things like add extra markup, apply negative margins, and other tricks to ensure everything would line up correctly regardless of the amount of content or screen size. But, as the example above shows, everything turns out much simpler on Flexbox.

Here's a summary of the code. In this example, we make the #main element a flex container, and leave the header and footer as block elements. In other words, only the middle part becomes flexbox. Here is a snippet that makes it a flex container.

#main ( display: flex; min-height: calc(100vh - 40vh); )

Just use display: flex to make a flex container. Note that we also set the min-height using the calc() function and set #main to 100% of the viewport height minus height of the header and base (20vh each). This ensures that the layout will fill the entire height of the screen, even if there is little content. As a result, the footer will never rise and leave empty space below it if the content takes up less than the height of the screen.

Calculating min-height was quite simple, given that we applied box-sizing: border-box to all elements. If we didn't do this, then we would need to add the padding value to the sum to subtract.

After installing the flex container, we deal with flex elements.

#main > article ( flex: 1; ) #main > nav, #main > aside ( flex: 0 0 20vw; background: beige; ) #main > nav ( order: -1; )

The flex property is shorthand for the flex-grow, flex-shrink, and flex-basis properties. In the first case, we only wrote one value, so flex sets the flex-grow property. In the second case, we wrote all three values ​​for