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 htmldeclaration at the top of the document so the browser knows what type of document it’s reading.Add opening and closing
htmltags below theDOCTYPEso you have a place to start putting some code. Be sure to set the language to English.Next, add opening and closing
headandbodytags within thehtmlelement.Within the
head, nest ametaelement with acharsetofUTF-8, atitleelement with a title ofCity Skyline, and alinkelement that links yourstyles.cssfile.In CSS, you can target everything with an asterisk. Add a border to everything by using the
*selector, and giving it aborderof1px solid black. This is a trick that helps visualize where elements are and their size. You will remove this later.Also add a
box-sizingofborder-boxto 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 thehtmlelement. Make yourbodyfill the whole viewport by giving it aheightof100vh. Remove the defaultmarginfrom thebodyby setting themarginto0. Finally, set theoverflowproperty tohiddento hide any scroll bars that appear when something extends past the viewport.Create a
divelement in thebodywith a class ofbackground-buildings. This will be a container for a group of buildings.Give your
.background-buildingselement awidthandheightof100%to make it the full width and height of its parent, thebody.Nest a
divwith a class ofbb1in the background buildings container. Open yourstyles.cssfile, and give.bb1awidthof10%andheightof70%. “bb” stands for “background building”, this will be your first building.Nest four
divelements in the.bb1container. Give them the classesbb1a,bb1b,bb1c, andbb1din that order. This building will have four sections.Give the parts of your building
widthandheightproperties 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
.bb1element into a flexbox parent. Use theflex-directionandalign-itemsproperties 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 thebb1class, create a variable named--building-color1and give it a value of#999.To use a variable, put the variable name in parentheses with
varin 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-color1you created in the previous step as the value of thebackground-colorproperty of the.bb1aclass.Use the same variable as the
background-colorof the.bb1b,.bb1c, and.bb1dclasses to fill in the rest of the building.Change the value of your variable from
#999to#aa80ffand 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
divelements in the.background-buildingscontainer and give them the classes ofbb2,bb3, andbb4in that order. These will be three more buildings for the background.Give the new buildings
widthandheightproperties 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-buildingselement into a flexbox parent. Use thealign-itemsandjustify-contentproperties to evenly space the buildings across the bottom of the element.The buildings are too spaced out. Squeeze them together by adding two empty
divelements to the top of the.background-buildingselement, two more at the bottom of it, and one more in between.bb3and.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-color1variable. Name your new variable--building-color2and give it a value of#66cc99. Then set it as thebackground-colorof.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 ofgreento thebackground-colorof.bb2.Create a new variable below the other ones named
--building-color3and give it a value of#cc6699. Then use it as thebackground-colorof the.bb3class and give it a fallback value ofpink.That didn’t work, because the variables you declared in
.bb1do not cascade to the.bb2and.bb3sibling elements. That’s just how CSS works. Because of this, variables are often declared in the:rootselector. This is the highest level selector in CSS; putting your variables there will make them usable everywhere. Add the:rootselector 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-color4and give it a value of#538cc6. Make sure it’s in the:rootselector this time. Then use it to fill in the last building.The background buildings are starting to look pretty good. Create a new
divbelow the.background-buildingselement and give it a class offoreground-buildings. This will be another container for more buildings.You want the
.foreground-buildingscontainer to sit directly on top of the.background-buildingselement. Give it awidthandheightof100%, set thepositiontoabsolute, and thetopto0. This will make it the same size as the body and move the start of it to the top left corner.Nest six
divelements within.foreground-buildingsand give them the classes offb1throughfb6in that order. “fb” stands for “foreground building”. These will be six more buildings for the foreground.Give the six new elements these
widthandheightvalues: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-contentproperties and values to.foreground-buildingsthat 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
positionandtopproperties and values from.foreground-buildingsto.background-buildings. Then select both.background-buildingsand.foreground-buildingsthere, 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-buildingsdeclaration and all of its properties since they aren’t needed anymore.The skyline is coming together. Fill in the
background-colorproperty of the foreground buildings. Use your--building-color1variable to fill in.fb3and.fb4,--building-color2for.fb5,--building-color3for.fb2and.fb6, and--building-color4for.fb1.Squeeze the buildings together again by adding two empty
divelements within both the top and bottom of the.foreground-buildingselement, and one more in between.fb2and.fb3.Move the position of
.fb4relative to where it is now by adding apositionofrelativeandleftof10%to it. Do the same for.fb5but userightinstead 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
.fb1class that saysFOREGROUND BUILDINGS - "fb" stands for "foreground building"to help people understand your code. Above the.bb1class 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
:rootcalled--window-color1and 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
backgroundproperty and the syntax looks like this:1
2
3
4gradient-type(
color1,
color2
);In the example,
color1is solid at the top,color2is solid at the bottom, and in between it transitions evenly from one to the next. In.bb1a, add abackgroundproperty below thebackground-colorproperty. Set it as a gradient of typelinear-gradientthat uses--building-color1as the first color and--window-color1as 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 theheightandbackgroundproperties and values from.bb1ato the new class selector.Add the new
bb1-windowclass to the.bb1a,.bb1b, and.bb1celements. This will apply the gradient to them.You don’t need the
heightorbackground-colorproperties in.bb1a,.bb1bor.bb1canymore, 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-gradientto.bb1dwithorangeas the first color,--building-color1as the second, and--window-color1as the third. Remember to use the gradient on thebackgroundproperty.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-colorproperty 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
color1tocolor2between0%and20%of the element and then transition tocolor3for the rest. Add80%to the--building-color1color of the.bb1dgradient so you can see it in action.Remove
orangefrom the.bb1dgradient and change the80%to50%. This will make--building-color1solid for the top half, and then transition to--window-color1for the bottom half.Nest two new
divelements within.bb2, give them the classes ofbb2aandbb2b, in that order. These will be two sections for this building.Give
.bb2bawidthandheightof100%to make it fill the building container. You will add something on the top a little later.Create a new variable in
:rootnamedwindow-color2with 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-gradientto.bb2bthat uses--building-color2from0%to6%and--window-color2from6%to9%.You can see the hard color change at the top of the section. Change the gradient type from
linear-gradienttorepeating-linear-gradientfor 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
.bb2asection into a triangle at the top of the building. First, remove thebackground-colorfrom.bb2since 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
widthandheightfrom.bb2a, and change theborder-leftandborder-rightto use5vwinstead of1vw. The element will now have zero size and the borders will come together in the middle.Next, change the two
#999of.bb2atotransparent. This will make the left and right borders invisible.Remove the
marginandborder-topproperties and values from.bb2ato turn it into a triangle for the top of the building.Finally, on the
border-bottomproperty of.bb2a, change the1vwto5vhand change the#000color to your--building-color2variable. There you go, now it looks good! At any time throughout this project, you can comment out or remove theborderproperty 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-color3in:rootand 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
.bb3with arepeating-linear-gradient. Use90degfor the direction, yourbuilding-color3for the first two colors, andwindow-color3at15%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-colorproperty and value from.bb3since you are using the gradient as the background now.The next building will have three sections. Nest three
divelements within.bb4. Give them the classes ofbb4a,bb4bandbb4cin that order.Give the new
divelements thesewidthandheightvalues:3%and10%to.bb4a,80%and5%to.bb4b, and100%and85%to.bb4c.Remove the
background-colorproperty and value from.bb4, and add it to the three new sections.bb4a,.bb4b, and.bb4c, so only the sections are filled.You want
.bb4to share the properties of.bb1that 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-itemsproperties and values from.bb1to the newbuilding-wrapclass.Add the new
building-wrapclass to the.bb1and.bb4elements. This will apply the centering properties to the buildings that need it.Create a new variable called
--window-color4in:rootand give it a value of#8cb3d9. This will be the secondary color for the last background building.Nest four new
divelements within.bb4c, give them all the class ofbb4-window. These will be windows for this building.Give the
bb4-windowclass awidthof18%, aheightof90%, and add your--window-color4variable 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-wrapcalledwindow-wrap. Make.window-wrapa flexbox container, and use thealign-itemsandjustify-contentproperties to center its child elements vertically and evenly space them in their parent, respectively.Add the new
window-wrapclass to the.bb4celement.Looks good! On to the foreground buildings! Turn the
.fb1building into three sections by nesting three newdivelements within it. Give them the classes offb1a,fb1bandfb1c, in that order.Give
.fb1bawidthof60%andheightof10%, and.fb1cawidthof100%andheightof80%.Add the
building-wrapclass to the.fb1element to center the sections.Move the
background-colorproperty and value from.fb1to.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-gradientto.fb1cwith a90degangle, your--building-color4from0%to10%andtransparentfrom10%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-gradientto.fb1cbelow the one that’s there; use your--building-color4from0%to10%and--window-color4from10%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-bottomwith a value of7vh solid var(--building-color4)to.fb1a. This will put a7vhheight 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 transparentas the value of theborder-leftandborder-rightproperties of.fb1a. They will be invisible, but it will make the border on the bottom4vwwide.On to the next building! Nest two
divelements within.fb2and give them classes offb2aandfb2b, in that order.Give
.fb2aawidthof100%and.fb2bawidthof100%andheightof75%.Nest three
divelements within.fb2band give them a class offb2-window. These will be windows for this section of the building.Add your
window-wrapclass to.fb2bto position the new window elements.Give the
.fb2-windowelements awidthof22%,heightof100%, and abackground-colorof your--window-color3variable.Move the
background-colorproperty and value from.fb2to.fb2bto just color the section and not the container.For
.fb2a, add aborder-bottomof10vh solid var(--building-color3), and aborder-leftandborder-rightof1vw solid transparent. This time the border trick will create a trapezoid shape.For the next building, nest four
divelements within.fb3with classes offb3a,fb3b,fb3aagain, andfb3bagain, in that order. This building will have four sections, and the top two will be almost the same as the bottom two.Give the
.fb3aelement awidthof80%andheightof15%. Then give the.fb3belement awidthof100%andheightof35%.Remove the
background-colorproperty and value from.fb3, and add them to.fb3aand.fb3b.Add your
building-wrapclass to the.fb3element to center the sections.Nest three new
divelements in the first.fb3aelement. Give them each a class offb3-window. These will be windows for this section.Give the
.fb3-windowelements awidthof25%, aheightof80%, and use your--window-color1variable as thebackground-colorvalue.Add your
window-wrapclass to the.fb3aelement to center and space the windows.With CSS variables you can change values without searching everywhere in the stylesheet. Change the
--window-color1value to#bb99ff.Only three more buildings to go. Nest two new
divelements within the.fb4element and give them the classes offb4aandfb4b, in that order. Remember that you sort of flipped the location of.fb4and.fb5, so it’s the rightmost purple building you are working on now.Give
.fb4bawidthof100%andheightof89%.Add your
--building-color1variable as value of thebackground-colorproperty of.fb4b. Then, remove thebackground-colorfrom.fb4.Nest six
divelements within.fb4band give them all a class offb4-window.Give the
.fb4-windowelements awidthof30%,heightof10%, andborder-radiusof50%. These will make some circular windows for this building.Fill in the windows with your secondary color for this building. Also add a
marginof10%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-wrapproperty 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-topof5vh solid transparent, and aborder-leftthat 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-gradientwith your building color from0%to5%, andtransparentfrom5%to10%.Add another
repeating-linear-gradientbelow the one you just added. Give it a90degdirection, use your building color from0%to12%and window color12%to44%. This will make a bunch of rectangle windows.You don’t need the
background-colorfor 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
90degdirection. Use the building color from0%to10%andtransparentfrom10%to30%.Add another repeating gradient to this building; make it the same as the one you just added, except don’t add the
90degdirection and use your window color instead of the twotransparentcolors.You can remove the
background-colorfor this building now, since it isn’t needed.Okay, the buildings are done. Go back to the
*selector and remove theborderyou applied to everything at the beginning and the buildings will come together.Add
skyas a second class to the.background-buildingselement. You are going to make a background for the skyline.Give the
skyclass aradial-gradient. Use#ffcf33from0%to20%,#ffff66at21%, and#bbeeffat100%. 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-cornerand it will maintain acircleshape. 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
skyclass 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
skyclass of the media query, change the two#ffcf33color values to#ccc, the#ffff66to#445, and the#bbeeffto#223. Then you can resize your window to see the background change colors.Add a
:rootselector to the top of your media query. Then redefine all four of the--building-colorvariables to use the value#000there.Lastly, in the
:rootselector of the media query, redefine all four of the--window-colorvariables 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 { |