Learn Responsive Web Design by Building a Piano, Completed
- Responsive Design tells your webpage how it should look on different-sized screens.
- In this course, you'll use CSS and Responsive Design to code a piano. You'll also learn more about media queries and pseudo selectors.
Step 1
Begin with the basic HTML structure. Add aDOCTYPE
declaration andhtml
,head
,body
, andtitle
elements.
Set the language of this page to English. Set thetitle
toPiano
.
#index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Piano</title>
</head>
<body></body>
</html>
Step 2
Add twometa
tags, one to optimize your page for mobile devices, and one to specify an acceptedcharset
for the page.
#index.html
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Step 3
Time to start working on the piano. Create adiv
element within yourbody
element with the id set topiano
.
#index.html
<div id="piano">
Step 4
Nest a seconddiv
within your existingdiv
, and set theclass
to bekeys
.
#index.html
<div id="piano">
<div class="keys"></div>
</div>
Step 5
Within your.keys
element, add sevendiv
elements. Give them all the classkey
.
#index.html
<div id="piano">
<div class="keys">
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
<div class="key"></div>
</div>
</div>
Step 6
Remember that aclass
attribute can have multiple values. To separate your white keys from your black keys, you'll add a secondclass
value ofblack--key
. Add this to your second, third, fifth, sixth, and seventh.key
elements.
#index.html
<div id="piano">
<div class="keys">
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
</div>
</div>
Step 7
Now copy the set of seven.key
elements, and paste two more sets into the.keys
div.
#index.html
<div id="piano">
<div class="keys">
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
<div class="key black--key"></div>
</div>
</div>
Step 8
Add alink
element within yourhead
element. For thatlink
element, set therel
attribute tostylesheet
and thehref
to./styles.css
.
#index.html
<link rel="stylesheet" href="./styles.css" />
Step 9
Browsers can apply default margin and padding values to specific elements. To make sure your piano looks correct, you need to reset the box model.
Add anhtml
rule selector to your CSS file, and set thebox-sizing
property toborder-box
.
#styles.css
html {
box-sizing: border-box;
}
Step 10
Now that you have reset thehtml
box model, you need to pass that on to the elements within as well. To do this, you can set thebox-sizing
property toinherit
, which will tell the targeted elements to use the same value as the parent element.
You will also need to target the pseudo-elements, which are special keywords that follow a selector. The two pseudo-elements you will be using are the::before
and::after
pseudo-elements.
The::before
selector creates a pseudo-element which is the first child of the selected element, while the::after
selector creates a pseudo-element which is the last child of the selected element. These pseudo-elements are often used to create cosmetic content, which you will see later in this project.
For now, create a CSS selector to target all elements with*
, and include the pseudo-elements with::before
and::after
. Set thebox-sizing
property toinherit
.
#styles.css
*, *::before, *::after {
box-sizing: inherit;
}
Step 11
Now target your#piano
element with anid
selector. Setbackground-color
property to#00471b
, thewidth
property to992px
and theheight
property to290px
.
#styles.css
#piano {
background-color: #00471b;
width: 992px;
height: 290px;
}
Step 12
Set themargin
of the#piano
to80px auto
.
#styles.css
#piano {
background-color: #00471b;
width: 992px;
height: 290px;
margin: 80px auto;
}
Step 13
Time to style the keys. Below the#piano
rule, target the.keys
element with aclass
selector. Give the new rule abackground-color
property of#040404
, awidth
property of949px
and aheight
property of180px
.
#styles.css
.keys {
background-color: #040404;
width: 949px;
height: 180px;
}
Step 14
Give the.keys
apadding-left
of2px
.
#styles.css
.keys {
background-color: #040404;
width: 949px;
height: 180px;
padding-left: 2px;
}
Step 15
Move the keys into position by adjusting the#piano
selector. Set thepadding
property to90px 20px 0 20px
.
#styles.css
#piano {
background-color: #00471b;
width: 992px;
height: 290px;
margin: 80px auto;
padding: 90px 20px 0 20px;
}
Step 16
Time to style the keys themselves. Create aclass
selector for the.key
elements. Set thebackground-color
set to the value#ffffff
, theposition
property torelative
, thewidth
property to41px
, and theheight
property to175px
.
#styles.css
.key {
background-color: #ffffff;
position: relative;
width: 41px;
height: 175px;
}
Step 17
Give the.key
amargin
of2px
and afloat
property set toleft
.
#styles.css
.key {
background-color: #ffffff;
position: relative;
width: 41px;
height: 175px;
margin: 2px;
float: left;
}
Step 18
Now it is time to use the pseudo-selectors you prepared for earlier. To create the black keys, add a new.key.black--key::after
selector. This will target the elements with the classkey black--key
, and select the pseudo-element after these elements in the HTML.
In the new selector, set thebackground-color
to#1d1e22
. Also set thecontent
property to""
. This will make the pseudo-elements empty.
Thecontent
property is used to set or override the content of the element. By default, the pseudo-elements created by the::before
and::after
pseudo-selectors are empty, and the elements will not be rendered to the page. Setting thecontent
property to an empty string""
will ensure the element is rendered to the page while still being empty.
If you would like to experiment, try removing thebackground-color
property and setting different values for thecontent
property, such as"♥"
. Remember to undo these changes when you are done so the tests pass.
#styles.css
.key.black--key::after {
background-color: #1d1e22;
content: "";
}
Step 19
Give the.key.black--key::after
aposition
property set toabsolute
and aleft
property set to-18px
.
#styles.css
.key.black--key::after {
background-color: #1d1e22;
content: "";
position: absolute;
left: -18px;
}
Step 20
For the.key.black--key::after
, set thewidth
to32px
and theheight
to100px
.
#styles.css
.key.black--key::after {
background-color: #1d1e22;
content: "";
position: absolute;
left: -18px;
width: 32px;
height: 100px;
}
Step 21
The piano needs the freeCodeCamp logo to make it official.
Add animg
element before your.keys
element. Give theimg
aclass
oflogo
, and set thesrc
tohttps://cdn.freecodecamp.org/platform/universal/fcc_primary.svg
. Give it analt
text offreeCodeCamp Logo
.
#index.html
<div id="piano">
<img class="logo" src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" alt="freeCodeCamp Logo" />
<div class="keys">
Step 22
Start styling the logo by creating a.logo
selector. Set thewidth
to200px
, aposition
ofabsolute
and atop
set to23px
.
#styles.css
.logo {
width: 200px;
position: absolute;
top: 23px;
}
Step 23
Theimg
element needs its parent to have aposition
set as a point of reference. Set theposition
of the#piano
selector torelative
.
#styles.css
#piano {
background-color: #00471b;
width: 992px;
height: 290px;
margin: 80px auto;
padding: 90px 20px 0 20px;
position: relative;
}
Step 24
To smooth the sharp edges of the piano and keys, start by giving the#piano
aborder-radius
of10px
.
#styles.css
#piano {
background-color: #00471b;
width: 992px;
height: 290px;
margin: 80px auto;
padding: 90px 20px 0 20px;
position: relative;
border-radius: 10px;
}
Step 25
Give the.key
selector aborder-radius
value of0 0 3px 3px
.
#styles.css
.key {
background-color: #ffffff;
position: relative;
width: 41px;
height: 175px;
margin: 2px;
float: left;
border-radius: 0 0 3px 3px;
}
Step 26
Give the.key.black--key::after
selector aborder-radius
of0 0 3px 3px
to match the keys.
#styles.css
.key.black--key::after {
background-color: #1d1e22;
content: "";
position: absolute;
left: -18px;
width: 32px;
height: 100px;
border-radius: 0 0 3px 3px;
}
Step 27
The@media
at-rule, also known as a media query, is used to conditionally apply CSS. Media queries are commonly used to apply CSS based on the viewport width using themax-width
andmin-width
properties.
In the below example the padding is applied to the.card
class when the viewport is960px
wide and below.#styles.css @media (max-width: 960px) { .card { padding: 2rem; } }Add a media query that will be applied when the viewport is
768px
wide and below.
#styles.css
@media (max-width: 768px) {
}
Step 28
Add a new#piano
selector within your@media
query, and set thewidth
to358px
.
#styles.css
@media (max-width: 768px) {
#piano {
width: 358px;
}
}
Step 29
Within the@media
query, add a.keys
selector and set thewidth
to318px
.
#styles.css
@media (max-width: 768px) {
#piano {
width: 358px;
}
.keys {
width: 318px;
}
}
Step 30
Now add a.logo
selector to the@media
query, and set thewidth
property to150px
.
#styles.css
@media (max-width: 768px) {
#piano {
width: 358px;
}
.keys {
width: 318px;
}
.logo {
width: 150px;
}
}
Step 31
You might have noticed the keys collapse when the browser window is smaller than768px
. Setoverflow
tohidden
in the first.keys
selector, to take care of this issue. This property will hide any element that is pushed outside the setwidth
value of.keys
.
#styles.css
.keys {
background-color: #040404;
width: 949px;
height: 180px;
padding-left: 2px;
overflow: hidden;
}
Step 32
Add another@media
rule to apply if the browser window is bigger than769px
but smaller than1199px
.
#styles.css
@media (min-width: 769px) and (max-width: 1199px) {
}
Step 33
For the new@media
rule, set thewidth
of the#piano
to675px
and thewidth
of the.keys
to633px
.
With that, your piano is complete!
#styles.css
@media (max-width: 1199px) and (min-width: 769px) {
#piano {
width: 675px;
}
.keys {
width: 633px;
}
}