Flexbox Simple Sample
First Article
. . . . . . . . . . random text . . . . . . . . . .
Second Article
. . . . . . . . . . random text . . . . . . . . . .
Third Article
. . . . . . . . . . random text . . . . . . . . . .
I
II
III
elements.
CSS
The CSS for this codepen styles all the elements. Take time to go through each line and discuss as a class or in
small groups what each line does. Have students locate flexbox lines. Flexbox lines are identified here with
comments.
section {
display: flex; /* Create the top level flex container */
background: lightgrey;
padding: 10px 10px 10px 10px;
}
article {
background: maroon;
height: 100px;
margin: 10px;
display: flex; /* Create flex container inside flex item */
4 / 16
justify-content: center; /* Center content horizontally */
align-items: center; /* Center content vertically */
}
#one {
order: 1; /* Set
flex-grow: 1; /*
}
#two {
order: 2; /* Set
flex-grow: 2; /*
}
#three {
order: 3; /* Set
flex-grow: 1; /*
}
p {
margin: 0px;
padding: 0px;
font-size: 36px;
color: white;
}
order of item */
Set size of item */
order of item */
Set size of item */
order of item */
Set size of item */
Note that both section and article are styled as flexbox containers. This means that while the section is a
flexbox container, the article is both a flexbox item inside the section and a flexbox container. The individual p
elements are items inside each article container.
Documentation
Research and figure out how to perform the following modifications to the codepen.
1. How do you change the order?
Modify the order number in the rules for #one, #two and #three.
Have students create 3 different orders.
2. How do you change the size?
Modify the flex-grow number in the rules for #one, #two and #three. The height can be changed by
modifying height in the article rule.
Have students create 3 different sizings.
3. How do you make the flexbox container order the items vertically?
Add flex-direction: column to the section rule.
Applying Flexbox
index.html
5 / 16
Flex Simple Sample
style.css
body {
font-family: sans-serif;
}
header {
background: navy;
height: 100px;
color: white;
}
h1 {
text-align: center;
line-height: 100px;
}
article {
padding: 10px;
margin: 10px;
background: lightblue;
}
Adding More Flex
Students will add the following features using flexbox:
6 / 16
Three images above the articles
Even spacing of articles
Sidebar
Footer
Create a Container for Images
Students will add sections for images and use placeholder images to test their code. The added section code is
shown in the lesson images. Here is the code that should be added between the and lines:
Even Spacing
Students will modify article CSS to make the images spaced out evenly. Students might observer that if their
randomly generated text articles are of different lengths, then they aren't evenly spaced. Emphasize that flexgrow:1 does not force article elements to be the same size, instead it equally shares the remaining space. We
will fix this in the next section.
article {
padding: 10px;
margin: 10px;
background: lightblue;
flex-grow: 1;
}
Flex Shorthand
Students will replace flex-grow: 1 declarations with the shorthand flex: 1. This gives flexbox more
responsibility to "figure out" how to space things perfectly.
7 / 16
article {
padding: 10px;
margin: 10px;
background: lightblue;
flex: 1;
}
Add a Sidebar
To add the sidebar we are first going divide the pages into
b5">5Flexbox Simple Sample
First Article
. . . . . . . . . . random text . . . . . . . . . .
Second Article
. . . . . . . . . . random text . . . . . . . . . .
Third Article
. . . . . . . . . . random text . . . . . . . . . .
elements. Surround the sections containing the
images and articles with a div of class "content".
See the in lesson animation for more detailed instructions. Create a new div above this with class "sidebar".
Add an image and a short list to the sidebar. Sidebar HTML should look like this:
Have students save and look at their page. It should look similar to the image in the lesson. The sidebar is not on
the side yet. To do this we need to put the sidebar and the content into their own flex container. Do this by
surrounding the sidebar and content divs in a element as shown in the lesson animation.
Add a Footer
Add a footer using
b4">4