Responsive Web Dedign(十五)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Transforms by Building a Penguin
You will be building a happy Flappy Penguin, and further exploring CSS transforms and animations in the process.
Begin with your basic HTML boilerplate. Include the
DOCTYPEdeclaration,htmlelement with a language set to English, the appropriatemetatags, ahead,body, andtitleelement. Also, link your stylesheet to the page.Target the
bodyelement to set thebackgroundto a linear gradient angled 45 degrees clockwise, starting atrgb(118, 201, 255)and ending atrgb(247, 255, 222).Normalise your page’s sizing, by removing the
bodyelement’smarginandpadding.Normalise your page, by setting the
widthto100%, andheightto100vh.Remove both the horizontal and vertical scrollbars, using only one property.
Within the
body, add adivwith aclassofground.Target the
.groundelement, and set itswidthto take up the full width of the viewport. Then, set theheightto400px.Give the
.groundelement abackgroundwith a linear gradient angled 90 degrees clockwise, starting atrgb(88, 175, 236)and ending atrgb(182, 255, 255).As the
.groundelement will be third in the stacking context of the page layout, set itsz-indexto3, andpositiontoabsolute.Above the
.groundelement, add adivwith aclassofpenguin. Thisdivwill contain Flappy Penguin.Target the
.penguinelement, and set itswidthandheightto300px.Use the
marginproperty to horizontally center the.penguinelement, and set themargin-topto75px.To create some scenery in the background, you will add two mountains.
Above the
.penguinelement, add adivwith aclassofleft-mountain.Target the
.left-mountainelement, and set itswidthandheightto300px. Then, set thebackgroundto a linear gradient starting atrgb(203, 241, 228)and ending atrgb(80, 183, 255).To prevent the mountain from pushing the
.groundelement, adjust itspositionto prevent it from taking up space in the page layout.To make the mountain look more like a mountain, you can use the
skewtransform function, which takes two arguments. The first being an angle to shear the x-axis by, and the second being an angle to shear the y-axis by.Use the
transformproperty to skew the mountain by0degin the x-axis and44degin the y-axis.Set the stack level of the mountain element such that it remains directly behind the
.groundelement.To overlap the mountain and
.groundelements better, give the mountain amargin-topof100px, and the.groundelement amargin-topof-58px.To give the effect of a mountain range, add another mountain, by creating a new
divimmediately after.left-mountain, and give the newdivtheclassofback-mountain.Target the
.back-mountainelement, and set itswidthandheightto300px. Then, set thebackgroundto a linear gradient starting atrgb(203, 241, 228)and ending atrgb(47, 170, 255).Set the
positionproperty of the.back-mountainto prevent it from taking up space in the page layout.Change the stack level of the
.back-mountainelement such that it is directly behind the.left-mountainelement.Rotate the
.back-mountainelement by45degclockwise. Then, give it aleftproperty of110px, and atopproperty of225px.Rotate the
.back-mountainelement by45degclockwise. Then, give it aleftproperty of110px, and atopproperty of225px.Give the
.sunelement awidthandheightof200px, and abackground-colorofyellow.Set the
positionproperty of the sun to prevent it from taking up space in the page layout, and set theborder-radiussuch that the sun’s shape is a circle.Position the sun in the top right corner of the screen such that
75pxof its top and right edges are off screen.Your penguin will consist of two main sections: the head, and the body.
Within
.penguin, add two newdivelements. The first with aclassofpenguin-head, and the second with aclassofpenguin-body.Change the stack level of the
.penguinelement such that it appears in front of the.groundelement, and give it apositionofrelative.Target the
.penguin-headelement, and give it awidthhalf of its parent’s, and aheightof45%. Then, set thebackgroundto a linear gradient at45degstarting atgray, and ending atrgb(239, 240, 228).Most penguins do not have a square head.
Give the penguin a slightly oval head by setting the radius of the top corners to
70%and the radius of the bottom corners to65%.Target the
.penguin-bodyelement, and give it awidthof53%, and aheightof45%. Then, set thebackgroundto a linear gradient at45deg,rgb(134, 133, 133)from0%,rgb(234, 231, 231)from25%, andwhitefrom67%.Another interesting fact about penguins is that they do not have square bodies.
Use the
border-radiusproperty with a value of80% 80% 100% 100%, to give the penguin a slightly rounded body.Target all descendent elements of the
.penguinelement, and give them apositionofabsolute.Position the
.penguin-headelement10%from the top, and25%from the left of its parent.Position the
.penguin-bodyelement40%from the top, and23.5%from the left of its parent.Position the
.penguin-bodyelement40%from the top, and23.5%from the left of its parent.To give the penguin body a crest, create a pseudo-element that is the first child of the
.penguin-bodyelement. Set thecontentproperty of the pseudo-element to an empty string.Position the pseudo-element relative to its closest positioned ancestor.
Give the pseudo-element a
widthhalf that of its parent, aheightof45%, and abackground-colorofgray.Position the pseudo-element
10%from the top and25%from the left of its parent.Position the pseudo-element
10%from the top and25%from the left of its parent.Increase the pseudo-element’s transparency by
30%.Start the penguin’s face, by adding two
divelements within.penguin-head, and giving them both aclassofface.Give the
.faceelements awidthof60%, aheightof70%, and abackground-colorofwhite.Make the top corners of the
.faceelements have a radius of70%, and the bottom corners have a radius of60%.Position the
.faceelements so that they are15%from the top.Currently, the two
.faceelements are on top of each other.Fix this, by adding a
classofleftto the first.faceelement, and aclassofrightto the second.faceelement.Target the
.faceelement with theleftclass, and position it5%left of its parent.Target the
.faceelement with therightclass, and position it5%right of its parent.Target the
.faceelement with therightclass, and position it5%right of its parent.Target the
.chinelement, and give it awidthof90%,heightof70%, andbackground-colorofwhite.Position the
.chinelement such that it is25%from the top, and5%from the left of its parent. Then, give the top corners a radius of70%, and the bottom corners a radius of100%.So far, the
.faceand.chinelements have the samebackground-color.Create a custom CSS property called
--penguin-face, and set it towhite.Where relevant, replace property values with your
--penguin-facevariable.Below the
.chinelement, add twodivelements each with aclassofeye. Also, give the first.eyeelement aclassofleft, and the second.eyeelement aclassofright.Target the
.eyeelements, and give them awidthof15%,heightof17%, andbackground-colorofblack.Position the
.eyeelements45%from the top of their parent, and give all corners a radius of50%.Target the
.eyeelement with theleftclass, and position it25%from the left of its parent. Then, target the.eyeelement with therightclass, and position it25%from the right of its parent.Within each
.eyeelement, add adivwith aclassofeye-lid.Target the
.eye-lidelements, and give them awidthof150%,heightof100%, andbackground-colorof--penguin-face.Position the
.eye-lidelements25%from the top, and-23%from the left of their parents. Then, give all corners a radius of50%.Below the
.eye.rightelement, add twodivelements each with aclassofblush. Also, give the first.blushelement aclassofleft, and the second.blushelement aclassofright.Target the
.blushelements, and give them awidthof15%,heightof10%, andbackground-colorofpink.Position the
.blushelements65%from the top of their parent, and give all corners a radius of50%.Target the
.blushelement with aclassofleft, and position it15%left of its parent. Then, target the.blushelement with aclassofright, and position it15%right of its parent.Below the
.blush.rightelement, add twodivelements each with aclassofbeak. Also, give the first.beakelement aclassoftop, and the second.beakelement aclassofbottom.Target the
.beakelements, and give them aheightof10%,background-coloroforange, and give all corners a radius of50%.Target the
.beakelement with aclassoftop, give it awidthof20%, and position it60%from the top, and40%from the left of its parent.Target the
.beakelement with aclassofbottom, and give it awidth4%smaller than.beak.top,5%further from the top, and2%further from the left of its parent than.beak.top.The penguin’s body looks a bit plain. Spruce him up by adding a
divelement with aclassofshirt, immediately before the.penguin-bodyelement.Within the
.shirtelement, add adivwith the following emoji as content: 💜Within
.shirt, after thedivelement, add apelement with the following content:I CSSTarget the
.shirtelement, and set itsfont-sizeto25px,font-familytoHelveticawith a fallback ofsans-serif, andfont-weighttobold.In some browsers, the heart emoji may look slightly different from the previous step. This is because some of the character’s properties were overridden by the
font-weightstyle ofbold.Fix this, by targeting the
divwith the heart emoji, and setting itsfont-weightto its original value.Position the
divwith the heart emoji22.5pxfrom the top, and12pxfrom the left of its parent.Position the
.shirtelement165pxfrom the top, and127.5pxfrom the left of its parent. Then, increase its stacking order such that it appears above the.penguin-bodyelement.For the shirt’s final touch, set the
colorto#6a6969.Fun fact: Penguins cannot stand without at least two feet.
Within the
.penguin-bodyelement, add twodivelements each with aclassoffoot. Give the first.footaclassofleft, and the second.footaclassofright.Target the
.footelements, and give them awidthof15%,heightof30%, andbackground-coloroforange.Position the
.footelements85%from the top of their parent, and give all corners a radius of50%.The penguin’s beak and feet share the same
color.Create a new custom CSS variable named
--penguin-picorna, and replace all relavant property values with itTarget the
.footelement with aclassofleft, and position it25%left of its parent. Then, target the.footelement with aclassofright, and position it25%right of its parent.To make the penguin’s feet look more penguiny, rotate the left foot by
80deg, and the right by-80deg.Change the stacking order of the
.footelements such that they appear beneath the.penguin-bodyelement.Fun fact: Penguins cannot fly without wings.
Within
.penguin-body, before the.footelements, add twodivelements each with aclassofarm. Give the first.armaclassofleft, and the second.armaclassofright.Fun fact: Penguins cannot fly without wings.
Within
.penguin-body, before the.footelements, add twodivelements each with aclassofarm. Give the first.armaclassofleft, and the second.armaclassofright.Create a custom CSS variable named
--penguin-skin, and set it togray. Then, replace all relevant property values with it.Target the
.armelement with aclassofleft, and position it35%from the top, and5%from the left of its parent. Then, target the.armelement with aclassofright, and position it0%from the top, and-5%from the right of its parent.Within the
.arm.leftselector, alter the origin of thetransformfunction to be the top left corner of its parent.To keep the linear gradient on the correct side of the penguin’s left arm, first rotate it by
130deg, then invert the x-axis.Rotate the right arm by
45degcounterclockwise.Fun fact: Most, if not all, flippers are not naturally rectangles.
Give the
.armelements’ top-left, top-right, and bottom-right corners a radius of30%, and the bottom-left corner a radius of120%.Change the
.armelements’ stacking order such that they appear behind the.penguin-bodyelement.Now, you are going to use CSS animations to make the penguin wave.
Define a new
@keyframesnamedwave.Give
wavefour waypoints starting at10%, and incrementing by10%.Within the first waypoint, rotate to
110deg, and retain the scaling of the left arm.Within the second waypoint, rotate to
130deg, and retain the scaling of the left arm.For the third and fourth waypoints, repeat the
transformpattern once more.Use the
waveanimation on the left arm. Have the animation last3s, infinitely iterate, and have a linear timing function.Target the
.penguinelement when it is active, and increase its size by50%in both dimensions.When you activate the
.penguinelement, it might look as though you can drag it around. This is not true.Indicate this to users, by giving the active element a
cursorproperty ofnot-allowed.Change the
.penguinelement’stransitionbehavior during transformation to have a duration of1s, a timing function ofease-in-out, and a delay of0ms.Finally, calculate the
heightof the.groundelement to be the height of the viewport minus the height of the.penguinelement.Congratulations! You have completed the Responsive Web Design certification.
1 |
|
1 | :root { |