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
DOCTYPE
declaration,html
element with a language set to English, the appropriatemeta
tags, ahead
,body
, andtitle
element. Also, link your stylesheet to the page.Target the
body
element to set thebackground
to 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
body
element’smargin
andpadding
.Normalise your page, by setting the
width
to100%
, andheight
to100vh
.Remove both the horizontal and vertical scrollbars, using only one property.
Within the
body
, add adiv
with aclass
ofground
.Target the
.ground
element, and set itswidth
to take up the full width of the viewport. Then, set theheight
to400px
.Give the
.ground
element abackground
with a linear gradient angled 90 degrees clockwise, starting atrgb(88, 175, 236)
and ending atrgb(182, 255, 255)
.As the
.ground
element will be third in the stacking context of the page layout, set itsz-index
to3
, andposition
toabsolute
.Above the
.ground
element, add adiv
with aclass
ofpenguin
. Thisdiv
will contain Flappy Penguin.Target the
.penguin
element, and set itswidth
andheight
to300px
.Use the
margin
property to horizontally center the.penguin
element, and set themargin-top
to75px
.To create some scenery in the background, you will add two mountains.
Above the
.penguin
element, add adiv
with aclass
ofleft-mountain
.Target the
.left-mountain
element, and set itswidth
andheight
to300px
. Then, set thebackground
to a linear gradient starting atrgb(203, 241, 228)
and ending atrgb(80, 183, 255)
.To prevent the mountain from pushing the
.ground
element, adjust itsposition
to prevent it from taking up space in the page layout.To make the mountain look more like a mountain, you can use the
skew
transform 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
transform
property to skew the mountain by0deg
in the x-axis and44deg
in the y-axis.Set the stack level of the mountain element such that it remains directly behind the
.ground
element.To overlap the mountain and
.ground
elements better, give the mountain amargin-top
of100px
, and the.ground
element amargin-top
of-58px
.To give the effect of a mountain range, add another mountain, by creating a new
div
immediately after.left-mountain
, and give the newdiv
theclass
ofback-mountain
.Target the
.back-mountain
element, and set itswidth
andheight
to300px
. Then, set thebackground
to a linear gradient starting atrgb(203, 241, 228)
and ending atrgb(47, 170, 255)
.Set the
position
property of the.back-mountain
to prevent it from taking up space in the page layout.Change the stack level of the
.back-mountain
element such that it is directly behind the.left-mountain
element.Rotate the
.back-mountain
element by45deg
clockwise. Then, give it aleft
property of110px
, and atop
property of225px
.Rotate the
.back-mountain
element by45deg
clockwise. Then, give it aleft
property of110px
, and atop
property of225px
.Give the
.sun
element awidth
andheight
of200px
, and abackground-color
ofyellow
.Set the
position
property of the sun to prevent it from taking up space in the page layout, and set theborder-radius
such that the sun’s shape is a circle.Position the sun in the top right corner of the screen such that
75px
of 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 newdiv
elements. The first with aclass
ofpenguin-head
, and the second with aclass
ofpenguin-body
.Change the stack level of the
.penguin
element such that it appears in front of the.ground
element, and give it aposition
ofrelative
.Target the
.penguin-head
element, and give it awidth
half of its parent’s, and aheight
of45%
. Then, set thebackground
to a linear gradient at45deg
starting 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-body
element, and give it awidth
of53%
, and aheight
of45%
. Then, set thebackground
to a linear gradient at45deg
,rgb(134, 133, 133)
from0%
,rgb(234, 231, 231)
from25%
, andwhite
from67%
.Another interesting fact about penguins is that they do not have square bodies.
Use the
border-radius
property with a value of80% 80% 100% 100%
, to give the penguin a slightly rounded body.Target all descendent elements of the
.penguin
element, and give them aposition
ofabsolute
.Position the
.penguin-head
element10%
from the top, and25%
from the left of its parent.Position the
.penguin-body
element40%
from the top, and23.5%
from the left of its parent.Position the
.penguin-body
element40%
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-body
element. Set thecontent
property of the pseudo-element to an empty string.Position the pseudo-element relative to its closest positioned ancestor.
Give the pseudo-element a
width
half that of its parent, aheight
of45%
, and abackground-color
ofgray
.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
div
elements within.penguin-head
, and giving them both aclass
offace
.Give the
.face
elements awidth
of60%
, aheight
of70%
, and abackground-color
ofwhite
.Make the top corners of the
.face
elements have a radius of70%
, and the bottom corners have a radius of60%
.Position the
.face
elements so that they are15%
from the top.Currently, the two
.face
elements are on top of each other.Fix this, by adding a
class
ofleft
to the first.face
element, and aclass
ofright
to the second.face
element.Target the
.face
element with theleft
class, and position it5%
left of its parent.Target the
.face
element with theright
class, and position it5%
right of its parent.Target the
.face
element with theright
class, and position it5%
right of its parent.Target the
.chin
element, and give it awidth
of90%
,height
of70%
, andbackground-color
ofwhite
.Position the
.chin
element 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
.face
and.chin
elements have the samebackground-color
.Create a custom CSS property called
--penguin-face
, and set it towhite
.Where relevant, replace property values with your
--penguin-face
variable.Below the
.chin
element, add twodiv
elements each with aclass
ofeye
. Also, give the first.eye
element aclass
ofleft
, and the second.eye
element aclass
ofright
.Target the
.eye
elements, and give them awidth
of15%
,height
of17%
, andbackground-color
ofblack
.Position the
.eye
elements45%
from the top of their parent, and give all corners a radius of50%
.Target the
.eye
element with theleft
class, and position it25%
from the left of its parent. Then, target the.eye
element with theright
class, and position it25%
from the right of its parent.Within each
.eye
element, add adiv
with aclass
ofeye-lid
.Target the
.eye-lid
elements, and give them awidth
of150%
,height
of100%
, andbackground-color
of--penguin-face
.Position the
.eye-lid
elements25%
from the top, and-23%
from the left of their parents. Then, give all corners a radius of50%
.Below the
.eye.right
element, add twodiv
elements each with aclass
ofblush
. Also, give the first.blush
element aclass
ofleft
, and the second.blush
element aclass
ofright
.Target the
.blush
elements, and give them awidth
of15%
,height
of10%
, andbackground-color
ofpink
.Position the
.blush
elements65%
from the top of their parent, and give all corners a radius of50%
.Target the
.blush
element with aclass
ofleft
, and position it15%
left of its parent. Then, target the.blush
element with aclass
ofright
, and position it15%
right of its parent.Below the
.blush.right
element, add twodiv
elements each with aclass
ofbeak
. Also, give the first.beak
element aclass
oftop
, and the second.beak
element aclass
ofbottom
.Target the
.beak
elements, and give them aheight
of10%
,background-color
oforange
, and give all corners a radius of50%
.Target the
.beak
element with aclass
oftop
, give it awidth
of20%
, and position it60%
from the top, and40%
from the left of its parent.Target the
.beak
element with aclass
ofbottom
, and give it awidth
4%
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
div
element with aclass
ofshirt
, immediately before the.penguin-body
element.Within the
.shirt
element, add adiv
with the following emoji as content: 💜Within
.shirt
, after thediv
element, add ap
element with the following content:I CSS
Target the
.shirt
element, and set itsfont-size
to25px
,font-family
toHelvetica
with a fallback ofsans-serif
, andfont-weight
tobold
.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-weight
style ofbold
.Fix this, by targeting the
div
with the heart emoji, and setting itsfont-weight
to its original value.Position the
div
with the heart emoji22.5px
from the top, and12px
from the left of its parent.Position the
.shirt
element165px
from the top, and127.5px
from the left of its parent. Then, increase its stacking order such that it appears above the.penguin-body
element.For the shirt’s final touch, set the
color
to#6a6969
.Fun fact: Penguins cannot stand without at least two feet.
Within the
.penguin-body
element, add twodiv
elements each with aclass
offoot
. Give the first.foot
aclass
ofleft
, and the second.foot
aclass
ofright
.Target the
.foot
elements, and give them awidth
of15%
,height
of30%
, andbackground-color
oforange
.Position the
.foot
elements85%
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
.foot
element with aclass
ofleft
, and position it25%
left of its parent. Then, target the.foot
element with aclass
ofright
, 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
.foot
elements such that they appear beneath the.penguin-body
element.Fun fact: Penguins cannot fly without wings.
Within
.penguin-body
, before the.foot
elements, add twodiv
elements each with aclass
ofarm
. Give the first.arm
aclass
ofleft
, and the second.arm
aclass
ofright
.Fun fact: Penguins cannot fly without wings.
Within
.penguin-body
, before the.foot
elements, add twodiv
elements each with aclass
ofarm
. Give the first.arm
aclass
ofleft
, and the second.arm
aclass
ofright
.Create a custom CSS variable named
--penguin-skin
, and set it togray
. Then, replace all relevant property values with it.Target the
.arm
element with aclass
ofleft
, and position it35%
from the top, and5%
from the left of its parent. Then, target the.arm
element with aclass
ofright
, and position it0%
from the top, and-5%
from the right of its parent.Within the
.arm.left
selector, alter the origin of thetransform
function 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
45deg
counterclockwise.Fun fact: Most, if not all, flippers are not naturally rectangles.
Give the
.arm
elements’ top-left, top-right, and bottom-right corners a radius of30%
, and the bottom-left corner a radius of120%
.Change the
.arm
elements’ stacking order such that they appear behind the.penguin-body
element.Now, you are going to use CSS animations to make the penguin wave.
Define a new
@keyframes
namedwave
.Give
wave
four 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
transform
pattern once more.Use the
wave
animation on the left arm. Have the animation last3s
, infinitely iterate, and have a linear timing function.Target the
.penguin
element when it is active, and increase its size by50%
in both dimensions.When you activate the
.penguin
element, it might look as though you can drag it around. This is not true.Indicate this to users, by giving the active element a
cursor
property ofnot-allowed
.Change the
.penguin
element’stransition
behavior during transformation to have a duration of1s
, a timing function ofease-in-out
, and a delay of0ms
.Finally, calculate the
height
of the.ground
element to be the height of the viewport minus the height of the.penguin
element.Congratulations! You have completed the Responsive Web Design certification.
1 |
|
1 | :root { |