Positioning items in CSS can be one of the most challenging aspects of web design, due to many different screen sizes and alignments. Flexbox is an efficient way of managing webpage layout and aligning elements.
The main idea is to give the outside container the ability to alter the width/height of its elements in order to best fill the available space (this can allow webpages to work well on different devices and screen sizes!)
Before we get started, its important to understand a few key terms that will be used to describe these processes.
parent and child - you may have already heard these terms. A child is any element that belongs to another element, which would be its parent element, for example the div with the id “mySubDiv1” is a child of its parent div, “myDiv”.
When using flexbox, there is a parent element, called the flex container, and child elements, called the flex items.
Properties for the parent (flex container):
Our parent and child elements for our flexbox layout can first be created in HTML, like this:
<div class="flex-container">
<div class="flex-items" id="number1" >1</div>
<div class ="flex-items">2</div>
<div class ="flex-items">3</div>
<div class ="flex-items">4</div>
<div class ="flex-items">5</div>
</div>
.flex-container {
display: flex;
}
We will go through these properties, showing codepen examples for each one.
For each example, we will use a styling for the flex items and the flex container that make it easy to see what is happening.
See the Pen 5.6 Example by LSU DDEM (@lsuddem) on CodePen.
See the Pen 5.6 Example by LSU DDEM (@lsuddem) on CodePen.
See the Pen 5.6 Example_1 by LSU DDEM (@lsuddem) on CodePen.
The justify-content property is used to align the flex items along the “main axis” (in a row layout, this is the x axis, in a column layout, its the y axis!) It will move all of the flex items to the the center, or to the left, or make space between them etc.
The center value aligns the flex items at the center of the container.
The flex-start value aligns the flex items at the beginning of the container (this is default).
The flex-end value aligns the flex items at the end of the container.
The space-around value displays the flex items with space before, between, and after the lines.
The space-between value displays the flex items with space between the lines.
See the Pen 5.6 Example_2 by LSU DDEM (@lsuddem) on CodePen.
The align-content property is used to align the flex items along the cross axis. (the other axis that isn’t the main axis!)
The space-between value displays the flex lines with equal space between them.
The space-around value displays the flex lines with space before, between, and after them.
The stretch value stretches the flex lines to take up the remaining space (this is default).
The center value displays display the flex lines in the middle of the container.
The flex-start value displays the flex lines at the start of the container.
The flex-end value displays the flex lines at the end of the container.
See the Pen 5.6 Example_3 by LSU DDEM (@lsuddem) on CodePen.
The align-items property is used to align the flex items with themselves. This will only make a difference if some of the flex items are different sizes.
The center value aligns the flex items in the middle of the container.
The flex-start value aligns the flex items at the top of the container.
The flex-end value aligns the flex items at the bottom of the container.
The stretch value stretches the flex items to fill the container (this is default).
See the Pen 5.6 Example_4 by LSU DDEM (@lsuddem) on CodePen.
As well as flex container properties, there are a number of flex item properties that can be used to control the layout of the flex items.
For these examples, we will use a different layout. The styling for the outside container will be the same for each one.
See the Pen 5.6 Example_6 by LSU DDEM (@lsuddem) on CodePen.
The flex-grow property specifies how much a flex item will grow relative to the rest of the flex items.
The value must be a number, the default value is 0.
If we give the third item a flex-grow value of 1, it will be larger than the other two items.
See the Pen 5.6 Example_6 by LSU DDEM (@lsuddem) on CodePen.
Read about more properties here: https://www.w3schools.com/css/css3_flexbox_items.asp
See the Pen Exercise 5.6 by LSU DDEM (@lsuddem) on CodePen.