Step 62
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
.h1{ font-size:40px } h2{ font-size:30px; }
Step 63
Add a
footer
element below themain
element, where you can add some additional information.<main> <h1>CAMPER CAFE</h1> <p class="established">Est. 2020</p> <section> <h2>Coffee</h2> <article class="item"> <p class="flavor">French Vanilla</p><p class="price">3.00</p> </article> <article class="item"> <p class="flavor">Caramel Macchiato</p><p class="price">3.75</p> </article> <article class="item"> <p class="flavor">Pumpkin Spice</p><p class="price">3.50</p> </article> <article class="item"> <p class="flavor">Hazelnut</p><p class="price">4.00</p> </article> <article class="item"> <p class="flavor">Mocha</p><p class="price">4.50</p> </article> </section> <section> <h2>Desserts</h2> <article class="item"> <p class="dessert">Donut</p><p class="price">1.50</p> </article> <article class="item"> <p class="dessert">Cherry Pie</p><p class="price">2.75</p> </article> <article class="item"> <p class="dessert">Cheesecake</p><p class="price">3.00</p> </article> <article class="item"> <p class="dessert">Cinnamon Roll</p><p class="price">2.50</p> </article> </section> </main> <footer></footer>
Step 64
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
.<footer> <p><a href="https://www.freecodecamp.org">Visit our website</a></p> </footer>
Step 65
Add a second
p
element below the one with the link and give it the text123 Free Code Camp Drive
.<p> <a href="https://www.freecodecamp.org" target="_blank">Visit our website</a> </p> <p> <a href="https://www.freecodecamp.org" target="_blank">123 Free Code Camp Drive</a> </p>
Step 66
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.<main> <h1>CAMPER CAFE</h1> <p class="established">Est. 2020</p> <hr> <section> <h2>Coffee</h2> <article class="item"> <p class="flavor">French Vanilla</p><p class="price">3.00</p> </article> <article class="item"> <p class="flavor">Caramel Macchiato</p><p class="price">3.75</p> </article> <article class="item"> <p class="flavor">Pumpkin Spice</p><p class="price">3.50</p> </article> <article class="item"> <p class="flavor">Hazelnut</p><p class="price">4.00</p> </article> <article class="item"> <p class="flavor">Mocha</p><p class="price">4.50</p> </article> </section> <section> <h2>Desserts</h2> <article class="item"> <p class="dessert">Donut</p><p class="price">1.50</p> </article> <article class="item"> <p class="dessert">Cherry Pie</p><p class="price">2.75</p> </article> <article class="item"> <p class="dessert">Cheesecake</p><p class="price">3.00</p> </article> <article class="item"> <p class="dessert">Cinnamon Roll</p><p class="price">2.50</p> </article> </section> </main>
Step 67
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
.hr{ height:3px; }
Step 68
Change the background color of the
hr
element tobrown
so it matches the color of the coffee beans.hr { height: 3px; background-color:brown; }
Step 69
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.hr { height: 3px; background-color: brown; border-color:brown; }
Step 70
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
.hr { height: 2px; background-color: brown; border-color: brown; }
Step 71
Go ahead and add another
hr
element between themain
element and thefooter
element.<main> <h1>CAMPER CAFE</h1> <p class="established">Est. 2020</p> <hr> <section> <h2>Coffee</h2> <article class="item"> <p class="flavor">French Vanilla</p><p class="price">3.00</p> </article> <article class="item"> <p class="flavor">Caramel Macchiato</p><p class="price">3.75</p> </article> <article class="item"> <p class="flavor">Pumpkin Spice</p><p class="price">3.50</p> </article> <article class="item"> <p class="flavor">Hazelnut</p><p class="price">4.00</p> </article> <article class="item"> <p class="flavor">Mocha</p><p class="price">4.50</p> </article> </section> <section> <h2>Desserts</h2> <article class="item"> <p class="dessert">Donut</p><p class="price">1.50</p> </article> <article class="item"> <p class="dessert">Cherry Pie</p><p class="price">2.75</p> </article> <article class="item"> <p class="dessert">Cheesecake</p><p class="price">3.00</p> </article> <article class="item"> <p class="dessert">Cinnamon Roll</p><p class="price">2.50</p> </article> </section> </main> <hr> <footer> <p> <a href="https://www.freecodecamp.org" target="_blank">Visit our website</a> </p> <p>123 Free Code Camp Drive</p> </footer>
Step 72
To create a little more room around the menu, add
20px
of space on the inside of thebody
element by using thepadding
property.body { background-image: url(https://cdn.freecodecamp.org/curriculum/css-cafe/beans.jpg); font-family: sans-serif; padding:20px; }
Step 73
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
.h1, h2 { font-family: Impact, serif; } .item p { display: inline-block; margin-top:5px; margin-bottom:5px; } .flavor, .dessert { text-align: left; width: 75%; } .price { text-align: right; width: 25% }
Step 74
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
..item p { display: inline-block; margin-top: 5px; margin-bottom: 5px; font-size:18px; }
Step 75
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..bottom-line{ margin-top:25px; }
Step 76
Now add the
bottom-line
class to the secondhr
element so the styling is applied.<hr class="bottom-line">
Step 77
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
./*FOOTER*/
Step 78
Moving down to the
footer
element, make all the text have a value of14px
for the font size.footer{ font-size:14px; }
Step 79
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.a{ color:black; }
Step 80
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.a:visited{ color:grey; }
什么是伪类?
伪类用于定义元素的特殊状态。
例如,它可以用于:
- 设置鼠标悬停在元素上时的样式
- 为已访问和未访问链接设置不同的样式
- 设置元素获得焦点时的样式
语法
伪类的语法:
selector:pseudo-class { property: value; }
锚伪类
链接能够以不同的方式显示:
/* 未访问的链接 */ a:link { color: #FF0000; } /* 已访问的链接 */ a:visited { color: #00FF00; } /* 鼠标悬停链接 */ a:hover { color: #FF00FF; } /* 已选择的链接 */ a:active { color: #0000FF; }
注意: 在CSS定义中,a:hover 必须被置于 a:link 和 a:visited 之后,才是有效的。
注意: 在 CSS 定义中,a:active 必须被置于 a:hover 之后,才是有效的。
注意:伪类的名称不区分大小写。
Step 81
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.a:hover{ color:brown; }
Step 82
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.a:active{ color:white; }
Step 83
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.a:visited { color:black; } a:hover { color: brown; } a:active { color:brown; }
Step 84
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.h1 { font-size: 40px; margin-top:0px; }
Step 85
To remove some of the vertical space between the
h1
element and the textEst. 2020
, change the bottom margin of theh1
to15px
.h1 { font-size: 40px; margin-top: 0; margin-bottom:15px; }
Step 86
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..address{ margin-bottom:5px; }
Step 87
Now apply the
address
class to thep
element containing the address.<footer> <p> <a href="https://www.freecodecamp.org" target="_blank">Visit our website</a> </p> <p class="address">123 Free Code Camp Drive</p> </footer>
Step 88
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
.<h2>Coffee</h2> <img src="https://cdn.freecodecamp.org/curriculum/css-cafe/coffee.jpg" alt="coffee icon">
Step 89
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.img{ display:block; margin-left:auto; margin-right:auto; }
Step 90
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
.<h2>Desserts</h2> <img src="https://cdn.freecodecamp.org/curriculum/css-cafe/pie.jpg" alt="pie icon">
Step 91
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.img { display: block; margin-left: auto; margin-right: auto; margin-top:-25px; }