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``en
Add a
head
element within thehtml
element, so you can add atitle
element. Thetitle
element’s text should beCafe Menu
.The
title
is 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
head
element, nest ameta
element with an attribute namedcharset
set to the valueutf-8
to tell the browser how to encode characters for the page. Note thatmeta
elements are self-closing.To prepare to create some actual content, add a
body
element below thehead
element.It’s time to add some menu content. Add a
main
element within the existingbody
element. It will eventually contain pricing information about coffee and desserts offered by the cafe.The name of the cafe is
CAMPER CAFE
. Add anh1
element within yourmain
element. 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
p
element below theh1
element with the textEst. 2020
.There will be two sections on the menu, one for coffees and one for desserts. Add a
section
element within themain
element so you have a place to put all the coffees available.Create an
h2
element in thesection
element 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
style
element within thehead
element.You can add style to an element by specifying it in the
style
element and setting a property for it like this:1
2
3element {
property: value;
}Center your
h1
element by setting itstext-align
property to the valuecenter
.n the previous step, you used a type selector to style the
h1
element. Center theh2
andp
elements by adding a new type selector for each one to the existingstyle
element.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
, andp
elements.You have styled three elements by writing CSS inside the
style
tags. 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.css
file 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.css
file. Make sure to exclude the opening and closingstyle
tags.Now that you have the CSS in the
styles.css
file, go ahead and remove thestyle
element 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.css
file so the styles will be applied again. Nest a self-closinglink
element in thehead
element. Give it arel
attribute valuestylesheet
and anhref
attribute 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
meta
element with a specialcontent
attribute.Add the following within the
head
element: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-color
property tobrown
for thebody
element.That brown background makes it hard to read the text. Change the
body
element’s background color to beburlywood
so it has some color but you are still be able to read the text.The
div
element is used mainly for design layout purposes unlike the other content elements you have used so far. Add adiv
element inside thebody
element and then move all the other elements inside the newdiv
.The goal now is to make the
div
not take up the entire width of the page. The CSSwidth
property is perfect for this. Create a new type selector in the style sheet that gives yourdiv
element a width of300px
.Comments in CSS look like this:
1
/* comment here */
In your style sheet, comment out the line containing the
background-color
property and value, so you can see the effect of only stylingdiv
element. This will make the background white again.Now use the existing
div
selector to set the background color of thediv
element to beburlywood
.Now it’s easy to see that the text is centered inside the
div
element. Currently, the width of thediv
element is specified in pixels (px
). Change thewidth
property’s value to be80%
, to make it 80% the width of its parent element (body
).Next, you want to center the
div
horizontally. You can do this by setting itsmargin-left
andmargin-right
properties toauto
. Think of the margin as invisible space around an element. Using these two margin properties, center thediv
element within thebody
element.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
div
selector into a class selector by replacingdiv
with a class namedmenu
.To apply the class’s styling to the
div
element, add aclass
attribute to thediv
element’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
body
type selector. Now add abackground-image
property 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
article
element under theCoffee
heading. It will contain a flavor and price of each coffee you currently offer.article
elements commonly contain multiple elements that have related information. In this case, it will contain a coffee flavor and a price for that flavor. Nest twop
elements inside yourarticle
element. 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
article
elements with two nestedp
elements inside each. As before, the firstp
element’s text should contain the coffee flavor and the secondp
element’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
p
elements. It would be nice if the flavor was on the left and the price was on the right.Add the class name
flavor
to theFrench Vanilla
p
element.Using your new
flavor
class as a selector, set thetext-align
property’s value toleft
.Next, you want to align the price to the right. Add a class named
price
to yourp
element that has3.00
as its text.Now align the text to the
right
for the elements with theprice
class.That is kind of what you want, but now it would be nice if the flavor and price were on the same line.
p
elements 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
p
elements so they behave more like inline elements. To do that, start by adding aclass
attribute with the valueitem
to the firstarticle
element under theCoffee
heading.The
p
elements are nested in anarticle
element with the class attribute ofitem
. You can style all thep
elements nested anywhere in elements with a class nameditem
like this:1
.item p { }
Using the above selector, add a
display
property with valueinline-block
so thep
elements behave more like inline elements.That’s closer, but the price didn’t stay over on the right. This is because
inline-block
elements only take up the width of their content. To spread them out, add awidth
property to theflavor
andprice
class selectors that have a value of50%
each.Well that did not work. Styling the
p
elements asinline-block
and placing them on separate lines in the code creates an extra space to the right of the firstp
element, causing the second one to shift to the next line. One way to fix this is to make eachp
element’s width a little less than50%
.Change the
width
value 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
p
element with the classprice
next to thep
element with the classflavor
so 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
flavor
andprice
class’ widths to be50%
again.Now that you know it works, you can change the remaining
article
andp
elements to match the first set. Start by adding the classitem
to the otherarticle
elements.Next, position the other
p
elements to be on the same line with no space between them.To complete the styling, add the applicable class names
flavor
andprice
to all the remainingp
elements.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
p
elements 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
flavor
classwidth
value to be75%
and theprice
classwidth
value to be25%
.You will come back to styling the menu in a few steps, but for now, go ahead and add a second
section
element below the first for displaying the desserts offered by the cafe.Add an
h2
element in the new section and give it the textDesserts
.Add an empty
article
element under theDesserts
heading. Give it aclass
attribute with the valueitem
.Nest two
p
elements inside yourarticle
element. 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
p
elements you just added, adddessert
as the value of the firstp
element’sclass
attribute and the valueprice
as the secondp
elementsclass
attribute.Something does not look right. You added the correct
class
attribute value to thep
element withDonut
as its text, but you have not defined a selector for it.The CSS rule for the
flavor
class already sets the properties you want. Add thedessert
class 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
article
elements, each with two nestedp
elements. 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
padding
properties.Give the
menu
class apadding-left
and apadding-right
with the same value20px
.That looks better. Now try to add the same
20px
padding 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
padding
property with the value20px
.The current width of the menu will always take up 80% of the
body
element’s width. On a very wide screen, the coffee and dessert appear far apart from their prices.Add a
max-width
property to themenu
class with a value of500px
to prevent it from growing too wide.You can change the
font-family
of 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-family
property 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-serif
and make just theh1
andh2
elements different using a different selector.Style both the
h1
and theh2
elements so that only these elements’ text useImpact
font.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
serif
after theImpact
font.Make the
Est. 2020
text italicized by creating anestablished
class selector and giving it thefont-style
property with the valueitalic
.Now apply the
established
class to theEst. 2020
text.The typography of heading elements (e.g.
h1
,h2
) is set by default values of users’ browsers.Add two new type selectors (
h1
andh2
). Use thefont-size
property for both, but use the value40px
for theh1
and30px
for theh2
.Add a
footer
element below themain
element, where you can add some additional information.Inside the
footer
, add ap
element. Then, nest an anchor (a
) element in thep
that links tohttps://www.freecodecamp.org
and has the textVisit our website
.Add a second
p
element below the one with the link and give it the text123 Free Code Camp Drive
.You can use an
hr
element to display a divider between sections of different content.First, add an
hr
element between thep
element with the classestablished
and the firstsection
element. Note thathr
elements are self closing.The default properties of an
hr
element will make it appear as a thin light grey line. You can change the height of the line by specifying a value for theheight
property.Change the height of the
hr
element to be3px
.Change the background color of the
hr
element tobrown
so 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
hr
element the same color as the background of it using theborder-color
property.Notice how the thickness of the line looks bigger? The default value of a property named
border-width
is1px
for all edges ofhr
elements. By changing the border to the same color as the background, the total height of the line is5px
(3px
plus the top and bottom border width of1px
).Change the
height
property of thehr
to be2px
, so the total height of it becomes4px
.Go ahead and add another
hr
element between themain
element and thefooter
element.To create a little more room around the menu, add
20px
of space on the inside of thebody
element by using thepadding
property.Focusing on the menu items and prices, there is a fairly large gap between each line.
Target all the
p
elements nested in elements with theclass
nameditem
and 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-bottom
to5px
looks great. However, now the space between theCinnamon Roll
menu item and the secondhr
element does not match the space between the tophr
element and theCoffee
heading.Add some more space by creating a class named
bottom-line
using25px
for themargin-top
property.Now add the
bottom-line
class to the secondhr
element so the styling is applied.Next you are going to be styling the
footer
element. To keep the CSS organized, add a comment at the end ofstyles.css
with the textFOOTER
.Moving down to the
footer
element, make all the text have a value of14px
for 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
footer
links the same color regardless if a link has been visited, use a type selector for the anchor element (a
) and use the valueblack
for thecolor
property.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 website
link to begrey
when 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 website
link to bebrown
when 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 website
link to bewhite
when 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
black
and usebrown
for when the link is actually clicked.The menu text
CAMPER CAFE
has 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 theh1
element.Change the top margin of the
h1
element to0
to remove all the top margin.To remove some of the vertical space between the
h1
element and the textEst. 2020
, change the bottom margin of theh1
to15px
.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
h1
element.To decrease the default margin space below the address
p
element, create a class selector namedaddress
and use the value5px
for themargin-bottom
property.Now apply the
address
class to thep
element 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
Coffee
heading, add an image using the urlhttps://cdn.freecodecamp.org/curriculum/css-cafe/coffee.jpg
. Give the image analt
value ofcoffee icon
.The image you added is not centered horizontally like the
Coffee
heading above it.img
elements are “like” inline elements.To make the image behave like heading elements (which are block-level), create an
img
type selector and use the valueblock
for thedisplay
property and use the applicablemargin-left
andmargin-right
values to center it horizontally.Add one last image under the
Desserts
heading using the urlhttps://cdn.freecodecamp.org/curriculum/css-cafe/pie.jpg
. Give the image analt
value ofpie icon
.It would be nice if the vertical space between the
h2
elements and their associated icons was smaller. Theh2
elements have default top and bottom margin space, so you could change the bottom margin of theh2
elements to say0
or another number.There is an easier way, simply add a negative top margin to the
img
elements 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 of25px
in theimg
type 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;
}