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
h1element within yourbodyelement and give it the textNutrition Facts.Below your
h1element, add apelement with the text8 servings per container.Add a second
pelement with the textServing size 2/3 cup (55g).Within your
headelement, add alinkelement with therelattribute set tostylesheetand thehrefattribute set tohttps://fonts.googleapis.com/css?family=Open+Sans:400,700,800.This will import the
Open Sansfont family, with the font weight values400,700, and800.Also add a
linkelement to link yourstyles.cssfile.Create a
bodyselector and give it afont-familyset toOpen Sanswith 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
htmlselector and set the font to have a size of16px.Wrap your
h1andpelements in adivelement. Give thatdivaclassattribute set tolabel.Borders can be used to group and prioritize content.
Create a
.labelselector and give it aborderset 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
.labelselector awidthproperty set to270px.Give your
.labelselector amarginproperty set to20px auto, and apaddingproperty set to0 7px.If you inspect your
.labelelement 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-sizingproperty 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
h1rule and set thefont-weightproperty to800. This will make yourh1text bolder.Give your
h1selector atext-alignproperty ofcenter.Fine-tune the placement of your
h1by giving it a top and bottom margin of-4pxand a left and right margin of0.Create a
pselector and remove all margins.Lines can help separate and group important content, especially when space is limited.
Create a
divelement below yourh1element, and give it aclassattribute set todivider.Create a selector for your new
.dividerand set theborder-bottomproperty to1px solid #888989. Also give it a top and bottom margin of2px. It should not have any left or right margin.The
letter-spacingproperty can be used to adjust the space between each character of text in an element.Give your
h1selector aletter-spacingproperty set to0.15pxto 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
pelement aclassattribute set tobold.Your new class does not have any styling yet. Create a
.boldselector and give it afont-weightproperty set to800to make the text bold.Go ahead and remove the
font-weightproperty from yourh1selector as well.Give your
h1element aclassattribute 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 aspanelement.Now we can add the horizontal spacing using
flex. In yourpselector, add adisplayproperty set toflexand ajustify-contentproperty set tospace-between.Wrap everything within the
.labelelement in a newheaderelement.Now update your
h1selector to beheader h1to specifically target yourh1element within your newheader.Create a new
divelement below yourheaderelement, and give it aclassattribute set todivider large.Create a new
.largeselector and give it aheightproperty set to10px. Also create an.large, .mediumselector and set thebackground-colorproperty toblack.You may notice there is still a small border at the bottom of your
.largeelement. To reset this, give your.large, .mediumselector aborderproperty set to0.Note: the
medium(medium) class will be utilized in step 37 for the thinner bars of the nutrition label.Create a new
divbelow your.largeelement and give it aclassattribute set tocalories-info.Within your
.calories-infoelement, create adivelement. Give thatdivelement aclassattribute set toleft-container. Within the newly createddivelement, create ah2element with the textAmount per serving. Give theh2element aclassattribute set tobold small-text.The
remunit stands forroot em, and is relative to the font size of thehtmlelement.Create a
.small-textselector and set thefont-sizeto0.85rem, which would calculate to roughly13.6px(remember that you set yourhtmlto have afont-sizeof16px).Create a
.calories-info h2selector and remove all margins.Below your
.small-textelement, create a newpelement with the textCalories. Also below the.left-containerelement, create a newspanelement with the text230.Create a new
.calories-infoselector and give it adisplayproperty set toflex. Also give it ajustify-contentproperty set tospace-betweenandalign-itemsproperty set toflex-end.Create a new
.left-container pselector setting the top and bottom margin to-5px, and the left and right margin to-2px. Also set thefont-sizeto2emandfont-weightto700.Create a
.calories-info spanselector, set itsfont-sizeto2.4emandfont-weightto700.Typography is often more art than science. You may have to tweak things like alignment until it looks correct.
Give your
.calories-info spanselector amarginset to-7px -2px. This will shift your230text into place.Below your
.calories-infoelement, add adivwith theclassattribute set todivider medium.5Create an
.mediumselector and give it aheightproperty of5px.Create a new
divelement below your.mediumelement. Give it aclassattribute set todaily-value small-text. Within this newdiv, add apelement with the text% Daily Value *, and set theclassattribute tobold right.The text
* Daily Value %should be aligned to the right. Create a.rightselector and use thejustify-contentproperty to do it.采用justify-content:flex-end;
Use your existing
.dividerelement as an example to add a new divider after thepelement.After your last
.dividerelement, create apelement and give it the textTotal Fat 8g 10%. Wrap the textTotal Fatin aspanelement with theclassofbold. Wrap the text10%in anotherspanelement with theclassofbold. Finally, nest theTotal Fatspanelement and the text8gin an additionalspanelement for alignment.Below your element with the
Total Fattext, create a newpelement with the textSaturated Fat 1g 5%. Wrap the5%in aspanwith theclassattribute set tobold. In this case this is enough to align the percentage to5%.This new
pelement will need to be indented. Give it aclassset toindent.Create a new
.indentselector and give it amargin-leftproperty set to1em.Create a
.daily-value pselector to target all of yourpelements in thedaily-valuesection. Give this new selector aborder-bottomset 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-dividerto theclassfor these two elements.The
:notpseudo-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
divelements without anidofexample.Modify your
.daily-value pselector to exclude the.no-dividerelements.Now you will have to add separate dividers below your
.no-dividerelements.Your first
.no-dividerelement has a.dividerafter it. Create another.dividerafter your second.no-dividerelement.After your last
.divider, create anotherpelement with the textTrans Fat 0g. Italicize the wordTransby wrapping it in anielement. Give the newpelement theclassattribute set toindent no-divider. WrapTrans Fat 0gin aspanelement for alignment.Create another
.dividerafter your lastpelement.After your last
.divider, create a newpelement with the textCholesterol 0mg 0%. Wrap the textCholesterolin aspanelement, and give thatspanelement theclassofbold. Wrap the text0%in anotherspanelement, with theclassofbold. Finally, nest theCholesterolspanelement and the text0mgin an additionalspanelement for alignment.Below your last
pelement, create anotherpelement with the textSodium 160mg 7%. Wrap the textSodiumin aspanelement with aclassattribute set tobold. Wrap the7%text in anotherspanelement with theclassset tobold. Also add an additionalspanelement aroundSodium 160mgfor aligning it correctly.Add another
pelement with the textTotal Carbohydrate 37g 13%. Like before, usespanelements to make the textTotal Carbohydrateand13%bold. Also add an additionalspanelement to wrap theTotal Carbohydrate 37gtext in a span element so to have it aligned to the left, and13%to the right.Below your last
pelement, add anotherpelement with the textDietary Fiber 4g. Give thepelement theclassnecessary to indent it and remove the dividing border. Then create a divider below thatpelement.Create another
pelement after your last.divider, and give it the textTotal Sugars 12g. Assign thatpelement theclassvalues necessary to indent it and remove the bottom border. Then create another.dividerbelow your newpelement.The advantage to creating these dividers is that you can apply specific classes to style them individually. Add
double-indentto theclassfor your last.divider.Create a
.double-indentselector and give it a left margin of2em.Below your
.double-indentelement, add a newpelement with the textIncludes 10g Added Sugars 20%. Your newpelement should also be double indented, and have no bottom border. Use aspanto make the20%bold and right aligned.Then create another divider after that
pelement.After your last divider, create another
pelement with the textProtein 3g. Use the necessary classes to remove the bottom border, and aspanto make theProteinbold.Following this element, create a large divider.
Create another
pelement below your large divider. Give thepelement the textVitamin D 2mcg 10%.The
pelement contains only text, you can wrap the percentage in aspanelement so that it is considered a separate entity from the rest of the text, and it’s moved to the right.Create another
pelement, give it the textCalcium 260mg 20%. Align20%to the right. Below that, create apelement with the textIron 8mg 45%, aligning the45%to the right.Create the final
pelement for your.daily-valuesection. Give it the textPotassium 235mg 6%. Align the6%text to the right, and remove the bottom border of thepelement.Add a medium divider after your
.daily-valueelement. Below that new divider, create apelement with theclassattribute set tonote.Give the
pelement 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
.noteselector, 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
.noteselector a left and right padding of8px, removing the top and bottom padding. Also set thetext-indentproperty to-8px.With these last changes, your nutrition label is complete!
1 |
|
1 | * { |