Responsive Web Dedign(八)
freeCodeCamp —- Responsive Web Dedign
Learn Accessibility by Building a Quiz
Welcome to the first part of the Accessibility Quiz. As you are becoming a seasoned HTML and CSS developer, we have started you off with the basic boilerplate.
Start this accessibility journey by providing a
lang
attribute to yourhtml
element. This will assist screen readers in identifying the language of the page.You may be familiar with the
meta
element already; it is used to specify information about the page, such as the title, description, keywords, and author.Give your page a
meta
element with an appropriatecharset
value.The
charset
attribute specifies the character encoding of the page, and, nowadays,UTF-8
is the only encoding supported by most browsers.Continuing with the
meta
elements, aviewport
definition tells the browser how to render the page. Including one betters visual accessibility on mobile, and improves SEO (search engine optimization).Add a
viewport
definition with acontent
attribute detailing thewidth
andinitial-scale
of the page.<meta name="viewport" content="width=device-width, initial-scale=1">
Another important
meta
element for accessibility and SEO is thedescription
definition. The value of thecontent
attribute is used by search engines to provide a description of your page.Add a
meta
element with thename
attribute set todescription
, and give it a usefulcontent
attribute.Lastly in the
head
, thetitle
element is useful for screen readers to understand the content of a page. Furthermore, it is an important part of SEO.Give your page a
title
that is descriptive and concise.Navigation is a core part of accessibility, and screen readers rely on you to provide the structure of your page. This is accomplished with semantic HTML elements.
Add a
header
and amain
element to your page.The
header
element will be used to introduce the page, as well as provide a navigation menu.The
main
element will contain the core content of your page.Within the
header
, provide context about the page by nesting oneimg
,h1
, andnav
element.The
img
should point tohttps://cdn.freecodecamp.org/platform/universal/fcc_primary.svg
, and have anid
oflogo
.The
h1
should contain the textHTML/CSS Quiz
.A useful property of an SVG (scalable vector graphics) is that it contains a
path
attribute which allows the image to be scaled without affecting the resolution of the resultant image.Currently, the
img
is assuming its default size, which is too large. Correctly, scale the image using it’sid
as a selector, and setting thewidth
tomax(100px, 18vw)
.As described in the freeCodeCamp Style Guide, the logo should retain an aspect ratio of
35 / 4
, and have padding around the text.First, change the
background-color
to#0a0a23
so you can see the logo. Then, use theaspect-ratio
property to set the desired aspect ratio to35 / 4
. Finally, add apadding
of0.4rem
all around.Make the
header
take up the full width of its parent container, set itsheight
to50px
, and set thebackground-color
to#1b1b32
. Then, set thedisplay
to use Flexbox.Change the
h1
font color to#f1be32
, and set the font size tomin(5vw, 1.2em)
.To enable navigation on the page, add an unordered list with the following three list items:
INFO
HTML
CSS
The list items text should be wrapped in anchor tags.
<li>
中内容需要用a标签进行嵌套Target unordered list elements within
nav
elements, and use Flexbox to evenly space the children.As this is a quiz, you will need a form for users to submit answers. You can semantically separate the content within the form using
section
elements.Within the
main
element, create a form with three nestedsection
elements. Then, make the form submit tohttps://freecodecamp.org/practice-project/accessibility-quiz
, using the correct method.To increase the page accessibility, the
role
attribute can be used to indicate the purpose behind an element on the page to assistive technologies. Therole
attribute is a part of the Web Accessibility Initiative (WAI), and accepts preset values.Give each of the
section
elements theregion
role.Every
region
role requires a label, which helps screen reader users understand the purpose of the region. One method for adding a label is to add a heading element inside the region and then reference it with thearia-labelledby
attribute.Add the following
aria-labelledby
attributes to thesection
elements:student-info
html-questions
css-questions
Then, within each
section
element, nest oneh2
element with anid
matching the correspondingaria-labelledby
attribute. Give eachh2
suitable text content.Typeface plays an important role in the accessibility of a page. Some fonts are easier to read than others, and this is especially true on low-resolution screens.
Change the font for both the
h1
andh2
elements toVerdana
, and use another web-safe font in the sans-serif family as a fallback.Also, add a
border-bottom
of4px solid #dfdfe2
toh2
elements to make the sections distinct.To be able to navigate within the page, give each anchor element an
href
corresponding to theid
of theh2
elements.Filling out the content of the quiz, below
#student-info
, add threediv
elements with aclass
ofinfo
.Then, within each
div
nest onelabel
element, and oneinput
element.It is important to link each
input
to the correspondinglabel
element. This provides assistive technology users with a visual reference to the input.This is done by giving the
label
afor
attribute, which contains theid
of theinput
.This section will take a student’s name, email address, and date of birth. Give the
label
elements appropriatefor
attributes, as well as text content. Then, link theinput
elements to the correspondinglabel
elements.Keeping in mind best-practices for form inputs, give each
input
an appropriatetype
andname
attribute. Then, give the firstinput
aplaceholder
attribute.Even though you added a
placeholder
to the firstinput
element in the previous lesson, this is actually not a best-practice for accessibility; too often, users confuse the placeholder text with an actual input value - they think there is already a value in the input.Remove the placeholder text from the first
input
element, relying on thelabel
being the best-practice.Arguably,
D.O.B.
is not descriptive enough. This is especially true for visually impaired users. One way to get around such an issue, without having to add visible text to the label, is to add text only a screen reader can read.Append a
span
element with a class ofsr-only
to the current text content of the thirdlabel
element.Within the
span
element, add the text(Date of Birth)
.The
.sr-only
text is still visible. There is a common pattern to visually hide text for only screen readers to read.This pattern is to set the following CSS properties:
1
2
3
4
5
6
7
8
9position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;Use the above to define the
sr-only
class.Within the second
section
element, add twodiv
elements with a class ofquestion-block
.Then, within each
div.question-block
element, add onep
element with text of incrementing numbers, starting at1
, and onefieldset
element with a class ofquestion
.Each
fieldset
will contain a true/false question.Within each
fieldset
, nest onelegend
element, and oneul
element with two options.Give each
fieldset
an adequatename
attribute. Then, give both unordered lists aclass
ofanswers-list
.Finally, use the
legend
to caption the content of thefieldset
by placing a true/false question as the text content.To provide the functionality of the true/false questions, we need a set of inputs which do not allow both to be selected at the same time.
Within each list element, nest one
label
element, and within eachlabel
element, nest oneinput
element with the appropriatetype
.Add an
id
to all of your radioinput
s so you can link your labels to them. Give the first one anid
ofq1-a1
. Give the rest of themid
s ofq1-a2
,q2-a1
, andq2-a2
, respectively.Although not required for
label
elements with a nestedinput
, it is still best-practice to explicitly link alabel
with its correspondinginput
element.Now, add a
for
attribute to each of your fourlabel
s that links thelabel
to its corresponding radioinput
.Give the
label
elements text such that theinput
comes before the text. Then, give theinput
elements avalue
matching the text.The text should either be
True
orFalse
.If you click on the radio inputs, you might notice both inputs within the same true/false fieldset can be selected at the same time.
Group the relevant inputs together such that only one input from a pair can be selected at a time.
To prevent unnecessary repetition, target the
before
pseudo-element of thep
element, and give it acontent
property of"Question #"
.The final section of this quiz will contain a dropdown, and a text box.
Begin by nesting a
div
with aclass
offormrow
, and nest fourdiv
elements inside of it, alternating theirclass
attributes withquestion-block
andanswer
.Within the
div.question-block
elements, nest onelabel
element, and give thelabel
elements text contentWithin the first
div.answer
element, nest one requiredselect
element with threeoption
elements.Give the first
option
element avalue
of""
, and the textSelect an option
. Give the secondoption
element avalue
ofyes
, and the textYes
. Give the thirdoption
element avalue
ofno
, and the textNo
.Link the first
label
element to theselect
element, and give theselect
element aname
attribute.Nest one
textarea
element within the seconddiv.answer
element, and set the number of rows and columns it has.Then, give the
textarea
placeholder text describing an example answer.As with the other
input
andlabel
elements, link thetextarea
to its correspondinglabel
element, and give it aname
attribute.Do not forget to give your
form
a submit button with the textSend
.Two final semantic HTML elements for this project are the
footer
andaddress
elements. Thefooter
element is a container for a collection of content that is related to the page, and theaddress
element is a container for contact information for the author of the page.After the
main
element, add onefooter
element, and nest oneaddress
element within it.Within the
address
element, add the following:1
2
3
4freeCodeCamp<br />
San Francisco<br />
California<br />
USAThe
address
element does not have to contain a physical geographical location. It can be used to provide a link to the subject.Wrap a link around the text
freeCodeCamp
, and set its location tohttps://freecodecamp.org
.Back to styling the page. Select the list elements within the navigation bar, and give them the following styles:
On the topic of visual accessibility, contrast between elements is a key factor. For example, the contrast between the text and the background of a heading should be at least 4.5:1.
Change the font color of all the anchor elements within the list elements to something with a contrast ratio of at least 7:1.
To make the navigation buttons look more like typical buttons, remove the underline from the anchor tags.
Then, create a new selector targeting the navigation list elements so that when the cursor hovers over them, the background color and text color are switched, and the cursor becomes a pointer.
Tidy up the
header
, by using Flexbox to put space between the children, and vertically center them.Then, fix the
header
to the top of the viewport.When the screen width is small, the
h1
does not wrap its text content how it should. Align the text for theh1
element in the center.Then, give the
main
padding such that theStudent Info
section header can be fully seen.On small screens, the unordered list in the navigation bar overflows the right side of the screen.
Fix this by using Flexbox to wrap the
ul
content. Then, set the following CSS properties to correctly align the text:Set the width of the
section
elements to80%
of their parent container. Then, use margins to center thesection
elements, adding10px
to the bottom margin.Also, ensure the
section
elements cannot be larger than600px
in width.Replace the top margin of the
h2
elements with60px
of top padding.Add padding to the top and left of the
.info
elements, and set the other values to0
.Give the
.formrow
elements top margin, and left and right padding. The other padding values should be0
.Then, increase the font size for all
input
elements.To make the first section look more inline, target only the
input
elements within.info
elements, and set theirwidth
to50%
, and left-align their text.Target all
label
elements within.info
elements, and set theirwidth
to10%
, and make it so they do not take up less than55px
.To align the input boxes with each other, create a new ruleset that targets all
input
andlabel
elements within an.info
element and set thedisplay
property toinline-block
.Also, align the
label
element’s text to the right.To neaten the
.question-block
elements, set the following CSS properties:1
2
3
4
5text-align: left;
display: block;
width: 100%;
margin-top: 20px;
padding-top: 5px;Make the paragraph elements appear as a higher priority, with the following CSS properties:
1
2
3margin-top: 5px;
padding-left: 15px;
font-size: 20px;It is useful to see the default border around the
fieldset
elements, during development. However, it might not be the style you want.Remove the border and bottom padding on the
.question
elements.Remove the default styling for the list items of
.answers-list
, and remove the unordered list padding.Give the submit button a freeCodeCamp-style design, with the following CSS properties:
1
2
3
4
5
6
7display: block;
margin: 40px auto;
width: 40%;
padding: 15px;
font-size: 23px;
background: #d0d0d5;
border: 3px solid #3b3b4f;Set the
footer
background color to#2a2a40
, and use Flexbox to horizontally center the text.Now, we cannot read the text. Target the
footer
and the anchor element within to set the font color to a color of adequate contrast ratio.5Horizontally center all the text within the
address
element, and add some padding.Clicking on the navigation links should jump the viewport to the relevant section. However, this jumping can be disorienting for some users.
Select all elements, and set the
scroll-behavior
tosmooth
.Certain types of motion-based animations can cause discomfort for some users. In particular, people with vestibular disorders have sensitivity to certain motion triggers.
The
@media
at-rule has a media feature calledprefers-reduced-motion
to set CSS based on the user’s preferences. It can take one of the following values:reduce
no-preference
1
2
3
4
5@media (feature: value) {
selector {
styles
}
}
Wrap the style rule that sets
scroll-behavior: smooth
within an@media
at-rule with the media featureprefers-reduced-motion
havingno-preference
set as the value.Finally, the navigation accessibility can be improved by providing keyboard shortcuts.
The
accesskey
attribute accepts a space-separated list of access keys. For example:1
<button type="submit" accesskey="s">Submit</button>
Give each of the navigation links a single-letter access key.
Note: It is not always advised to use access keys, but they can be useful
Well done. You have completed the Accessibility Quiz practice project.
1 |
|
1 | @media (prefers-reduced-motion: no-preference) { |