Step 31
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.Caramel Macchiato 3.75 Pumpkin Spice 3.50 Hazelnut 4.00 Mocha 4.50
<article> <p>French Vanilla</p> <p>3.00</p> </article> <article> <p>Caramel Macchiato</p> <p>3.75</p> </article> <article> <p>Pumpkin Spice</p> <p>3.50</p> </article> <article> <p>Hazelnut</p> <p>4.00</p> </article> <article> <p>Mocha</p> <p>4.50</p> </article>
Step 32
The 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.<p class="flavor">French Vanilla</p> <p>3.00</p>
Step 33
Using your new
flavor
class as a selector, set thetext-align
property's value toleft
..flavor{ text-align:left }
text-align属性指定元素文本的水平对齐方式
值 描述 left 把文本排列到左边。默认值:由浏览器决定。 right 把文本排列到右边。 center 把文本排列到中间。 justify 实现两端对齐文本效果。 inherit 规定应该从父元素继承 text-align 属性的值。
Step 34
Next, you want to align the price to the right. Add a class named
price
to yourp
element that has3.00
as its text.<p class="price">3.00</p>
Step 35
Now align the text to the
right
for the elements with theprice
class..price{ text-align:right; }
Step 36
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. Add aclass
attribute with the valueitem
to the firstarticle
element under theCoffee
heading.<h2>Coffee</h2> <article class="item"> <p class="flavor">French Vanilla</p> <p class="price">3.00</p> </article>
Step 37
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:.item p { }
Using the above selector, add a
display
property with valueinline-block
so thep
elements behave more like inline elements..item p{ display:inline-block; }
display 属性规定元素应该生成的框的类型
none 此元素不会被显示。 block 此元素将显示为块级元素,此元素前后会带有换行符。 inline 默认。此元素会被显示为内联元素,元素前后没有换行符。 inline-block 行内块元素。(CSS2.1 新增的值) list-item 此元素会作为列表显示。 run-in 此元素会根据上下文作为块级元素或内联元素显示。 compact CSS 中有值 compact,不过由于缺乏广泛支持,已经从 CSS2.1 中删除。 marker CSS 中有值 marker,不过由于缺乏广泛支持,已经从 CSS2.1 中删除。 table 此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符。 inline-table 此元素会作为内联表格来显示(类似 <table>),表格前后没有换行符。 table-row-group 此元素会作为一个或多个行的分组来显示(类似 <tbody>)。 table-header-group 此元素会作为一个或多个行的分组来显示(类似 <thead>)。 table-footer-group 此元素会作为一个或多个行的分组来显示(类似 <tfoot>)。 table-row 此元素会作为一个表格行显示(类似 <tr>)。 table-column-group 此元素会作为一个或多个列的分组来显示(类似 <colgroup>)。 table-column 此元素会作为一个单元格列显示(类似 <col>) table-cell 此元素会作为一个表格单元格显示(类似 <td> 和 <th>) table-caption 此元素会作为一个表格标题显示(类似 <caption>) inherit 规定应该从父元素继承 display 属性的值。
Step 38
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..flavor { text-align: left; width:50%; } .price { text-align: right; width:50%; }
Step 39
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..flavor { text-align: left; width: 49%; } .price { text-align: right; width: 49%; }
Step 40
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.<p class="flavor">French Vanilla</p><p class="price">3.00</p>
Step 41
Now go ahead and change both the
flavor
andprice
class' widths to be50%
again..flavor { text-align: left; width: 50%; } .price { text-align: right; width: 50%; }
Step 42
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.<article class="item"> <p>Caramel Macchiato</p> <p>3.75</p> </article> <article class="item"> <p>Pumpkin Spice</p> <p>3.50</p> </article> <article class="item"> <p>Hazelnut</p> <p>4.00</p> </article> <article class="item"> <p>Mocha</p> <p>4.50</p> </article>
Step 43
Next, position the other
p
elements to be on the same line with no space between them.<article class="item"> <p>Caramel Macchiato</p><p>3.75</p> </article> <article class="item"> <p>Pumpkin Spice</p><p>3.50</p> </article> <article class="item"> <p>Hazelnut</p><p>4.00</p> </article> <article class="item"> <p>Mocha</p><p>4.50</p> </article>
Step 44
To complete the styling, add the applicable class names
flavor
andprice
to all the remainingp
elements.<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>
Step 45
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%
..flavor { text-align: left; width: 75%; } .price { text-align: right; width: 25%; }
Step 46
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.<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></section>
Step 47
Add an
h2
element in the new section and give it the textDesserts
.<section> <h2>Desserts</h2> </section>
Step 48
Add an empty
article
element under theDesserts
heading. Give it aclass
attribute with the valueitem
.<h2>Desserts</h2> <article class="item"></article>
Step 49
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.<article class="item"> <p>Donut</p><p>1.50</p> </article>
Step 50
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.<p class="dessert">Donut</p><p class="price">1.50</p>
Step 51
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.Since the
flavor
class selector already has the properties you want, just add thedessert
class name to it..flavor,.dessert { text-align: left; width: 75%; }
Step 52
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.Cherry Pie 2.75 Cheesecake 3.00 Cinnamon Roll 2.50
<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>
Step 53
You 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
..menu { width: 80%; background-color: burlywood; margin-left: auto; margin-right: auto; padding-left:20px; padding-right:20px; }
Step 54
That looks better. Now try to add the same
20px
padding to the top and bottom of the menu..menu { width: 80%; background-color: burlywood; margin-left: auto; margin-right: auto; padding-left: 20px; padding-right: 20px; padding-top:20px; padding-bottom:20px; }
Step 55
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
..menu { width: 80%; background-color: burlywood; margin-left: auto; margin-right: auto; padding:20px }
Step 56
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..menu { max-width:500px; width: 80%; background-color: burlywood; margin-left: auto; margin-right: auto; padding: 20px; }
Step 57
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.body { background-image: url(https://cdn.freecodecamp.org/curriculum/css-cafe/beans.jpg); font-family:sans-serif; }
Step 58
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.h1,h2{ font-family:Impact; }
Step 59
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.h1, h2 { font-family: Impact,serif; }
Step 60
Make the
Est. 2020
text italicized by creating anestablished
class selector and giving it thefont-style
property with the valueitalic
..established{ font-style:italic; }
Step 61
Now apply the
established
class to theEst. 2020
text.<h1>CAMPER CAFE</h1> <p class="established">Est. 2020</p>
font - family属性指定一个元素的字体
font-family 可以把多个字体名称作为一个"回退"系统来保存。如果浏览器不支持第一个字体,则会尝试下一个。
有两种类型的字体系列名称:
- family-name - 指定的系列名称:具体字体的名称,比如:"times"、"courier"、"arial"。
- generic-family - 通常字体系列名称:比如:"serif"、"sans-serif"、"cursive"、"fantasy"、"monospace"。
使用某种特定的字体系列(Geneva)完全取决于用户机器上该字体系列是否可用;这个属性没有指示任何字体下载。因此,强烈推荐使用一个通用字体系列名作为后路。
注意: 每个值用逗号分开。
注意: 如果字体名称包含空格,它必须加上引号。在HTML中使用"style"属性时,必须使用单引号。
- family-name
- generic-family
用于某个元素的字体族名称或/及类族名称的一个优先表。
默认值:取决于浏览器。
inherit 规定应该从父元素继承字体系列。
font-style属性指定文本的字体样式
normal 默认值。浏览器显示一个标准的字体样式。 italic 浏览器会显示一个斜体的字体样式。 oblique 浏览器会显示一个倾斜的字体样式。 inherit 规定应该从父元素继承字体样式。