Responsive Web Dedign(二)
freeCodeCamp —- Responsive Web Dedign
Learn Basic CSS by Buiding a Cafe Menu
Add the tag, and an element with a attribute of .
<!DOCTYPE html>``html``lang``enAdd a
headelement within thehtmlelement, so you can add atitleelement. Thetitleelement’s text should beCafe Menu.The
titleis one of several elements that provide extra information not visible on the web page, but it is useful for search engines or how the page gets displayed.Inside the
headelement, nest ametaelement with an attribute namedcharsetset to the valueutf-8to tell the browser how to encode characters for the page. Note thatmetaelements are self-closing.To prepare to create some actual content, add a
bodyelement below theheadelement.It’s time to add some menu content. Add a
mainelement within the existingbodyelement. It will eventually contain pricing information about coffee and desserts offered by the cafe.The name of the cafe is
CAMPER CAFE. Add anh1element within yourmainelement. Give it the name of the cafe in capitalized letters to make it stand out.To let visitors know the cafe was founded in 2020, add a
pelement below theh1element with the textEst. 2020.There will be two sections on the menu, one for coffees and one for desserts. Add a
sectionelement within themainelement so you have a place to put all the coffees available.Create an
h2element in thesectionelement and give it the textCoffee.Up until now, you have been limited regarding the presentation and appearance of the content you create. To start taking control, add a
styleelement within theheadelement.You can add style to an element by specifying it in the
styleelement and setting a property for it like this:1
2
3element {
property: value;
}Center your
h1element by setting itstext-alignproperty to the valuecenter.n the previous step, you used a type selector to style the
h1element. Center theh2andpelements by adding a new type selector for each one to the existingstyleelement.You now have three type selectors with the exact same styling. You can add the same group of styles to many elements by creating a list of selectors. Each selector is separated with commas like this:
1
2
3selector1, selector2 {
property: value;
}Delete the three existing type selectors and replace them with one selector list that centers the text for the
h1,h2, andpelements.You have styled three elements by writing CSS inside the
styletags. This works, but since there will be many more styles, it’s best to put all the styles in a separate file and link to it.We have created a separate
styles.cssfile for you and switched the editor view to that file. You can change between files with the tabs at the top of the editor.Start by rewriting the styles you have created into the
styles.cssfile. Make sure to exclude the opening and closingstyletags.Now that you have the CSS in the
styles.cssfile, go ahead and remove thestyleelement and all its content. Once it is removed, the text that was centered will shift back to the left.Now you need to link the
styles.cssfile so the styles will be applied again. Nest a self-closinglinkelement in theheadelement. Give it arelattribute valuestylesheetand anhrefattribute value ofstyles.css.For the styling of the page to look similar on mobile as it does on a desktop or laptop, you need to add a
metaelement with a specialcontentattribute.Add the following within the
headelement:1
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
The text is centered again so the link to the CSS file is working. Add another style to the file that changes the
background-colorproperty tobrownfor thebodyelement.That brown background makes it hard to read the text. Change the
bodyelement’s background color to beburlywoodso it has some color but you are still be able to read the text.The
divelement is used mainly for design layout purposes unlike the other content elements you have used so far. Add adivelement inside thebodyelement and then move all the other elements inside the newdiv.The goal now is to make the
divnot take up the entire width of the page. The CSSwidthproperty is perfect for this. Create a new type selector in the style sheet that gives yourdivelement a width of300px.Comments in CSS look like this:
1
/* comment here */
In your style sheet, comment out the line containing the
background-colorproperty and value, so you can see the effect of only stylingdivelement. This will make the background white again.Now use the existing
divselector to set the background color of thedivelement to beburlywood.Now it’s easy to see that the text is centered inside the
divelement. Currently, the width of thedivelement is specified in pixels (px). Change thewidthproperty’s value to be80%, to make it 80% the width of its parent element (body).Next, you want to center the
divhorizontally. You can do this by setting itsmargin-leftandmargin-rightproperties toauto. Think of the margin as invisible space around an element. Using these two margin properties, center thedivelement within thebodyelement.So far you have been using type selectors to style elements. A class selector is defined by a name with a dot directly in front of it, like this:
1
2
3.class-name {
styles
}Change the existing
divselector into a class selector by replacingdivwith a class namedmenu.To apply the class’s styling to the
divelement, add aclassattribute to thedivelement’s opening tag and set its value tomenu.Since the cafe’s main product for sale is coffee, you could use an image of coffee beans for the background of the page.
Delete the comment and its contents inside the
bodytype selector. Now add abackground-imageproperty and set its value tourl(https://cdn.freecodecamp.org/curriculum/css-cafe/beans.jpg).It’s looking good. Time to start adding some menu items. Add an empty
articleelement under theCoffeeheading. It will contain a flavor and price of each coffee you currently offer.articleelements commonly contain multiple elements that have related information. In this case, it will contain a coffee flavor and a price for that flavor. Nest twopelements inside yourarticleelement. The first one’s text should beFrench Vanilla, and the second’s text3.00.Starting below the existing coffee/price pair, add the following coffee and prices using
articleelements with two nestedpelements inside each. As before, the firstpelement’s text should contain the coffee flavor and the secondpelement’s text should contain the price.1
2
3
4Caramel Macchiato 3.75
Pumpkin Spice 3.50
Hazelnut 4.00
Mocha 4.50The flavors and prices are currently stacked on top of each other and centered with their respective
pelements. It would be nice if the flavor was on the left and the price was on the right.Add the class name
flavorto theFrench Vanillapelement.Using your new
flavorclass as a selector, set thetext-alignproperty’s value toleft.Next, you want to align the price to the right. Add a class named
priceto yourpelement that has3.00as its text.Now align the text to the
rightfor the elements with thepriceclass.That is kind of what you want, but now it would be nice if the flavor and price were on the same line.
pelements are block-level elements, so they take up the entire width of their parent element.To get them on the same line, you need to apply some styling to the
pelements so they behave more like inline elements. To do that, start by adding aclassattribute with the valueitemto the firstarticleelement under theCoffeeheading.The
pelements are nested in anarticleelement with the class attribute ofitem. You can style all thepelements nested anywhere in elements with a class nameditemlike this:1
.item p { }
Using the above selector, add a
displayproperty with valueinline-blockso thepelements behave more like inline elements.That’s closer, but the price didn’t stay over on the right. This is because
inline-blockelements only take up the width of their content. To spread them out, add awidthproperty to theflavorandpriceclass selectors that have a value of50%each.Well that did not work. Styling the
pelements asinline-blockand placing them on separate lines in the code creates an extra space to the right of the firstpelement, causing the second one to shift to the next line. One way to fix this is to make eachpelement’s width a little less than50%.Change the
widthvalue to49%for each class to see what happens.That worked, but there is still a little space on the right of the price.
You could keep trying various percentages for the widths. Instead, use the back space key on your keyboard to move the
pelement with the classpricenext to thepelement with the classflavorso that they are on the same line in the editor. Make sure there is no space between them.Now go ahead and change both the
flavorandpriceclass’ widths to be50%again.Now that you know it works, you can change the remaining
articleandpelements to match the first set. Start by adding the classitemto the otherarticleelements.Next, position the other
pelements to be on the same line with no space between them.To complete the styling, add the applicable class names
flavorandpriceto all the remainingpelements.If you make the width of the page preview smaller, you will notice at some point, some of the text on the left starts wrapping around to the next line. This is because the width of the
pelements on the left side can only take up50%of the space.Since you know the prices on the right have significantly fewer characters, change the
flavorclasswidthvalue to be75%and thepriceclasswidthvalue to be25%.You will come back to styling the menu in a few steps, but for now, go ahead and add a second
sectionelement below the first for displaying the desserts offered by the cafe.Add an
h2element in the new section and give it the textDesserts.Add an empty
articleelement under theDessertsheading. Give it aclassattribute with the valueitem.Nest two
pelements inside yourarticleelement. The first one’s text should beDonut, and the second’s text1.50. Put both of them on the same line making sure there is no space between them.For the two
pelements you just added, adddessertas the value of the firstpelement’sclassattribute and the valuepriceas the secondpelementsclassattribute.Something does not look right. You added the correct
classattribute value to thepelement withDonutas its text, but you have not defined a selector for it.The CSS rule for the
flavorclass already sets the properties you want. Add thedessertclass as an additional selector for this CSS rule.Below the dessert you just added, add the rest of the desserts and prices using three more
articleelements, each with two nestedpelements. Each element should have the correct dessert and price text, and all of them should have the correct classes.1
2
3Cherry Pie 2.75
Cheesecake 3.00
Cinnamon Roll 2.50You can give your menu some space between the content and the sides with various
paddingproperties.Give the
menuclass apadding-leftand apadding-rightwith the same value20px.That looks better. Now try to add the same
20pxpadding to the top and bottom of the menu.Since all 4 sides of the menu have the same internal spacing, go ahead and delete the four properties and use a single
paddingproperty with the value20px.The current width of the menu will always take up 80% of the
bodyelement’s width. On a very wide screen, the coffee and dessert appear far apart from their prices.Add a
max-widthproperty to themenuclass with a value of500pxto prevent it from growing too wide.You can change the
font-familyof text, to make it look different from the default font of your browser. Each browser has some common fonts available to it.Change all the text in your
body, by adding afont-familyproperty with the valuesans-serif. This is a fairly common font that is very readable.It is a bit boring for all the text to have the same
font-family. You can still have the majority of the textsans-serifand make just theh1andh2elements different using a different selector.Style both the
h1and theh2elements so that only these elements’ text useImpactfont.You can add a fallback value for the font-family by adding another font name separated by a comma. Fallbacks are used in instances where the initial is not found/available.
Add the fallback font
serifafter theImpactfont.Make the
Est. 2020text italicized by creating anestablishedclass selector and giving it thefont-styleproperty with the valueitalic.Now apply the
establishedclass to theEst. 2020text.The typography of heading elements (e.g.
h1,h2) is set by default values of users’ browsers.Add two new type selectors (
h1andh2). Use thefont-sizeproperty for both, but use the value40pxfor theh1and30pxfor theh2.Add a
footerelement below themainelement, where you can add some additional information.Inside the
footer, add apelement. Then, nest an anchor (a) element in thepthat links tohttps://www.freecodecamp.organd has the textVisit our website.Add a second
pelement below the one with the link and give it the text123 Free Code Camp Drive.You can use an
hrelement to display a divider between sections of different content.First, add an
hrelement between thepelement with the classestablishedand the firstsectionelement. Note thathrelements are self closing.The default properties of an
hrelement will make it appear as a thin light grey line. You can change the height of the line by specifying a value for theheightproperty.Change the height of the
hrelement to be3px.Change the background color of the
hrelement tobrownso it matches the color of the coffee beans.Notice the grey color along the edges of the line. Those edges are known as borders. Each side of an element can have a different color or they can all be the same.
Make all the edges of the
hrelement the same color as the background of it using theborder-colorproperty.Notice how the thickness of the line looks bigger? The default value of a property named
border-widthis1pxfor all edges ofhrelements. By changing the border to the same color as the background, the total height of the line is5px(3pxplus the top and bottom border width of1px).Change the
heightproperty of thehrto be2px, so the total height of it becomes4px.Go ahead and add another
hrelement between themainelement and thefooterelement.To create a little more room around the menu, add
20pxof space on the inside of thebodyelement by using thepaddingproperty.Focusing on the menu items and prices, there is a fairly large gap between each line.
Target all the
pelements nested in elements with theclassnameditemand set their top and bottom margin to be5px.Using the same style selector in the previous step, make the font size of the items and prices larger by using a value of
18px.Changing the
margin-bottomto5pxlooks great. However, now the space between theCinnamon Rollmenu item and the secondhrelement does not match the space between the tophrelement and theCoffeeheading.Add some more space by creating a class named
bottom-lineusing25pxfor themargin-topproperty.Now add the
bottom-lineclass to the secondhrelement so the styling is applied.Next you are going to be styling the
footerelement. To keep the CSS organized, add a comment at the end ofstyles.csswith the textFOOTER.Moving down to the
footerelement, make all the text have a value of14pxfor the font size.The default color of a link that has not yet been clicked on is typically blue. The default color of a link that has already been visited from a page is typically purple.
To make the
footerlinks the same color regardless if a link has been visited, use a type selector for the anchor element (a) and use the valueblackfor thecolorproperty.You change properties of a link when the link has actually been visited by using a pseudo-selector that looks like
a:visited { propertyName: propertyValue; }.Change the color of the footer
Visit our websitelink to begreywhen a user has visited the link.You change properties of a link when the mouse hovers over them by using a pseudo-selector that looks like
a:hover { propertyName: propertyValue; }.Change the color of the footer
Visit our websitelink to bebrownwhen a user hovers over it.You change properties of a link when the link is actually being clicked by using a pseudo-selector that looks like
a:active { propertyName: propertyValue; }.Change the color of the footer
Visit our websitelink to bewhitewhen clicked on.To keep with the same color theme you have already been using (black and brown), change the color for when the link is visited to
blackand usebrownfor when the link is actually clicked.The menu text
CAMPER CAFEhas a different space from the top than the address’s space at the bottom of the menu. This is due to the browser having some default top margin for theh1element.Change the top margin of the
h1element to0to remove all the top margin.To remove some of the vertical space between the
h1element and the textEst. 2020, change the bottom margin of theh1to15px.Now the top spacing looks good. The space below the address at the bottom of the menu is a little bigger than the space at the top of the menu and the
h1element.To decrease the default margin space below the address
pelement, create a class selector namedaddressand use the value5pxfor themargin-bottomproperty.Now apply the
addressclass to thepelement containing the street address123 Free Code Camp Drive.The menu looks good, but other than the coffee beans background image, it is mainly just text.
Under the
Coffeeheading, add an image using the urlhttps://cdn.freecodecamp.org/curriculum/css-cafe/coffee.jpg. Give the image analtvalue ofcoffee icon.The image you added is not centered horizontally like the
Coffeeheading above it.imgelements are “like” inline elements.To make the image behave like heading elements (which are block-level), create an
imgtype selector and use the valueblockfor thedisplayproperty and use the applicablemargin-leftandmargin-rightvalues to center it horizontally.Add one last image under the
Dessertsheading using the urlhttps://cdn.freecodecamp.org/curriculum/css-cafe/pie.jpg. Give the image analtvalue ofpie icon.It would be nice if the vertical space between the
h2elements and their associated icons was smaller. Theh2elements have default top and bottom margin space, so you could change the bottom margin of theh2elements to say0or another number.There is an easier way, simply add a negative top margin to the
imgelements to pull them up from their current positions. Negative values are created using a-in front of the value. To complete this project, go ahead and use a negative top margin of25pxin theimgtype selector.```html
Cafe Menu 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
```css
body {
/*background-color: burlywood;*/
background-image:url(https://cdn.freecodecamp.org/curriculum/css-cafe/beans.jpg)
font-family:sans-serif;
padding:20px;
}
.menu{
width:80%;
background-color: burlywood;
margin-left:auto;
margin-right:auto;
/* padding-left:20px;
padding-right:20px;
padding-top: 20px;
padding-bottom: 20px;*/
padding:20px;
max-width:500px;
}
hr{
height:2px;
background-color:brown;
border-color:brown;
}
.bottom-line{
margin-top:25px;
}
h1{
font-size:40px;
margin-top:0px;
margin-bottom:15px;
}
h2{
font-size:30px;
}
h1,
h2,
p {
text-align:center;
}
h1,
h2{
font-family: Impact,serif;;
}
.flavor,
.dessert {
text-align: left;
width:75%;
}
.price {
text-align: right;
width:25%;
}
.item p{
display:inline-block;
margin-top:5px;
margin-bottom:5px;
font-size:18px;
}
.established{
font-style:italic;
}
img{
display:block;
margin-left:auto;
margin-right:auto;
margin-top:-25px;
}
/* FOOTER */
footer{
font-size:14px;
}
a{
color:black;
}
a:visited {
color: black;
}
a:hover {
color: brown;
}
a:active {
color: brown;
}
.address{
margin-bottom:5px;
}