1section {
2 display: flex;
3 flex-direction: row | row-reverse | column | column-reverse;
4}
The example above illustrates the ability to specify the direction in which elements are arranged in a flex container. For the Flexbox model, there are four main layout options:
Thanks to these options, we can control the direction of element placement inside the Flexbox container, allowing flexible adaptation of the page layout to our needs.
In this exercise, you will learn how to use the
flex-direction property in the Flexbox model to control the direction of element placement in a flex container. Your task will be to create a web page with sections that are arranged in different directions using the flex-direction property.First, create a new HTML file and add a basic HTML document structure. Add a
<section> element with several <div> elements in the document body (<body>).1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <link rel="stylesheet" href="styles.css"> <!-- Link CSS file -->
7 <title>Layout in the Flexbox Model</title>
8</head>
9<body>
10 <section class="flex-row">
11 <div>Element 1</div>
12 <div>Element 2</div>
13 <div>Element 3</div>
14 </section>
15 <section class="flex-row-reverse">
16 <div>Element 1</div>
17 <div>Element 2</div>
18 <div>Element 3</div>
19 </section>
20 <section class="flex-column">
21 <div>Element 1</div>
22 <div>Element 2</div>
23 <div>Element 3</div>
24 </section>
25 <section class="flex-column-reverse">
26 <div>Element 1</div>
27 <div>Element 2</div>
28 <div>Element 3</div>
29 </section>
30</body>
31</html>Open or create a new CSS file and save it as
styles.css.Add basic styles for the
<section> and <div> elements so they are visible on the page.1body {
2 display: flex;
3 flex-wrap: wrap;
4 gap: 20px;
5 margin: 0;
6 padding: 20px;
7 justify-content: space-around;
8}
9
10section {
11 display: flex;
12 border: 1px solid #000;
13 padding: 10px;
14 width: 45%;
15 height: 200px;
16 margin: 10px 0;
17}
18
19section div {
20 background-color: lightblue;
21 margin: 5px;
22 padding: 20px;
23 text-align: center;
24 flex: 1;
25}Add a style for the
.flex-row class to arrange elements in a horizontal line from left to right.1/* Horizontal layout (row) */
2.flex-row {
3 flex-direction: row;
4}Add a style for the
.flex-row-reverse class to arrange elements in a horizontal line from right to left.1/* Reversed horizontal layout (row-reverse) */
2.flex-row-reverse {
3 flex-direction: row-reverse;
4}Add a style for the
.flex-column class to arrange elements in a vertical line from top to bottom.1/* Vertical layout (column) */
2.flex-column {
3 flex-direction: column;
4}Add a style for the
.flex-column-reverse class to arrange elements in a vertical line from bottom to top.1/* Reversed vertical layout (column-reverse) */
2.flex-column-reverse {
3 flex-direction: column-reverse;
4}After completing all the steps, your HTML and CSS files should look as follows:
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <link rel="stylesheet" href="styles.css"> <!-- Link CSS file -->
7 <title>Layout in the Flexbox Model</title>
8</head>
9<body>
10 <section class="flex-row">
11 <div>Element 1</div>
12 <div>Element 2</div>
13 <div>Element 3</div>
14 </section>
15 <section class="flex-row-reverse">
16 <div>Element 1</div>
17 <div>Element 2</div>
18 <div>Element 3</div>
19 </section>
20 <section class="flex-column">
21 <div>Element 1</div>
22 <div>Element 2</div>
23 <div>Element 3</div>
24 </section>
25 <section class="flex-column-reverse">
26 <div>Element 1</div>
27 <div>Element 2</div>
28 <div>Element 3</div>
29 </section>
30</body>
31</html>1body {
2 display: flex;
3 flex-wrap: wrap;
4 gap: 20px;
5 margin: 0;
6 padding: 20px;
7 justify-content: space-around;
8}
9
10section {
11 display: flex;
12 border: 1px solid #000;
13 padding: 10px;
14 width: 45%;
15 height: 200px;
16 margin: 10px 0;
17}
18
19section div {
20 background-color: lightblue;
21 margin: 5px;
22 padding: 20px;
23 text-align: center;
24 flex: 1;
25}
26
27/* Horizontal layout (row) */
28.flex-row {
29 flex-direction: row;
30}
31
32/* Reversed horizontal layout (row-reverse) */
33.flex-row-reverse {
34 flex-direction: row-reverse;
35}
36
37/* Vertical layout (column) */
38.flex-column {
39 flex-direction: column;
40}
41
42/* Reversed vertical layout (column-reverse) */
43.flex-column-reverse {
44 flex-direction: column-reverse;
45}flex-direction options work.After completing this exercise, you should better understand how to use the
flex-direction property in the Flexbox model to control the direction of element placement in a flex container.