Responsive Web Dedign(十二)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Variables by Building a City Skyline
Welcome to the CSS Variables Skyline project! Start by adding the
!DOCTYPE html
declaration at the top of the document so the browser knows what type of document it’s reading.Add opening and closing
html
tags below theDOCTYPE
so you have a place to start putting some code. Be sure to set the language to English.Next, add opening and closing
head
andbody
tags within thehtml
element.Within the
head
, nest ameta
element with acharset
ofUTF-8
, atitle
element with a title ofCity Skyline
, and alink
element that links yourstyles.css
file.In CSS, you can target everything with an asterisk. Add a border to everything by using the
*
selector, and giving it aborder
of1px solid black
. This is a trick that helps visualize where elements are and their size. You will remove this later.Also add a
box-sizing
ofborder-box
to everything. This will make it so the border you added doesn’t add any size to your elements.You can see the
body
(it’s the inner-most box on your page); the box around it is thehtml
element. Make yourbody
fill the whole viewport by giving it aheight
of100vh
. Remove the defaultmargin
from thebody
by setting themargin
to0
. Finally, set theoverflow
property tohidden
to hide any scroll bars that appear when something extends past the viewport.Create a
div
element in thebody
with a class ofbackground-buildings
. This will be a container for a group of buildings.Give your
.background-buildings
element awidth
andheight
of100%
to make it the full width and height of its parent, thebody
.Nest a
div
with a class ofbb1
in the background buildings container. Open yourstyles.css
file, and give.bb1
awidth
of10%
andheight
of70%
. “bb” stands for “background building”, this will be your first building.Nest four
div
elements in the.bb1
container. Give them the classesbb1a
,bb1b
,bb1c
, andbb1d
in that order. This building will have four sections.Give the parts of your building
width
andheight
properties with these values:70%
and10%
to.bb1a
,80%
and10%
to.bb1b
,90%
and10%
to.bb1c
, and100%
and70%
to.bb1d
. Remember that these percentages are relative to the parent and note that the heights will add up to 100% - vertically filling the container.Center the parts of your building by turning the
.bb1
element into a flexbox parent. Use theflex-direction
andalign-items
properties to center the children.Now you have something that is resembling a building. You are ready to create your first variable. Variable declarations begin with two dashes (
-
) and are given a name and a value like this:--variable-name: value;
. In the rule for thebb1
class, create a variable named--building-color1
and give it a value of#999
.To use a variable, put the variable name in parentheses with
var
in front of them like this:var(--variable-name)
. Whatever value you gave the variable will be applied to whatever property you use it on.Add the variable
--building-color1
you created in the previous step as the value of thebackground-color
property of the.bb1a
class.Use the same variable as the
background-color
of the.bb1b
,.bb1c
, and.bb1d
classes to fill in the rest of the building.Change the value of your variable from
#999
to#aa80ff
and you can see how it gets applied everywhere you used the variable. This is the main advantage of using variables, being able to quickly change many values in your stylesheet by just changing the value of a variable.Your first building looks pretty good now. Nest three new
div
elements in the.background-buildings
container and give them the classes ofbb2
,bb3
, andbb4
in that order. These will be three more buildings for the background.Give the new buildings
width
andheight
properties of:10%
and50%
for.bb2
,10%
and55%
for.bb3
, and11%
and58%
for.bb4
. You will be using almost all percent based units and some flexbox for this project, so everything will be completely responsive.The buildings are currently stacked on top of each other. Align the buildings by turning the
.background-buildings
element into a flexbox parent. Use thealign-items
andjustify-content
properties to evenly space the buildings across the bottom of the element.The buildings are too spaced out. Squeeze them together by adding two empty
div
elements to the top of the.background-buildings
element, two more at the bottom of it, and one more in between.bb3
and.bb4
. These will be added as evenly-spaced elements across the container, effectively moving the buildings closer to the center.Create a new variable below your
--building-color1
variable. Name your new variable--building-color2
and give it a value of#66cc99
. Then set it as thebackground-color
of.bb2
.That didn’t work. You should add a fallback value to a variable by putting it as the second value of where you use the variable like this:
var(--variable-name, fallback-value)
. The property will use the fallback value when there’s a problem with the variable. Add a fallback value ofgreen
to thebackground-color
of.bb2
.Create a new variable below the other ones named
--building-color3
and give it a value of#cc6699
. Then use it as thebackground-color
of the.bb3
class and give it a fallback value ofpink
.That didn’t work, because the variables you declared in
.bb1
do not cascade to the.bb2
and.bb3
sibling elements. That’s just how CSS works. Because of this, variables are often declared in the:root
selector. This is the highest level selector in CSS; putting your variables there will make them usable everywhere. Add the:root
selector to the top of your stylesheet, and move all your variable declarations there.Now that you’ve worked the bugs out and the buildings are the right colors, you can remove the fallback values in the two places they were used. Go ahead and do that now.
Create another variable named
--building-color4
and give it a value of#538cc6
. Make sure it’s in the:root
selector this time. Then use it to fill in the last building.The background buildings are starting to look pretty good. Create a new
div
below the.background-buildings
element and give it a class offoreground-buildings
. This will be another container for more buildings.You want the
.foreground-buildings
container to sit directly on top of the.background-buildings
element. Give it awidth
andheight
of100%
, set theposition
toabsolute
, and thetop
to0
. This will make it the same size as the body and move the start of it to the top left corner.Nest six
div
elements within.foreground-buildings
and give them the classes offb1
throughfb6
in that order. “fb” stands for “foreground building”. These will be six more buildings for the foreground.Give the six new elements these
width
andheight
values:10%
and60%
to.fb1
,10%
and40%
to.fb2
,10%
and35%
to.fb3
,8%
and45%
to.fb4
,10%
and33%
to.fb5
, and9%
and38%
to.fb6
.Add the same
display
,align-items
, andjustify-content
properties and values to.foreground-buildings
that you used on.background-buildings
. Again, this will use Flexbox to evenly space the buildings across the bottom of their container.You should optimize your code. Move the
position
andtop
properties and values from.foreground-buildings
to.background-buildings
. Then select both.background-buildings
and.foreground-buildings
there, effectively applying those styles to both of the elements. You can use a comma (,
) to separate selectors like this:selector1, selector2
.Now that you did that, you can delete the old
.foreground-buildings
declaration and all of its properties since they aren’t needed anymore.The skyline is coming together. Fill in the
background-color
property of the foreground buildings. Use your--building-color1
variable to fill in.fb3
and.fb4
,--building-color2
for.fb5
,--building-color3
for.fb2
and.fb6
, and--building-color4
for.fb1
.Squeeze the buildings together again by adding two empty
div
elements within both the top and bottom of the.foreground-buildings
element, and one more in between.fb2
and.fb3
.Move the position of
.fb4
relative to where it is now by adding aposition
ofrelative
andleft
of10%
to it. Do the same for.fb5
but useright
instead ofleft
. This will cover up the remaining white space in between the buildings.Your code is starting to get quite long. Add a comment above the
.fb1
class that saysFOREGROUND BUILDINGS - "fb" stands for "foreground building"
to help people understand your code. Above the.bb1
class add another comment that saysBACKGROUND BUILDINGS - "bb" stands for "background building"
. If you don’t remember, comments in CSS look like this:/* Comment here */
.Create a new variable in
:root
called--window-color1
and give it a value ofblack
. This will be a secondary color for the purple buildings.Gradients in CSS are a way to transition between colors across the distance of an element. They are applied to the
background
property and the syntax looks like this:1
2
3
4gradient-type(
color1,
color2
);In the example,
color1
is solid at the top,color2
is solid at the bottom, and in between it transitions evenly from one to the next. In.bb1a
, add abackground
property below thebackground-color
property. Set it as a gradient of typelinear-gradient
that uses--building-color1
as the first color and--window-color1
as the second.You want to add the same gradient to the next two sections. Instead of doing that, create a new class selector called
bb1-window
, and move theheight
andbackground
properties and values from.bb1a
to the new class selector.Add the new
bb1-window
class to the.bb1a
,.bb1b
, and.bb1c
elements. This will apply the gradient to them.You don’t need the
height
orbackground-color
properties in.bb1a
,.bb1b
or.bb1c
anymore, so go ahead and remove them.Gradients can use as many colors as you want like this:
1
2
3
4
5gradient-type(
color1,
color2,
color3
);Add a
linear-gradient
to.bb1d
withorange
as the first color,--building-color1
as the second, and--window-color1
as the third. Remember to use the gradient on thebackground
property.It’s a little hidden behind the foreground buildings, but you can see the three color gradient there. Since you are using that now, remove the
background-color
property from.bb1d
.You can specify where you want a gradient transition to complete by adding it to the color like this:
1
2
3
4
5gradient-type(
color1,
color2 20%,
color3
);Here, it will transition from
color1
tocolor2
between0%
and20%
of the element and then transition tocolor3
for the rest. Add80%
to the--building-color1
color of the.bb1d
gradient so you can see it in action.Remove
orange
from the.bb1d
gradient and change the80%
to50%
. This will make--building-color1
solid for the top half, and then transition to--window-color1
for the bottom half.Nest two new
div
elements within.bb2
, give them the classes ofbb2a
andbb2b
, in that order. These will be two sections for this building.Give
.bb2b
awidth
andheight
of100%
to make it fill the building container. You will add something on the top a little later.Create a new variable in
:root
namedwindow-color2
with a value of#8cd9b3
. This will be used as the secondary color for this building.Gradient transitions often gradually change from one color to another. You can make the change a solid line like this:
1
2
3
4
5
6linear-gradient(
var(--first-color) 0%,
var(--first-color) 40%,
var(--second-color) 40%,
var(--second-color) 80%
);Add a
linear-gradient
to.bb2b
that uses--building-color2
from0%
to6%
and--window-color2
from6%
to9%
.You can see the hard color change at the top of the section. Change the gradient type from
linear-gradient
torepeating-linear-gradient
for this section. This will make the four colors of your gradient repeat until it gets to the bottom of the element; giving you some stripes, and saving you from having to add a bunch of elements to create them.In the next few steps, you are going to use some tricks with CSS borders to turn the
.bb2a
section into a triangle at the top of the building. First, remove thebackground-color
from.bb2
since you don’t need it anymore.Add these properties to
.bb2a
:1
2
3
4
5
6
7margin: auto;
width: 5vw;
height: 5vw;
border-top: 1vw solid #000;
border-bottom: 1vw solid #000;
border-left: 1vw solid #999;
border-right: 1vw solid #999;After you add these, you can see how a thick border on an element gives you some angles where two sides meet. You are going to use that bottom border as the top of the building.
Next, remove the
width
andheight
from.bb2a
, and change theborder-left
andborder-right
to use5vw
instead of1vw
. The element will now have zero size and the borders will come together in the middle.Next, change the two
#999
of.bb2a
totransparent
. This will make the left and right borders invisible.Remove the
margin
andborder-top
properties and values from.bb2a
to turn it into a triangle for the top of the building.Finally, on the
border-bottom
property of.bb2a
, change the1vw
to5vh
and change the#000
color to your--building-color2
variable. There you go, now it looks good! At any time throughout this project, you can comment out or remove theborder
property you added to everything at the beginning to see what the buildings will look like when that gets removed at the end.On to the next building! Create a new variable called
--window-color3
in:root
and give it a value of#d98cb3
. This will be the secondary color for the pink buildings.So far, all the gradients you created have gone from top to bottom, that’s the default direction. You can specify another direction by adding it before your colors like this:
1
2
3
4
5gradient-type(
direction,
color1,
color2
);Fill in
.bb3
with arepeating-linear-gradient
. Use90deg
for the direction, yourbuilding-color3
for the first two colors, andwindow-color3
at15%
for the third. When you don’t specify a distance for a color, it will use the values that makes sense. In this case, the first two colors will default to0%
and7.5%
because it starts at0%
, and7.5%
is half of the15%
.Remove the
background-color
property and value from.bb3
since you are using the gradient as the background now.The next building will have three sections. Nest three
div
elements within.bb4
. Give them the classes ofbb4a
,bb4b
andbb4c
in that order.Give the new
div
elements thesewidth
andheight
values:3%
and10%
to.bb4a
,80%
and5%
to.bb4b
, and100%
and85%
to.bb4c
.Remove the
background-color
property and value from.bb4
, and add it to the three new sections.bb4a
,.bb4b
, and.bb4c
, so only the sections are filled.You want
.bb4
to share the properties of.bb1
that center the sections. Instead of duplicating that code, create a new class above the background building comment calledbuilding-wrap
. Leave it empty for now; this class will be used in a few places to save you some coding.Move the
display
,flex-direction
, andalign-items
properties and values from.bb1
to the newbuilding-wrap
class.Add the new
building-wrap
class to the.bb1
and.bb4
elements. This will apply the centering properties to the buildings that need it.Create a new variable called
--window-color4
in:root
and give it a value of#8cb3d9
. This will be the secondary color for the last background building.Nest four new
div
elements within.bb4c
, give them all the class ofbb4-window
. These will be windows for this building.Give the
bb4-window
class awidth
of18%
, aheight
of90%
, and add your--window-color4
variable as thebackground-color
.The windows are stacked on top of each other at the left of the section, behind the purple building. Add a new class below
.building-wrap
calledwindow-wrap
. Make.window-wrap
a flexbox container, and use thealign-items
andjustify-content
properties to center its child elements vertically and evenly space them in their parent, respectively.Add the new
window-wrap
class to the.bb4c
element.Looks good! On to the foreground buildings! Turn the
.fb1
building into three sections by nesting three newdiv
elements within it. Give them the classes offb1a
,fb1b
andfb1c
, in that order.Give
.fb1b
awidth
of60%
andheight
of10%
, and.fb1c
awidth
of100%
andheight
of80%
.Add the
building-wrap
class to the.fb1
element to center the sections.Move the
background-color
property and value from.fb1
to.fb1b
.Don’t worry about the space at the bottom, everything will get moved down later when you add some height to the element at the top of the building.
Add a
repeating-linear-gradient
to.fb1c
with a90deg
angle, your--building-color4
from0%
to10%
andtransparent
from10%
to15%
.You can add multiple gradients to an element by separating them with a comma (
,
) like this:1
2
3
4
5
6gradient1(
colors
),
gradient2(
colors
);Add a
repeating-linear-gradient
to.fb1c
below the one that’s there; use your--building-color4
from0%
to10%
and--window-color4
from10%
and90%
. This will fill in behind the gradient you added last.You’re going to use some more border tricks for the top section. Add a
border-bottom
with a value of7vh solid var(--building-color4)
to.fb1a
. This will put a7vh
height border on the bottom. But since the element has zero size, it only shows up as a 2px wide line from the 1px border that is on all the elements.When you increase the size of the left and right borders, the border on the bottom will expand to be the width of the combined left and right border widths. Add
2vw solid transparent
as the value of theborder-left
andborder-right
properties of.fb1a
. They will be invisible, but it will make the border on the bottom4vw
wide.On to the next building! Nest two
div
elements within.fb2
and give them classes offb2a
andfb2b
, in that order.Give
.fb2a
awidth
of100%
and.fb2b
awidth
of100%
andheight
of75%
.Nest three
div
elements within.fb2b
and give them a class offb2-window
. These will be windows for this section of the building.Add your
window-wrap
class to.fb2b
to position the new window elements.Give the
.fb2-window
elements awidth
of22%
,height
of100%
, and abackground-color
of your--window-color3
variable.Move the
background-color
property and value from.fb2
to.fb2b
to just color the section and not the container.For
.fb2a
, add aborder-bottom
of10vh solid var(--building-color3)
, and aborder-left
andborder-right
of1vw solid transparent
. This time the border trick will create a trapezoid shape.For the next building, nest four
div
elements within.fb3
with classes offb3a
,fb3b
,fb3a
again, andfb3b
again, in that order. This building will have four sections, and the top two will be almost the same as the bottom two.Give the
.fb3a
element awidth
of80%
andheight
of15%
. Then give the.fb3b
element awidth
of100%
andheight
of35%
.Remove the
background-color
property and value from.fb3
, and add them to.fb3a
and.fb3b
.Add your
building-wrap
class to the.fb3
element to center the sections.Nest three new
div
elements in the first.fb3a
element. Give them each a class offb3-window
. These will be windows for this section.Give the
.fb3-window
elements awidth
of25%
, aheight
of80%
, and use your--window-color1
variable as thebackground-color
value.Add your
window-wrap
class to the.fb3a
element to center and space the windows.With CSS variables you can change values without searching everywhere in the stylesheet. Change the
--window-color1
value to#bb99ff
.Only three more buildings to go. Nest two new
div
elements within the.fb4
element and give them the classes offb4a
andfb4b
, in that order. Remember that you sort of flipped the location of.fb4
and.fb5
, so it’s the rightmost purple building you are working on now.Give
.fb4b
awidth
of100%
andheight
of89%
.Add your
--building-color1
variable as value of thebackground-color
property of.fb4b
. Then, remove thebackground-color
from.fb4
.Nest six
div
elements within.fb4b
and give them all a class offb4-window
.Give the
.fb4-window
elements awidth
of30%
,height
of10%
, andborder-radius
of50%
. These will make some circular windows for this building.Fill in the windows with your secondary color for this building. Also add a
margin
of10%
to give the windows some space.The windows are stacked on top of each other on the rightmost purple building. Turn the building into a flexbox parent, and use the
flex-wrap
property to put the windows side by side, and push them down to a new row when they don’t fit.This building is going to have another triangle on top. Give the top section a
border-top
of5vh solid transparent
, and aborder-left
that is8vw
,solid
, and uses your building color variable as the color.On to the next building! It’s the green one in the foreground. Give it a
repeating-linear-gradient
with your building color from0%
to5%
, andtransparent
from5%
to10%
.Add another
repeating-linear-gradient
below the one you just added. Give it a90deg
direction, use your building color from0%
to12%
and window color12%
to44%
. This will make a bunch of rectangle windows.You don’t need the
background-color
for this building anymore so you can remove that property.Finally! You made it to the last building! Add a repeating gradient to it with a
90deg
direction. Use the building color from0%
to10%
andtransparent
from10%
to30%
.Add another repeating gradient to this building; make it the same as the one you just added, except don’t add the
90deg
direction and use your window color instead of the twotransparent
colors.You can remove the
background-color
for this building now, since it isn’t needed.Okay, the buildings are done. Go back to the
*
selector and remove theborder
you applied to everything at the beginning and the buildings will come together.Add
sky
as a second class to the.background-buildings
element. You are going to make a background for the skyline.Give the
sky
class aradial-gradient
. Use#ffcf33
from0%
to20%
,#ffff66
at21%
, and#bbeeff
at100%
. This will add circular gradient to the background that will be your sun.At the top of the sky gradient color list, where you would put a direction for the gradient; add
circle closest-corner at 15% 15%,
. This will move the start of the gradient to15%
from the top and left. It will make it end at theclosest-corner
and it will maintain acircle
shape. These are some keywords built into gradients to describe how it behaves.A media query can be used to change styles based on certain conditions, and they look like this:
1
2
3@media (condition) {
}Add an empty media query at the bottom of your stylesheet with a condition of
max-width: 1000px
. Styles added in here will take effect when the document size is 1000px wide or less.Copy and paste your whole
sky
class along with all of its properties and values into the media query. You are going to make another color scheme for the skyline that changes it from day to night.Note: You are going to need to scroll past the editable region to copy the class.
In the
sky
class of the media query, change the two#ffcf33
color values to#ccc
, the#ffff66
to#445
, and the#bbeeff
to#223
. Then you can resize your window to see the background change colors.Add a
:root
selector to the top of your media query. Then redefine all four of the--building-color
variables to use the value#000
there.Lastly, in the
:root
selector of the media query, redefine all four of the--window-color
variables to use#777
. When you’re done, resize the window and watch it go from day to night.Variables are primarily used with colors, and that’s how you used them here. But they can be given any value and used on any property. Your project looks great!
1 |
|
1 | :root { |