In this tutorial, we aim to provide an introductory guide to CSS Flexbox, a powerful layout model which allows for flexible and efficient alignment, distribution and sizing of elements within a container.
By the end of this tutorial, you will learn:
If you have a basic understanding of HTML and CSS, you're good to go.
CSS Flexbox, or Flexible Box, is a layout model that allows you to control the alignment, direction, order, and size of boxes within a container, even when their sizes are unknown or dynamic. It's a great tool for building responsive designs.
To create a Flex container, you simply need to select an element and set its display property to flex
.
.container {
display: flex;
}
Any child elements of the .container
will now become flex items.
By default, the flex items are displayed in a row, but you can change this using the flex-direction
property.
.container {
display: flex;
flex-direction: column;
}
This will stack the flex items vertically.
Here is a simple example of a Flexbox layout.
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
.container {
display: flex;
}
.box {
border: 1px solid black;
padding: 10px;
}
The CSS above will horizontally align the three .box
divs within the .container
div.
justify-content
The justify-content
property aligns items along the main axis of the container.
.container {
display: flex;
justify-content: center;
}
This will center the flex items within the container.
In this tutorial, you have learned the basics of CSS Flexbox, including how to create a flex container, control the direction of flex items, and align them using justify-content
.
To continue learning, you should experiment with other Flexbox properties, such as align-items
, flex-wrap
, and flex-grow
.
You can check your solutions and get more practice with the Flexbox Froggy game. Happy coding!