Aligning and Justifying Content with Flexbox

Tutorial 3 of 5

1. Introduction

This tutorial provides you with a comprehensive guide on how to align and justify HTML elements using Flexbox, a one-dimensional layout method in CSS.

By the end of this tutorial, you will learn how to:
- Understand the basics of the Flexbox layout
- Use Flexbox properties to align and justify items in a container
- Apply these practices in real-world examples

Prerequisites:
- Basic knowledge of HTML and CSS is required.
- Familiarity with the concept of CSS box model would be beneficial.

2. Step-by-Step Guide

The Flexbox model is based on 'flex containers' and 'flex items'. A flex container expands items to fill available free space, or shrinks them to prevent overflow.

Flex Container

To make an HTML element a flex container, we use the display property.

.container {
  display: flex;
}

Aligning Items

To align items along the cross axis, we use align-items property. It accepts the following values: stretch, flex-start, flex-end, center, baseline.

.container {
  display: flex;
  align-items: center;
}

This will align all the flex items to the center of the container.

Justifying Content

To justify items along the main axis, we use justify-content property. It accepts the following values: flex-start, flex-end, center, space-between, space-around, space-evenly.

.container {
  display: flex;
  justify-content: center;
}

This will justify all the flex items to the center of the container.

3. Code Examples

Example 1: Aligning items to the center

<!-- This is your HTML -->
<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

<!-- This is your CSS -->
<style>
.container {
  display: flex; /* Makes the div a flex container */
  align-items: center; /* Aligns items vertically in the center */
}

.item {
  padding: 10px; /* Adds some space around items */
  border: 1px solid black; /* Gives each item a border */
}
</style>

Example 2: Justifying content to the center

<!-- This is your HTML -->
<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

<!-- This is your CSS -->
<style>
.container {
  display: flex; /* Makes the div a flex container */
  justify-content: center; /* Centers items horizontally */
}

.item {
  padding: 10px; /* Adds some space around items */
  border: 1px solid black; /* Gives each item a border */
}
</style>

4. Summary

In this tutorial, we learned about the Flexbox layout and how to align and justify items using align-items and justify-content properties. We also applied these concepts in practical examples.

Next steps for learning:
- Learn about other Flexbox properties like flex-wrap, flex-direction, etc.
- Practice making complex layouts using Flexbox

Additional resources:
- CSS-Tricks' A Complete Guide to Flexbox
- MDN's Basic concepts of flexbox

5. Practice Exercises

  1. Create a container with 5 items. Align the items to the start of the container.
  2. Now, justify the items to the space between.
  3. Finally, align and justify the items to the center.

Solutions and explanations:

.container {
  display: flex;
  align-items: flex-start; /* Aligns items to the start */
  justify-content: space-between; /* Distributes items evenly in the container */
}
.container {
  display: flex;
  align-items: center; /* Aligns items to the center */
  justify-content: center; /* Justifies items to the center */
}

Keep practicing these exercises until you're comfortable with aligning and justifying content in Flexbox. Happy coding!