In this activity, youβll practice creating layouts with CSS Flexbox. Youβll make two CSS Flexbox containers using the flex-direction
property.
In this step, youβll build the HTML for our two separate flex layouts.
Open the file index.html
.
Create a div
with the class flex_item
inside the div
with an id
of column
.
Add lorem ipsum text within this newly created div
.
Use this text: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Your div
should look like this:
<!-- This div has the id of column and the class of flex. -->
<div id="column" class="flex">
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
</div>
Create another div
with the class flex_item inside the div
with an id of row.
*Add more lorem ipsum text within this newly created div
.
Copy the flex_item
div
you just created and paste it twice within its parent div
.
You should now have a total of three div
s within each parent div
. Your column div
should look like this:
<!-- This div has the id of column and the class of flex. -->
<div id="column" class="flex">
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
</div>
Your row div
should look like this:
<div id="row" class="flex">
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
</div>
</div>
In this step, youβll turn items into flex containers and set their flex direction.
Open the index.html
file in a browser.
Open the index.css
file located in 01_Your_First_Flex_Layout/css
.
Add the display: flex;
property to the .flex
selector.
*This property will define any HTML element with the flex class to become a flexible parent container.
flex-direction: column;
property to the #column
selector.*The flex-direction
property will define the direction of an item stacked within its parent container (which must be set to display: flex;).
Add the flex-direction: row;
property to the #row
selector.
Save the changes to index.css
, then refresh index.html
in your browser.
Your result should resemble the following image, but itβs OK if it doesnβt:
01-Stu-Your-First-Flex-Layout
βββ css
β βββ index.css
βββ index.html
<!DOCTYPE html>
<html lang="en-gb">
<head>
<link href="css/index.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="column" class="flex">
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div id="row" class="flex">
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div class="flex_item">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</body>
</html>
.flex_item {
padding:10px;
margin: 10px;
background-color: orange;
color: white;
}
.flex {
display: flex;
padding: 10px;
margin: 10px;
}
#column {
background-color: #E6F5FF;
flex-direction: column;
}
#row {
background-color: #FFF7DE;
flex-direction: row;
}