Responsive Web Dedign(七)
freeCodeCamp —- Responsive Web Dedign
Learn Typography by Building a Nutrition Label
We’ve provided a basic HTML boilerplate for you.
Create an
h1
element within yourbody
element and give it the textNutrition Facts
.Below your
h1
element, add ap
element with the text8 servings per container
.Add a second
p
element with the textServing size 2/3 cup (55g)
.Within your
head
element, add alink
element with therel
attribute set tostylesheet
and thehref
attribute set tohttps://fonts.googleapis.com/css?family=Open+Sans:400,700,800
.This will import the
Open Sans
font family, with the font weight values400
,700
, and800
.Also add a
link
element to link yourstyles.css
file.Create a
body
selector and give it afont-family
set toOpen Sans
with a fallback ofsans-serif
.Remember that fonts with spaces in the name must be wrapped in quotes for CSS.
The font is a bit small. Create an
html
selector and set the font to have a size of16px
.Wrap your
h1
andp
elements in adiv
element. Give thatdiv
aclass
attribute set tolabel
.Borders can be used to group and prioritize content.
Create a
.label
selector and give it aborder
set to2px solid black
.Good use of white space can bring focus to the important elements of your page, and help guide your user’s eyes through your text.
Give your
.label
selector awidth
property set to270px
.Give your
.label
selector amargin
property set to20px auto
, and apadding
property set to0 7px
.If you inspect your
.label
element with your browser’s developer tools, you may notice that it’s actually 288 pixels wide instead of 270. This is because, by default, the browser includes the border and padding when determining an element’s size.To solve this, reset the box model by creating a
*
selector and giving it abox-sizing
property ofborder-box
.Remember that the use of
h1
,h2
, and similar tags determine the semantic structure of your HTML. However, you can adjust the CSS of these elements to control the visual flow and hierarchy.Create an
h1
rule and set thefont-weight
property to800
. This will make yourh1
text bolder.Give your
h1
selector atext-align
property ofcenter
.Fine-tune the placement of your
h1
by giving it a top and bottom margin of-4px
and a left and right margin of0
.Create a
p
selector and remove all margins.Lines can help separate and group important content, especially when space is limited.
Create a
div
element below yourh1
element, and give it aclass
attribute set todivider
.Create a selector for your new
.divider
and set theborder-bottom
property to1px solid #888989
. Also give it a top and bottom margin of2px
. It should not have any left or right margin.The
letter-spacing
property can be used to adjust the space between each character of text in an element.Give your
h1
selector aletter-spacing
property set to0.15px
to space them out a bit more.Nutrition labels have a lot of bold text to draw attention to important information. Rather than targeting each element that needs to be bold, it is more efficient to use a class to apply the bold styling to every element.
Give your second
p
element aclass
attribute set tobold
.Your new class does not have any styling yet. Create a
.bold
selector and give it afont-weight
property set to800
to make the text bold.Go ahead and remove the
font-weight
property from yourh1
selector as well.Give your
h1
element aclass
attribute set tobold
. This will make the text bold again.Horizontal spacing between equally important elements can increase the readability of your text.
Wrap the text
2/3 cup (55g)
in aspan
element.Now we can add the horizontal spacing using
flex
. In yourp
selector, add adisplay
property set toflex
and ajustify-content
property set tospace-between
.Wrap everything within the
.label
element in a newheader
element.Now update your
h1
selector to beheader h1
to specifically target yourh1
element within your newheader
.Create a new
div
element below yourheader
element, and give it aclass
attribute set todivider large
.Create a new
.large
selector and give it aheight
property set to10px
. Also create an.large, .medium
selector and set thebackground-color
property toblack
.You may notice there is still a small border at the bottom of your
.large
element. To reset this, give your.large, .medium
selector aborder
property set to0
.Note: the
medium
(medium) class will be utilized in step 37 for the thinner bars of the nutrition label.Create a new
div
below your.large
element and give it aclass
attribute set tocalories-info
.Within your
.calories-info
element, create adiv
element. Give thatdiv
element aclass
attribute set toleft-container
. Within the newly createddiv
element, create ah2
element with the textAmount per serving
. Give theh2
element aclass
attribute set tobold small-text
.The
rem
unit stands forroot em
, and is relative to the font size of thehtml
element.Create a
.small-text
selector and set thefont-size
to0.85rem
, which would calculate to roughly13.6px
(remember that you set yourhtml
to have afont-size
of16px
).Create a
.calories-info h2
selector and remove all margins.Below your
.small-text
element, create a newp
element with the textCalories
. Also below the.left-container
element, create a newspan
element with the text230
.Create a new
.calories-info
selector and give it adisplay
property set toflex
. Also give it ajustify-content
property set tospace-between
andalign-items
property set toflex-end
.Create a new
.left-container p
selector setting the top and bottom margin to-5px
, and the left and right margin to-2px
. Also set thefont-size
to2em
andfont-weight
to700
.Create a
.calories-info span
selector, set itsfont-size
to2.4em
andfont-weight
to700
.Typography is often more art than science. You may have to tweak things like alignment until it looks correct.
Give your
.calories-info span
selector amargin
set to-7px -2px
. This will shift your230
text into place.Below your
.calories-info
element, add adiv
with theclass
attribute set todivider medium
.5Create an
.medium
selector and give it aheight
property of5px
.Create a new
div
element below your.medium
element. Give it aclass
attribute set todaily-value small-text
. Within this newdiv
, add ap
element with the text% Daily Value *
, and set theclass
attribute tobold right
.The text
* Daily Value %
should be aligned to the right. Create a.right
selector and use thejustify-content
property to do it.采用justify-content:flex-end;
Use your existing
.divider
element as an example to add a new divider after thep
element.After your last
.divider
element, create ap
element and give it the textTotal Fat 8g 10%
. Wrap the textTotal Fat
in aspan
element with theclass
ofbold
. Wrap the text10%
in anotherspan
element with theclass
ofbold
. Finally, nest theTotal Fat
span
element and the text8g
in an additionalspan
element for alignment.Below your element with the
Total Fat
text, create a newp
element with the textSaturated Fat 1g 5%
. Wrap the5%
in aspan
with theclass
attribute set tobold
. In this case this is enough to align the percentage to5%
.This new
p
element will need to be indented. Give it aclass
set toindent
.Create a new
.indent
selector and give it amargin-left
property set to1em
.Create a
.daily-value p
selector to target all of yourp
elements in thedaily-value
section. Give this new selector aborder-bottom
set to1px solid #888989
.The bottom borders under your
% Daily Value *
andSaturated Fat 1g 5%
elements do not extend the full width of the label. Addno-divider
to theclass
for these two elements.The
:not
pseudo-selector can be used to select all elements that do not match the given CSS rule.1
2
3div:not(#example) {
color: red;
}The above selects all
div
elements without anid
ofexample
.Modify your
.daily-value p
selector to exclude the.no-divider
elements.Now you will have to add separate dividers below your
.no-divider
elements.Your first
.no-divider
element has a.divider
after it. Create another.divider
after your second.no-divider
element.After your last
.divider
, create anotherp
element with the textTrans Fat 0g
. Italicize the wordTrans
by wrapping it in ani
element. Give the newp
element theclass
attribute set toindent no-divider
. WrapTrans Fat 0g
in aspan
element for alignment.Create another
.divider
after your lastp
element.After your last
.divider
, create a newp
element with the textCholesterol 0mg 0%
. Wrap the textCholesterol
in aspan
element, and give thatspan
element theclass
ofbold
. Wrap the text0%
in anotherspan
element, with theclass
ofbold
. Finally, nest theCholesterol
span
element and the text0mg
in an additionalspan
element for alignment.Below your last
p
element, create anotherp
element with the textSodium 160mg 7%
. Wrap the textSodium
in aspan
element with aclass
attribute set tobold
. Wrap the7%
text in anotherspan
element with theclass
set tobold
. Also add an additionalspan
element aroundSodium 160mg
for aligning it correctly.Add another
p
element with the textTotal Carbohydrate 37g 13%
. Like before, usespan
elements to make the textTotal Carbohydrate
and13%
bold. Also add an additionalspan
element to wrap theTotal Carbohydrate 37g
text in a span element so to have it aligned to the left, and13%
to the right.Below your last
p
element, add anotherp
element with the textDietary Fiber 4g
. Give thep
element theclass
necessary to indent it and remove the dividing border. Then create a divider below thatp
element.Create another
p
element after your last.divider
, and give it the textTotal Sugars 12g
. Assign thatp
element theclass
values necessary to indent it and remove the bottom border. Then create another.divider
below your newp
element.The advantage to creating these dividers is that you can apply specific classes to style them individually. Add
double-indent
to theclass
for your last.divider
.Create a
.double-indent
selector and give it a left margin of2em
.Below your
.double-indent
element, add a newp
element with the textIncludes 10g Added Sugars 20%
. Your newp
element should also be double indented, and have no bottom border. Use aspan
to make the20%
bold and right aligned.Then create another divider after that
p
element.After your last divider, create another
p
element with the textProtein 3g
. Use the necessary classes to remove the bottom border, and aspan
to make theProtein
bold.Following this element, create a large divider.
Create another
p
element below your large divider. Give thep
element the textVitamin D 2mcg 10%
.The
p
element contains only text, you can wrap the percentage in aspan
element so that it is considered a separate entity from the rest of the text, and it’s moved to the right.Create another
p
element, give it the textCalcium 260mg 20%
. Align20%
to the right. Below that, create ap
element with the textIron 8mg 45%
, aligning the45%
to the right.Create the final
p
element for your.daily-value
section. Give it the textPotassium 235mg 6%
. Align the6%
text to the right, and remove the bottom border of thep
element.Add a medium divider after your
.daily-value
element. Below that new divider, create ap
element with theclass
attribute set tonote
.Give the
p
element the following text:1
* The % Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.
Create a
.note
selector, and set the size of the font to0.6rem
. Also set the top and bottom margins to5px
, removing the left and right margins.Give the
.note
selector a left and right padding of8px
, removing the top and bottom padding. Also set thetext-indent
property to-8px
.With these last changes, your nutrition label is complete!
1 |
|
1 | * { |