The "align-content" property in the Flexbox model allows controlling the distribution along the cross axis for multiple rows of elements in a container. It only works when elements occupy more space than is available on the cross axis.
Here are some examples of "align-content" property values:
An example of using "align-content" could be a situation where we have a Flexbox container with multiple rows of elements and want to control their distribution along the cross axis. We can choose the appropriate "align-content" value to achieve the desired layout that is aesthetic and functional for the given web page.
In this exercise, you will learn how to use the
align-content property in the Flexbox model to precisely control the distribution of elements along the cross axis for multiple rows of elements in a container. Your task will be to create a web page with containers that are arranged in different layouts using this 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>Aligning Elements with align-content</title>
8</head>
9<body>
10 <section class="container flex-start">
11 <div>Element 1</div>
12 <div>Element 2</div>
13 <div>Element 3</div>
14 <div>Element 4</div>
15 <div>Element 5</div>
16 </section>
17 <section class="container flex-end">
18 <div>Element 1</div>
19 <div>Element 2</div>
20 <div>Element 3</div>
21 <div>Element 4</div>
22 <div>Element 5</div>
23 </section>
24 <section class="container center">
25 <div>Element 1</div>
26 <div>Element 2</div>
27 <div>Element 3</div>
28 <div>Element 4</div>
29 <div>Element 5</div>
30 </section>
31 <section class="container space-between">
32 <div>Element 1</div>
33 <div>Element 2</div>
34 <div>Element 3</div>
35 <div>Element 4</div>
36 <div>Element 5</div>
37 </section>
38 <section class="container space-around">
39 <div>Element 1</div>
40 <div>Element 2</div>
41 <div>Element 3</div>
42 <div>Element 4</div>
43 <div>Element 5</div>
44 </section>
45 <section class="container space-evenly">
46 <div>Element 1</div>
47 <div>Element 2</div>
48 <div>Element 3</div>
49 <div>Element 4</div>
50 <div>Element 5</div>
51 </section>
52</body>
53</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-direction: column;
4 align-items: center;
5 gap: 20px;
6 margin: 0;
7 padding: 20px;
8}
9
10.container {
11 display: flex;
12 flex-wrap: wrap;
13 border: 1px solid #000;
14 padding: 10px;
15 width: 80%;
16 height: 200px;
17 margin: 10px 0;
18}
19
20.container div {
21 background-color: lightblue;
22 margin: 5px;
23 padding: 20px;
24 text-align: center;
25 flex: 1 1 30%;
26}align-content: flex-startAdd a style for the
.flex-start class to position elements at the start of the cross axis.1/* Layout with align-content: flex-start */
2.flex-start {
3 align-content: flex-start;
4}align-content: flex-endAdd a style for the
.flex-end class to position elements at the end of the cross axis.1/* Layout with align-content: flex-end */
2.flex-end {
3 align-content: flex-end;
4}align-content: centerAdd a style for the
.center class to center elements along the cross axis.1/* Layout with align-content: center */
2.center {
3 align-content: center;
4}align-content: space-betweenAdd a style for the
.space-between class to evenly distribute elements on the cross axis with equal spacing between them.1/* Layout with align-content: space-between */
2.space-between {
3 align-content: space-between;
4}align-content: space-aroundAdd a style for the
.space-around class to evenly distribute elements on the cross axis with equal spacing around them.1/* Layout with align-content: space-around */
2.space-around {
3 align-content: space-around;
4}align-content: space-evenlyAdd a style for the
.space-evenly class to evenly distribute elements on the cross axis with equal spacing between them and the container edges.1/* Layout with align-content: space-evenly */
2.space-evenly {
3 align-content: space-evenly;
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>Aligning Elements with align-content</title>
8</head>
9<body>
10 <section class="container flex-start">
11 <div>Element 1</div>
12 <div>Element 2</div>
13 <div>Element 3</div>
14 <div>Element 4</div>
15 <div>Element 5</div>
16 </section>
17 <section class="container flex-end">
18 <div>Element 1</div>
19 <div>Element 2</div>
20 <div>Element 3</div>
21 <div>Element 4</div>
22 <div>Element 5</div>
23 </section>
24 <section class="container center">
25 <div>Element 1</div>
26 <div>Element 2</div>
27 <div>Element 3</div>
28 <div>Element 4</div>
29 <div>Element 5</div>
30 </section>
31 <section class="container space-between">
32 <div>Element 1</div>
33 <div>Element 2</div>
34 <div>Element 3</div>
35 <div>Element 4</div>
36 <div>Element 5</div>
37 </section>
38 <section class="container space-around">
39 <div>Element 1</div>
40 <div>Element 2</div>
41 <div>Element 3</div>
42 <div>Element 4</div>
43 <div>Element 5</div>
44 </section>
45 <section class="container space-evenly">
46 <div>Element 1</div>
47 <div>Element 2</div>
48 <div>Element 3</div>
49 <div>Element 4</div>
50 <div>Element 5</div>
51 </section>
52</body>
53</html>1body {
2 display: flex;
3 flex-direction: column;
4 align-items: center;
5 gap: 20px;
6 margin: 0;
7 padding: 20px;
8}
9
10.container {
11 display: flex;
12 flex-wrap: wrap;
13 border: 1px solid #000;
14 padding: 10px;
15 width: 80%;
16 height: 200px;
17 margin: 10px 0;
18}
19
20.container div {
21 background-color: lightblue;
22 margin: 5px;
23 padding: 20px;
24 text-align: center;
25 flex: 1 1 30%;
26}
27
28/* Layout with align-content: flex-start */
29.flex-start {
30 align-content: flex-start;
31}
32
33/* Layout with align-content: flex-end */
34.flex-end {
35 align-content: flex-end;
36}
37
38/* Layout with align-content: center */
39.center {
40 align-content: center;
41}
42
43/* Layout with align-content: space-between */
44.space-between {
45 align-content: space-between;
46}
47
48/* Layout with align-content: space-around */
49.space-around {
50 align-content: space-around;
51}
52
53/* Layout with align-content: space-evenly */
54.space-evenly {
55 align-content: space-evenly;
56}align-content values to better understand how the different options work.After completing this exercise, you should better understand how to use the
align-content property in the Flexbox model to precisely control the distribution of elements along the cross axis.