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
,html
indicating this document is in English,head
, andbody
elements.Give your
head
element the appropriatemeta
elements for thecharset
andviewport
, atitle
element with an appropriate title, and alink
element for your stylesheet.Within your
body
element, nest asection
element within amain
element.Within your
section
element, add anh1
element with a nestedspan
element.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
span
theclass
attribute set toflex
, and add twospan
elements 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
h1
element, create adiv
element. Give it anid
attribute set toyears
. You want this particular element to be hidden from screen readers, so give it thearia-hidden
attribute set totrue
.Within your
div
element, add threespan
elements. Give each of them aclass
attribute set toyear
, and add the following text (in order):2019
,2020
, and2021
.Below your existing
div
element, add a newdiv
element with aclass
set totable-wrap
. This will be the container for your tables.Within that, add three
table
elements. 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
caption
element to describe what the table is about. Thecaption
element should always be the first child of atable
, but can be positioned with thecaption-side
CSS property.Add a
caption
element to your firsttable
, and give it the textAssets
.The
thead
andtbody
elements are used to indicate which portion of your table is the header, and which portion contains the primary data or content.Add a
thead
andtbody
to your firsttable
, below thecaption
element.The
tr
element is used to indicate a table row. Add atr
element within yourthead
element. In your newtr
element, add atd
element, followed by threeth
elements.The
td
element indicates a data cell, while theth
element indicates a header cell.Within each of your new
th
elements, nest aspan
element with theclass
set tosr-only year
. Give them the following text (in order):2019
,2020
, and2021
.Give your third
th
element theclass
attribute set tocurrent
.Leave the
td
element empty. This element exists only to ensure your table has a four-column layout and associate the headers with the correct columns.Within your
tbody
element, add fourtr
elements. Give the first three aclass
attribute set todata
, and the fourth aclass
attribute set tototal
.In your first
tr
, add ath
element with the textCash This is the cash we currently have on hand.
. Wrap all of that text exceptCash
in aspan
element with theclass
set todescription
.Following that, add three
td
elements with the following text (in order):$25
,$30
,$28
. Give the thirdtd
element aclass
attribute set tocurrent
.In your second
tr
element, add ath
element with the textChecking Our primary transactional account.
. Wrap that text, except forChecking
, in aspan
element with theclass
attribute set todescription
.Following that, add three
td
elements with the following text (in order):$54
,$56
,$53
. Give the thirdtd
element aclass
attribute set tocurrent
.In your third
tr
element, add ath
element with the textSavings Funds set aside for emergencies.
. Wrap that text, except forSavings
, in aspan
element with theclass
attribute set todescription
.Following that, add three
td
elements with the following text (in order):$500
,$650
,$728
. Give the thirdtd
element aclass
attribute set tocurrent
.In your fourth
tr
element, add ath
element with the textTotal Assets
. Wrap the textAssets
in aspan
element with theclass
attribute set tosr-only
.Following that, add three
td
elements with the following text (in order):$579
,$736
,$809
. Give the thirdtd
element aclass
attribute set tocurrent
.Time to move on to your second table. Start by giving it a
caption
element set toLiabilities
. Then add yourthead
andtbody
.Within your
thead
, add atr
. Inside that, add atd
and threeth
elements.Give each
th
element aspan
element with the class set tosr-only
and the following text, in order:2019
,2020
, and2021
.Within the
tbody
element, add fourtr
elements. Give the first three theclass
attribute set todata
, and the fourth theclass
attribute set tototal
.Within the first
tr
, add ath
element with the textLoans The outstanding balance on our startup loan.
. Wrap that text, except forLoans
, within aspan
element with theclass
set todescription
.Add three
td
elements below that, and give them the following text, in order:$500
,$250
, and$0
. Give the thirdtd
element aclass
set tocurrent
.Within the second
tr
, add ath
element with the textExpenses Annual anticipated expenses, such as payroll.
. Wrap that text, except forExpenses
, within aspan
element with theclass
set todescription
.Add three
td
elements below that, and give them the following text, in order:$200
,$300
, and$400
. Give the thirdtd
element aclass
set tocurrent
.Within the third
tr
, add ath
element with the textCredit The outstanding balance on our credit card.
. Wrap that text, except forCredit
, within aspan
element with theclass
set todescription
.Add three
td
elements below that, and give them the following text, in order:$50
,$50
, and$75
. Give the thirdtd
element aclass
set tocurrent
.In your fourth
tr
element, add ath
element with the textTotal Liabilities
. Wrap the textLiabilities
in aspan
element with theclass
attribute set tosr-only
.Following that, add three
td
elements with the following text (in order):$750
,$600
,$475
. Give the thirdtd
element aclass
attribute set tocurrent
.For your third table, add a
caption
with the textNet Worth
, and set up a table header and table body.Within your
thead
, create atr
element. In that, add atd
and threeth
elements. Within each of theth
elements, add aspan
element with theclass
set tosr-only
and the following text, in order:2019
,2020
, and2021
.Within the
tbody
, add atr
with theclass
set tototal
. In that, add ath
with the textTotal Net Worth
, and wrapNet Worth
in aspan
with theclass
set tosr-only
.Then add three
td
elements, giving the third aclass
set tocurrent
, and giving each the following text:$-171
,$136
,$334
.Time to style your table. Start by resetting the box model. Create an
html
selector and give it abox-sizing
property set toborder-box
.Create a
body
selector and give it afont-family
property set tosans-serif
and acolor
set to#0a0a23
.Before you get too far into your styling, you should make use of the
sr-only
class. 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 anyspan
element whoseclass
includessr-only
. Create that selector, and give it aborder
property set to0
.The CSS
clip
property is used to define the visible portions of an element. Set thespan[class~="sr-only"]
selector to have aclip
property ofrect(1px, 1px, 1px, 1px)
.The
clip-path
property determines the shape theclip
property should take. Set theclip-path
property 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 awidth
andheight
property set to1px
.5To prevent the text content from overflowing, give your
span[class~="sr-only"]
selector anoverflow
property set tohidden
and awhite-space
property set tonowrap
.Finally, you need to take these hidden elements out of the document flow. Give the
span[class~="sr-only"]
selector aposition
property set toabsolute
, apadding
property set to0
, and amargin
property 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
h1
selector. Give it amax-width
property set to37.25rem
, amargin
property set to0 auto
, and apadding
property set to1.5rem 1.25rem
.Target your flex container with an
h1 .flex
selector. Give it adisplay
property set toflex
to enable the flexbox layout. Then set theflex-direction
property tocolumn-reverse
- this will display the nested elements from bottom to top. Finally, set thegap
property to1rem
to create some space between the elements.The
:first-of-type
pseudo-selector is used to target the first element that matches the selector. Create anh1 .flex span:first-of-type
selector to target the firstspan
element in your.flex
container. Remember that yourspan
elements are reversed, visually, so this will appear to be the second element on the page.Give your new selector a
font-size
property of0.7em
to make it look like a sub-heading.The
:last-of-type
pseudo-selector does the exact opposite - it targets the last element that matches the selector. Create anh1 .flex span:last-of-type
selector to target the lastspan
in your flex container, and give it afont-size
property set to1.2em
to 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
section
selector, and give it amax-width
property set to40rem
for responsive design. Set themargin
property to0 auto
to center it, and set theborder
property to2px solid #d0d0d5
.The last part of your table heading is your years. Create a
#years
selector, 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#fff
and 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
#years
amargin
of0 -2px
, and apadding
set to0.5rem calc(1.25rem + 2px) 0.5rem 0
.Adding position
sticky
moved the element into its own stack. To ensure your#years
element does not get hidden by different stacks, add az-index
property set to999
in the#years
rule.Style the text within your
#years
element by creating a#years span[class]
selector. Thespan[class]
syntax will target anyspan
element that has aclass
attribute set, regardless of the attribute’s value.Give your new selector a
bold
font, a width of4.5rem
, and text aligned to the right.You wrapped your tables in a container with the
table-wrap
class. Create a selector for that class, and give it apadding
set to0 0.75rem 1.5rem 0.75rem
.Before you start diving in to the table itself, your
span
elements are currently bolded. Create aspan:not(.sr-only)
selector and give it afont-weight
property set tonormal
.The
:not()
pseudo-selector is used to target all elements that do not match the selector - in this case, any of yourspan
elements that do not have thesr-only
class. 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
!important
keyword to ensure these properties are always applied, regardless of order or specificity.Give each property in your
span[class~="sr-only"]
selector an!important
keyword. Do not change any of the values.Now that you have added the
!important
keyword, you can remove the:not(.sr-only)
from yourspan
selector.Create a
table
selector to target your tables. Set theborder-collapse
property tocollapse
, which will allow cell borders to collapse into a single border, instead of a border around each cell. Also set theborder
property to0
to hide the borders themselves.Create a
table
selector to target your tables. Set theborder-collapse
property tocollapse
, which will allow cell borders to collapse into a single border, instead of a border around each cell. Also set theborder
property to0
to hide the borders themselves.Next you need to style your
caption
elements to look more like headers. Create atable caption
selector. 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.25rem
from the top and0.5rem
from the left.Create a selector to target your
td
elements 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 settingwidth
specifically would allow the elements to shrink to the container.5Now target the
th
elements 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 yourtr
elements with thetotal
class. Give it a bottom border of4px double #0a0a23
and make the font bold.Using the same selector syntax, target the
th
elements within your table rows where theclass
istotal
. 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.total
is that the first will selecttr
elements where the only class istotal
. The second will selecttr
elements where the class includestotal
.In your case,
tr.total
will work. You can use this selector to target alltd
elements within your.total
rows. 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 thirdtd
element within yourtotal
table rows. Give it a right padding of0.5rem
.Give your
tr.total
elements a hover effect that changes the background to#99c9ff
.Select your
td
elements with theclass
value ofcurrent
, and make the font italic.Select the
tr
elements with theclass
set todata
. Give them a background image oflinear-gradient(to bottom, #dfdfe2 1.845rem, white 1.845rem)
.Select the
th
elements within yourtr.data
elements. Align the text to the left, and give them a top padding of0.3rem
and a left padding of0.5rem
.Create a
tr.data th .description
selector to target the elements with theclass
set todescription
that are within yourth
elements in your.data
table rows. Give them a block display, make the text italic with a normal weight, and position them with apadding
set to1rem 0 0.75rem
and a right margin of-13.5rem
.Your
span
elements now all have more specific styling, which means you can remove yourspan
rule.Your dollar amounts are currently misaligned. Create a selector to target the
td
elements within yourtr.data
elements. 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
td
elements within yourtr.data
element, 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"] { |