Responsive Web Dedign(九)
freeCodeCamp —- Responsive Web Dedign
Learn More About CSS Pseudo Selectors By Building A Balance Sheet
Set up your HTML with the
DOCTYPE,htmlindicating this document is in English,head, andbodyelements.Give your
headelement the appropriatemetaelements for thecharsetandviewport, atitleelement with an appropriate title, and alinkelement for your stylesheet.Within your
bodyelement, nest asectionelement within amainelement.Within your
sectionelement, add anh1element with a nestedspanelement.Screen readers announce HTML elements based on the document flow. We will eventually want the balance sheet to have a heading of “Balance Sheet” and a subheading of “AcmeWidgetCorp”. However, this order does not make sense if announced by a screen reader.
Give your existing
spantheclassattribute set toflex, and add twospanelements within it. Give the first the textAcmeWidgetCorp. Give the second the textBalance Sheet. You will use CSS to reverse the order of the text on the page, but the HTML order will make more sense for a screen reader.Below your
h1element, create adivelement. Give it anidattribute set toyears. You want this particular element to be hidden from screen readers, so give it thearia-hiddenattribute set totrue.Within your
divelement, add threespanelements. Give each of them aclassattribute set toyear, and add the following text (in order):2019,2020, and2021.Below your existing
divelement, add a newdivelement with aclassset totable-wrap. This will be the container for your tables.Within that, add three
tableelements. You will be using CSS to style these into a single table, but using separate tables will help screen readers understand the document flow.HTML tables use the
captionelement to describe what the table is about. Thecaptionelement should always be the first child of atable, but can be positioned with thecaption-sideCSS property.Add a
captionelement to your firsttable, and give it the textAssets.The
theadandtbodyelements are used to indicate which portion of your table is the header, and which portion contains the primary data or content.Add a
theadandtbodyto your firsttable, below thecaptionelement.The
trelement is used to indicate a table row. Add atrelement within yourtheadelement. In your newtrelement, add atdelement, followed by threethelements.The
tdelement indicates a data cell, while thethelement indicates a header cell.Within each of your new
thelements, nest aspanelement with theclassset tosr-only year. Give them the following text (in order):2019,2020, and2021.Give your third
thelement theclassattribute set tocurrent.Leave the
tdelement empty. This element exists only to ensure your table has a four-column layout and associate the headers with the correct columns.Within your
tbodyelement, add fourtrelements. Give the first three aclassattribute set todata, and the fourth aclassattribute set tototal.In your first
tr, add athelement with the textCash This is the cash we currently have on hand.. Wrap all of that text exceptCashin aspanelement with theclassset todescription.Following that, add three
tdelements with the following text (in order):$25,$30,$28. Give the thirdtdelement aclassattribute set tocurrent.In your second
trelement, add athelement with the textChecking Our primary transactional account.. Wrap that text, except forChecking, in aspanelement with theclassattribute set todescription.Following that, add three
tdelements with the following text (in order):$54,$56,$53. Give the thirdtdelement aclassattribute set tocurrent.In your third
trelement, add athelement with the textSavings Funds set aside for emergencies.. Wrap that text, except forSavings, in aspanelement with theclassattribute set todescription.Following that, add three
tdelements with the following text (in order):$500,$650,$728. Give the thirdtdelement aclassattribute set tocurrent.In your fourth
trelement, add athelement with the textTotal Assets. Wrap the textAssetsin aspanelement with theclassattribute set tosr-only.Following that, add three
tdelements with the following text (in order):$579,$736,$809. Give the thirdtdelement aclassattribute set tocurrent.Time to move on to your second table. Start by giving it a
captionelement set toLiabilities. Then add yourtheadandtbody.Within your
thead, add atr. Inside that, add atdand threethelements.Give each
thelement aspanelement with the class set tosr-onlyand the following text, in order:2019,2020, and2021.Within the
tbodyelement, add fourtrelements. Give the first three theclassattribute set todata, and the fourth theclassattribute set tototal.Within the first
tr, add athelement with the textLoans The outstanding balance on our startup loan.. Wrap that text, except forLoans, within aspanelement with theclassset todescription.Add three
tdelements below that, and give them the following text, in order:$500,$250, and$0. Give the thirdtdelement aclassset tocurrent.Within the second
tr, add athelement with the textExpenses Annual anticipated expenses, such as payroll.. Wrap that text, except forExpenses, within aspanelement with theclassset todescription.Add three
tdelements below that, and give them the following text, in order:$200,$300, and$400. Give the thirdtdelement aclassset tocurrent.Within the third
tr, add athelement with the textCredit The outstanding balance on our credit card.. Wrap that text, except forCredit, within aspanelement with theclassset todescription.Add three
tdelements below that, and give them the following text, in order:$50,$50, and$75. Give the thirdtdelement aclassset tocurrent.In your fourth
trelement, add athelement with the textTotal Liabilities. Wrap the textLiabilitiesin aspanelement with theclassattribute set tosr-only.Following that, add three
tdelements with the following text (in order):$750,$600,$475. Give the thirdtdelement aclassattribute set tocurrent.For your third table, add a
captionwith the textNet Worth, and set up a table header and table body.Within your
thead, create atrelement. In that, add atdand threethelements. Within each of thethelements, add aspanelement with theclassset tosr-onlyand the following text, in order:2019,2020, and2021.Within the
tbody, add atrwith theclassset tototal. In that, add athwith the textTotal Net Worth, and wrapNet Worthin aspanwith theclassset tosr-only.Then add three
tdelements, giving the third aclassset tocurrent, and giving each the following text:$-171,$136,$334.Time to style your table. Start by resetting the box model. Create an
htmlselector and give it abox-sizingproperty set toborder-box.Create a
bodyselector and give it afont-familyproperty set tosans-serifand acolorset to#0a0a23.Before you get too far into your styling, you should make use of the
sr-onlyclass. You can use CSS to make elements with this class completely hidden from the visual page, but still be announced by screen readers.The CSS you are about to write is a common set of properties used to ensure elements are completely hidden visually.
The
span[class~="sr-only"]selector will select anyspanelement whoseclassincludessr-only. Create that selector, and give it aborderproperty set to0.The CSS
clipproperty is used to define the visible portions of an element. Set thespan[class~="sr-only"]selector to have aclipproperty ofrect(1px, 1px, 1px, 1px).The
clip-pathproperty determines the shape theclipproperty should take. Set theclip-pathproperty to the value ofinset(50%), forming the clip-path into a rectangle within the element.Now you need to size these elements down. Give your
span[class~="sr-only"]selector awidthandheightproperty set to1px.5To prevent the text content from overflowing, give your
span[class~="sr-only"]selector anoverflowproperty set tohiddenand awhite-spaceproperty set tonowrap.Finally, you need to take these hidden elements out of the document flow. Give the
span[class~="sr-only"]selector apositionproperty set toabsolute, apaddingproperty set to0, and amarginproperty set to-1px. This will ensure that not only are they no longer visible, but they are not even within the page view.Time to style your table heading. Create an
h1selector. Give it amax-widthproperty set to37.25rem, amarginproperty set to0 auto, and apaddingproperty set to1.5rem 1.25rem.Target your flex container with an
h1 .flexselector. Give it adisplayproperty set toflexto enable the flexbox layout. Then set theflex-directionproperty tocolumn-reverse- this will display the nested elements from bottom to top. Finally, set thegapproperty to1remto create some space between the elements.The
:first-of-typepseudo-selector is used to target the first element that matches the selector. Create anh1 .flex span:first-of-typeselector to target the firstspanelement in your.flexcontainer. Remember that yourspanelements are reversed, visually, so this will appear to be the second element on the page.Give your new selector a
font-sizeproperty of0.7emto make it look like a sub-heading.The
:last-of-typepseudo-selector does the exact opposite - it targets the last element that matches the selector. Create anh1 .flex span:last-of-typeselector to target the lastspanin your flex container, and give it afont-sizeproperty set to1.2emto make it look like a header.You wrapped your table in a section element - now you can style that to give your table a border. Create a
sectionselector, and give it amax-widthproperty set to40remfor responsive design. Set themarginproperty to0 autoto center it, and set theborderproperty to2px solid #d0d0d5.The last part of your table heading is your years. Create a
#yearsselector, and enable flexbox. Justify the content to the end of the flex direction, and make the element sticky. Fix it to the top of its container withtop: 0.Now apply some color to your
#years. Make the text#fffand the background#0a0a23.The
calc()function is a CSS function that allows you to calculate a value based on other values. For example, you can use it to calculate the width of the viewport minus the margin of an element:1
2
3
4.example {
margin: 10px;
width: calc(100% - 20px);
}Give
#yearsamarginof0 -2px, and apaddingset to0.5rem calc(1.25rem + 2px) 0.5rem 0.Adding position
stickymoved the element into its own stack. To ensure your#yearselement does not get hidden by different stacks, add az-indexproperty set to999in the#yearsrule.Style the text within your
#yearselement by creating a#years span[class]selector. Thespan[class]syntax will target anyspanelement that has aclassattribute set, regardless of the attribute’s value.Give your new selector a
boldfont, a width of4.5rem, and text aligned to the right.You wrapped your tables in a container with the
table-wrapclass. Create a selector for that class, and give it apaddingset to0 0.75rem 1.5rem 0.75rem.Before you start diving in to the table itself, your
spanelements are currently bolded. Create aspan:not(.sr-only)selector and give it afont-weightproperty set tonormal.The
:not()pseudo-selector is used to target all elements that do not match the selector - in this case, any of yourspanelements that do not have thesr-onlyclass. This ensures that your earlier rules for thespan[class~="sr-only"]selector are not overwritten.Rather than having to constantly double-check you are not overwriting your earlier properties, you can use the
!importantkeyword to ensure these properties are always applied, regardless of order or specificity.Give each property in your
span[class~="sr-only"]selector an!importantkeyword. Do not change any of the values.Now that you have added the
!importantkeyword, you can remove the:not(.sr-only)from yourspanselector.Create a
tableselector to target your tables. Set theborder-collapseproperty tocollapse, which will allow cell borders to collapse into a single border, instead of a border around each cell. Also set theborderproperty to0to hide the borders themselves.Create a
tableselector to target your tables. Set theborder-collapseproperty tocollapse, which will allow cell borders to collapse into a single border, instead of a border around each cell. Also set theborderproperty to0to hide the borders themselves.Next you need to style your
captionelements to look more like headers. Create atable captionselector. Set the text to have a color of#356eaf, a size of1.3em, and a normal weight.Now give the captions an absolute position, and shift them
-2.25remfrom the top and0.5remfrom the left.Create a selector to target your
tdelements within your table body. Give them a width to fill the viewport, with a minimum and maximum of4rem. This approach ensures that the width is fixed, whereas settingwidthspecifically would allow the elements to shrink to the container.5Now target the
thelements within your table body, and give them a width of the entire container, less12rem.The
[attribute="value"]selector targets any element that has an attribute with a specific value. Create atr[class="total"]selector to target specifically yourtrelements with thetotalclass. Give it a bottom border of4px double #0a0a23and make the font bold.Using the same selector syntax, target the
thelements within your table rows where theclassistotal. Align the text to the left, and give them a padding of0.5rem 0 0.25rem 0.5rem.The key difference between
tr[class="total"]andtr.totalis that the first will selecttrelements where the only class istotal. The second will selecttrelements where the class includestotal.In your case,
tr.totalwill work. You can use this selector to target alltdelements within your.totalrows. Align the text to the right, and give them a padding of0 0.25rem.The
:nth-of-type()pseudo-selector is used to target specific elements based on their order among siblings of the same type. Use this pseudo-selector to target the thirdtdelement within yourtotaltable rows. Give it a right padding of0.5rem.Give your
tr.totalelements a hover effect that changes the background to#99c9ff.Select your
tdelements with theclassvalue ofcurrent, and make the font italic.Select the
trelements with theclassset todata. Give them a background image oflinear-gradient(to bottom, #dfdfe2 1.845rem, white 1.845rem).Select the
thelements within yourtr.dataelements. Align the text to the left, and give them a top padding of0.3remand a left padding of0.5rem.Create a
tr.data th .descriptionselector to target the elements with theclassset todescriptionthat are within yourthelements in your.datatable rows. Give them a block display, make the text italic with a normal weight, and position them with apaddingset to1rem 0 0.75remand a right margin of-13.5rem.Your
spanelements now all have more specific styling, which means you can remove yourspanrule.Your dollar amounts are currently misaligned. Create a selector to target the
tdelements within yourtr.dataelements. Vertically align the text to the top, horizontally align the text to the right, and set the padding to0.3rem 0.25rem 0.Create another selector for the
tdelements within yourtr.dataelement, but specifically select the last one. Give this a right padding of0.5rem.With this, your balance sheet is complete!
1 |
|
1 | span[class~="sr-only"] { |