Responsive Web Dedign(十一)
freeCodeCamp —- Responsive Web Dedign
Learn Responsive Web Design by Building a Piano
Begin with the basic HTML structure. Add a
DOCTYPE
declaration andhtml
,head
,body
, andtitle
elements.Set the language of this page to English. Set the
title
toPiano
.Add two
meta
tags, one to optimize your page for mobile devices, and one to specify an acceptedcharset
for the page.Time to start working on the piano. Create a
div
element within yourbody
element with theid
set topiano
.Nest a second
div
within your existingdiv
, and set theclass
to bekeys
.Within your
.keys
element, add sevendiv
elements. Give them all the classkey
.Remember that a
class
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.Now copy the set of seven
.key
elements, and paste two more sets into the.keys
div.Add a
link
element within yourhead
element. For thatlink
element, set therel
attribute tostylesheet
and thehref
to./styles.css
.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 an
html
rule selector to your CSS file, and set thebox-sizing
property toborder-box
.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
.Now target your
#piano
element with anid
selector. Setbackground-color
property to#00471b
, thewidth
property to992px
and theheight
property to290px
.Set the
margin
of the#piano
to80px auto
.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
.Give the
.keys
apadding-left
of2px
.Move the keys into position by adjusting the
#piano
selector. Set thepadding
property to90px 20px 0 20px
.Time to style the keys themselves. Create a
class
selector for the.key
elements. Set thebackground-color
set to the value#ffffff
, theposition
property torelative
, thewidth
property to41px
, and theheight
property to175px
.Give the
.key
amargin
of2px
and afloat
property set toleft
.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 the
background-color
to#1d1e22
. Also set thecontent
property to""
. This will make the pseudo-elements empty.The
content
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 the
background-color
property and setting different values for thecontent
property, such as"♥"
. Remember to undo these changes when you are done so the tests pass.Give the
.key.black--key::after
aposition
property set toabsolute
and aleft
property set to-18px
.For the
.key.black--key::after
, set thewidth
to32px
and theheight
to100px
.The piano needs the freeCodeCamp logo to make it official.
Add an
img
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
.Start styling the logo by creating a
.logo
selector. Set thewidth
to200px
, aposition
ofabsolute
and atop
set to23px
.The
img
element needs its parent to have aposition
set as a point of reference. Set theposition
of the#piano
selector torelative
.To smooth the sharp edges of the piano and keys, start by giving the
#piano
aborder-radius
of10px
.Give the
.key
selector aborder-radius
value of0 0 3px 3px
.Give the
.key.black--key::after
selector aborder-radius
of0 0 3px 3px
to match the keys.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.1
2
3
4
5@media (max-width: 960px) {
.card {
padding: 2rem;
}
}Add a media query that will be applied when the viewport is
768px
wide and below.Add a new
#piano
selector within your@media
query, and set thewidth
to358px
.Within the
@media
query, add a.keys
selector and set thewidth
to318px
.Now add a
.logo
selector to the@media
query, and set thewidth
property to150px
.You might have noticed the keys collapse when the browser window is smaller than
768px
. 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
.Logical operators can be used to construct more complex media queries. The
and
logical operator is used to query two media conditions.For example, a media query that targets a display width between 500px and 1000px would be:
1
2
3@media (min-width: 500px) and (max-width: 1000px){
}Add another
@media
rule to apply if the browser window is wider than769px
but smaller than1199px
.For the new
@media
rule, set thewidth
of the#piano
to675px
and thewidth
of the.keys
to633px
.With that, your piano is complete!
1 |
|
1 | html { |