Responsive Web Dedign(十一)
freeCodeCamp —- Responsive Web Dedign
Learn Responsive Web Design by Building a Piano
Begin with the basic HTML structure. Add a
DOCTYPEdeclaration andhtml,head,body, andtitleelements.Set the language of this page to English. Set the
titletoPiano.Add two
metatags, one to optimize your page for mobile devices, and one to specify an acceptedcharsetfor the page.Time to start working on the piano. Create a
divelement within yourbodyelement with theidset topiano.Nest a second
divwithin your existingdiv, and set theclassto bekeys.Within your
.keyselement, add sevendivelements. Give them all the classkey.Remember that a
classattribute can have multiple values. To separate your white keys from your black keys, you’ll add a secondclassvalue ofblack--key. Add this to your second, third, fifth, sixth, and seventh.keyelements.Now copy the set of seven
.keyelements, and paste two more sets into the.keysdiv.Add a
linkelement within yourheadelement. For thatlinkelement, set therelattribute tostylesheetand thehrefto./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
htmlrule selector to your CSS file, and set thebox-sizingproperty toborder-box.The
::beforeselector creates a pseudo-element which is the first child of the selected element, while the::afterselector 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::beforeand::after. Set thebox-sizingproperty toinherit.Now target your
#pianoelement with anidselector. Setbackground-colorproperty to#00471b, thewidthproperty to992pxand theheightproperty to290px.Set the
marginof the#pianoto80px auto.Time to style the keys. Below the
#pianorule, target the.keyselement with aclassselector. Give the new rule abackground-colorproperty of#040404, awidthproperty of949pxand aheightproperty of180px.Give the
.keysapadding-leftof2px.Move the keys into position by adjusting the
#pianoselector. Set thepaddingproperty to90px 20px 0 20px.Time to style the keys themselves. Create a
classselector for the.keyelements. Set thebackground-colorset to the value#ffffff, thepositionproperty torelative, thewidthproperty to41px, and theheightproperty to175px.Give the
.keyamarginof2pxand afloatproperty 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::afterselector. 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-colorto#1d1e22. Also set thecontentproperty to"". This will make the pseudo-elements empty.The
contentproperty is used to set or override the content of the element. By default, the pseudo-elements created by the::beforeand::afterpseudo-selectors are empty, and the elements will not be rendered to the page. Setting thecontentproperty 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-colorproperty and setting different values for thecontentproperty, such as"♥". Remember to undo these changes when you are done so the tests pass.Give the
.key.black--key::afterapositionproperty set toabsoluteand aleftproperty set to-18px.For the
.key.black--key::after, set thewidthto32pxand theheightto100px.The piano needs the freeCodeCamp logo to make it official.
Add an
imgelement before your.keyselement. Give theimgaclassoflogo, and set thesrctohttps://cdn.freecodecamp.org/platform/universal/fcc_primary.svg. Give it analttext offreeCodeCamp Logo.Start styling the logo by creating a
.logoselector. Set thewidthto200px, apositionofabsoluteand atopset to23px.The
imgelement needs its parent to have apositionset as a point of reference. Set thepositionof the#pianoselector torelative.To smooth the sharp edges of the piano and keys, start by giving the
#pianoaborder-radiusof10px.Give the
.keyselector aborder-radiusvalue of0 0 3px 3px.Give the
.key.black--key::afterselector aborder-radiusof0 0 3px 3pxto match the keys.The
@mediaat-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-widthandmin-widthproperties.In the below example the padding is applied to the
.cardclass when the viewport is960pxwide 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
768pxwide and below.Add a new
#pianoselector within your@mediaquery, and set thewidthto358px.Within the
@mediaquery, add a.keysselector and set thewidthto318px.Now add a
.logoselector to the@mediaquery, and set thewidthproperty to150px.You might have noticed the keys collapse when the browser window is smaller than
768px. Setoverflowtohiddenin the first.keysselector, to take care of this issue. This property will hide any element that is pushed outside the setwidthvalue of.keys.Logical operators can be used to construct more complex media queries. The
andlogical 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
@mediarule to apply if the browser window is wider than769pxbut smaller than1199px.For the new
@mediarule, set thewidthof the#pianoto675pxand thewidthof the.keysto633px.With that, your piano is complete!
1 |
|
1 | html { |