Unit 6 Website Development - Tutorial 4
4.2 Build an Image Card
First add the HTML in small steps. Then style the image card 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 image card section HTML
- Add the image HTML
- Add the card heading HTML
- Add the card paragraph HTML
- Add the card button link HTML
- Style the body
- Centre the card inside main
- Add height and padding to main
- Make the image card use flex
- Style the image card box
- Style the card image
- Style the card heading
- Style the card paragraph
- Style the card button link
- Add the card button hover effect
Step 1 Create the project files
Code
image-card
|
|-- index.html
|-- style.css
`-- image.png
What this does
- image-card is the project folder.
- index.html will contain the webpage structure.
- style.css will contain the CSS styling rules.
- image.png is the image file used in the card.
Expected output
Step 2 Add the basic HTML page structure
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Card</title>
</head>
<body>
</body>
</html>
What this does
- DOCTYPE html tells the browser 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>Image Card</title>
<link rel="stylesheet" href="style.css">
</head>
What this does
- link connects the HTML file to another file.
- rel tells the browser the linked file is a stylesheet.
- href tells the browser the CSS file is called style.css.
Expected output
Step 4 Add the main area HTML
Code
<main>
</main>
What this does
- main creates the main content area.
- main will hold the image card.
Expected output
Step 5 Add the image card section HTML
Code
<section class="image-card">
</section>
What this does
- section creates a separate content section.
- class gives the section a name so CSS can style it.
- image-card is the class name for this component.
Expected output
Step 6 Add the image HTML
Code
<img src="image.png" alt="Image icon">
What this does
- img displays an image.
- src tells the browser which image file to use.
- alt describes the image for accessibility.
Expected output
Step 7 Add the card heading HTML
Code
<h2>Image Card</h2>
What this does
- h2 creates the card heading.
- Image Card is placeholder heading text.
Expected output
Image Card
Step 8 Add the card paragraph HTML
Code
<p>This card has an image at the top, text underneath and a button at the bottom.</p>
What this does
- p creates paragraph text.
- Paragraph text explains what the card is about.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Step 9 Add the card button link HTML
Code
<a href="#">Read More</a>
What this does
- a creates a clickable link.
- href sets where the link goes.
- # keeps the link on the same page for now.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 10 Style the body
Code
body {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
font-family: Arial, sans-serif;
background-color: #eeeeee;
}
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 page.
- background-color changes the page background to light grey.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 11 Centre the card inside main
Code
main {
display: flex;
justify-content: center;
align-items: center;
}
What this does
- main selects the main content area.
- display turns main into a flex container.
- justify-content centres the card horizontally.
- align-items centres the card vertically when main has height.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 12 Add height and padding to main
Code
main {
min-height: 100vh;
padding-top: 30px;
padding-right: 30px;
padding-bottom: 30px;
padding-left: 30px;
}
What this does
- min-height makes main at least the full height of the browser window.
- 100vh means 100 percent of the viewport height.
- padding adds space inside main.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 13 Make the image card use flex
Code
.image-card {
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
}
What this does
- .image-card selects the card section.
- display turns the card into a flex container.
- flex-direction places the content in a vertical stack.
- gap adds space between each item.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 14 Style the image card box
Code
.image-card {
background-color: white;
border-width: 2px;
border-style: solid;
border-color: #cccccc;
border-radius: 12px;
padding-top: 20px;
padding-right: 20px;
padding-bottom: 20px;
padding-left: 20px;
width: 300px;
text-align: center;
}
What this does
- background-color changes the card background to white.
- border creates a light grey border.
- border-radius rounds the card corners.
- padding adds space inside the card.
- width sets the card width to 300 pixels.
- text-align centres the text.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 15 Style the card image
Code
.image-card img {
width: 100%;
border-radius: 10px;
}
What this does
- .image-card img selects only the image inside the image card.
- width makes the image fill the card width.
- border-radius rounds the image corners.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 16 Style the card heading
Code
.image-card h2 {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
color: #222222;
}
What this does
- .image-card h2 selects only the heading inside the card.
- margin removes extra spacing around the heading.
- color changes the heading text to dark grey.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 17 Style the card paragraph
Code
.image-card p {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
color: #555555;
line-height: 1.5;
}
What this does
- .image-card p selects only the paragraph inside the card.
- margin removes extra spacing around the paragraph.
- color changes the paragraph text to grey.
- line-height adds space between lines of text.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 18 Style the card button link
Code
.image-card a {
display: inline-block;
padding-top: 12px;
padding-right: 22px;
padding-bottom: 12px;
padding-left: 22px;
background-color: #0b7a5c;
color: white;
text-decoration: none;
border-radius: 6px;
font-weight: bold;
}
What this does
- .image-card a selects only the link inside the card.
- display lets the link behave like a button.
- padding adds space inside the button.
- background-color changes the button background to green.
- text-decoration removes the underline from the link.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreStep 19 Add the card button hover effect
Code
.image-card a:hover {
background-color: #075c45;
}
What this does
- .image-card a:hover selects the button when the mouse moves over it.
- background-color changes the button background to dark green.
- hover helps users see that the button is interactive.
Expected output
Image Card
This card has an image at the top, text underneath and a button at the bottom.
Read MoreMove your mouse over the button. It should turn dark green.
Final code
Final index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Card</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<section class="image-card">
<img src="image.png" alt="Image icon">
<h2>Image Card</h2>
<p>This card has an image at the top, text underneath and a button at the bottom.</p>
<a href="#">Read More</a>
</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: #eeeeee;
}
main {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding-top: 30px;
padding-right: 30px;
padding-bottom: 30px;
padding-left: 30px;
}
.image-card {
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
background-color: white;
border-width: 2px;
border-style: solid;
border-color: #cccccc;
border-radius: 12px;
padding-top: 20px;
padding-right: 20px;
padding-bottom: 20px;
padding-left: 20px;
width: 300px;
text-align: center;
}
.image-card img {
width: 100%;
border-radius: 10px;
}
.image-card h2 {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
color: #222222;
}
.image-card p {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
color: #555555;
line-height: 1.5;
}
.image-card a {
display: inline-block;
padding-top: 12px;
padding-right: 22px;
padding-bottom: 12px;
padding-left: 22px;
background-color: #0b7a5c;
color: white;
text-decoration: none;
border-radius: 6px;
font-weight: bold;
}
.image-card a:hover {
background-color: #075c45;
}