Unit 6 Website Development - Tutorial 3
3.1 Build a Flex Section
First add the HTML in small steps. Then style the section afterwards using small CSS chunks.
How To Use This Tutorial
Work through each step in order. Add the code shown, check the expected output, then move to the next step. The final code is visible at the bottom so you can compare your finished work.
Steps
- Create the project files
- Add the basic HTML page structure
- Link the external CSS file
- Add the main area HTML
- Add the section HTML
- Add the heading and paragraph HTML
- Style the body
- Make main use flex
- Add padding inside main
- Make the section use flex
- Style the section box
- Remove the heading margin
- Remove the paragraph margin
Step 1 Create the project files
Code
flex-section
|
|-- index.html
`-- style.css
What this does
- flex-section is the project folder.
- index.html will contain the webpage structure.
- style.css will contain the CSS styling rules.
Expected output
Step 2 Add the basic HTML page structure
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex Section</title>
</head>
<body>
</body>
</html>
What this does
- DOCTYPE html tells the browser that this is an HTML5 webpage.
- head stores information about the webpage.
- body is where the visible webpage content will go.
Expected output
Step 3 Link the external CSS file
Code
<head>
<meta charset="UTF-8">
<title>Flex Section</title>
<link rel="stylesheet" href="style.css">
</head>
What this does
- link connects the HTML file to another file.
- rel tells the browser that the linked file is a stylesheet.
- href tells the browser the name of the CSS file.
Expected output
Step 4 Add the main area HTML
Code
<main>
</main>
What this does
- main creates the main content area of the webpage.
- main will hold the flex section in this tutorial.
Expected output
Step 5 Add the section HTML
Code
<section class="alert-section">
</section>
What this does
- section creates a separate section of content inside main.
- class gives the section a name so CSS can style it later.
- alert-section is the class name used for this component.
Expected output
Step 6 Add the heading and paragraph HTML
Code
<h2>Section 1</h2>
<p>This section uses flex to place the content neatly inside the main area.</p>
What this does
- h2 creates the heading inside the section.
- p creates the paragraph text inside the section.
- Text content is now visible, but it has not been styled yet.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 7 Style the body
Code
body {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
font-family: Arial, sans-serif;
background-color: #f2f2f2;
color: #222222;
}
What this does
- body selects the whole visible webpage.
- margin-top removes the browser gap at the top.
- font-family changes the font used on the webpage.
- background-color changes the page background to light grey.
- color changes the normal text colour to dark grey.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 8 Make main use flex
Code
main {
display: flex;
justify-content: center;
}
What this does
- main selects the main content area.
- display turns the main area into a flex container.
- justify-content controls how content is positioned across the main axis.
- center places the section in the centre horizontally.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 9 Add padding inside main
CSS to add inside the main rule
main {
padding-top: 30px;
padding-right: 30px;
padding-bottom: 30px;
padding-left: 30px;
}
What this does
- padding-top adds space inside the top of main.
- padding-right adds space inside the right side of main.
- padding-bottom adds space inside the bottom of main.
- padding-left adds space inside the left side of main.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 10 Make the section use flex
Code
.alert-section {
display: flex;
flex-direction: column;
gap: 10px;
}
What this does
- .alert-section selects the section with the class name alert-section.
- display turns the section into a flex container.
- flex-direction controls the direction of the content inside the section.
- column places the heading above the paragraph.
- gap adds space between the heading and paragraph.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 11 Style the section box
CSS to add inside the .alert-section rule
.alert-section {
background-color: #ffe5e5;
border-width: 2px;
border-style: solid;
border-color: red;
padding-top: 20px;
padding-right: 20px;
padding-bottom: 20px;
padding-left: 20px;
width: 80%;
border-radius: 8px;
}
What this does
- background-color changes the section background to pale red.
- border-width sets the thickness of the border.
- border-style makes the border a solid line.
- border-color changes the border colour to red.
- padding adds space inside the section.
- width makes the section take up 80 percent of main.
- border-radius rounds the corners of the section.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 12 Remove the heading margin
Code
.alert-section h2 {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
}
What this does
- .alert-section h2 selects only the heading inside the alert section.
- margin-top removes extra space above the heading.
- margin-bottom removes extra space below the heading.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Step 13 Remove the paragraph margin
Code
.alert-section p {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
}
What this does
- .alert-section p selects only the paragraph inside the alert section.
- margin-top removes extra space above the paragraph.
- margin-bottom removes extra space below the paragraph.
Expected output
Section 1
This section uses flex to place the content neatly inside the main area.
Final code
Final index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex Section</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<section class="alert-section">
<h2>Section 1</h2>
<p>This section uses flex to place the content neatly inside the main area.</p>
</section>
</main>
</body>
</html>
Final style.css
body {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
font-family: Arial, sans-serif;
background-color: #f2f2f2;
color: #222222;
}
main {
display: flex;
justify-content: center;
padding-top: 30px;
padding-right: 30px;
padding-bottom: 30px;
padding-left: 30px;
}
.alert-section {
display: flex;
flex-direction: column;
gap: 10px;
background-color: #ffe5e5;
border-width: 2px;
border-style: solid;
border-color: red;
padding-top: 20px;
padding-right: 20px;
padding-bottom: 20px;
padding-left: 20px;
width: 80%;
border-radius: 8px;
}
.alert-section h2 {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
}
.alert-section p {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
}