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.
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.
To make an HTML element a flex container, we use the display
property.
.container {
display: flex;
}
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.
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.
<!-- 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>
<!-- 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>
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
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!