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
langattribute to yourhtmlelement. This will assist screen readers in identifying the language of the page.You may be familiar with the
metaelement already; it is used to specify information about the page, such as the title, description, keywords, and author.Give your page a
metaelement with an appropriatecharsetvalue.The
charsetattribute specifies the character encoding of the page, and, nowadays,UTF-8is the only encoding supported by most browsers.Continuing with the
metaelements, aviewportdefinition tells the browser how to render the page. Including one betters visual accessibility on mobile, and improves SEO (search engine optimization).Add a
viewportdefinition with acontentattribute detailing thewidthandinitial-scaleof the page.<meta name="viewport" content="width=device-width, initial-scale=1">Another important
metaelement for accessibility and SEO is thedescriptiondefinition. The value of thecontentattribute is used by search engines to provide a description of your page.Add a
metaelement with thenameattribute set todescription, and give it a usefulcontentattribute.Lastly in the
head, thetitleelement is useful for screen readers to understand the content of a page. Furthermore, it is an important part of SEO.Give your page a
titlethat 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
headerand amainelement to your page.The
headerelement will be used to introduce the page, as well as provide a navigation menu.The
mainelement will contain the core content of your page.Within the
header, provide context about the page by nesting oneimg,h1, andnavelement.The
imgshould point tohttps://cdn.freecodecamp.org/platform/universal/fcc_primary.svg, and have anidoflogo.The
h1should contain the textHTML/CSS Quiz.A useful property of an SVG (scalable vector graphics) is that it contains a
pathattribute which allows the image to be scaled without affecting the resolution of the resultant image.Currently, the
imgis assuming its default size, which is too large. Correctly, scale the image using it’sidas a selector, and setting thewidthtomax(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-colorto#0a0a23so you can see the logo. Then, use theaspect-ratioproperty to set the desired aspect ratio to35 / 4. Finally, add apaddingof0.4remall around.Make the
headertake up the full width of its parent container, set itsheightto50px, and set thebackground-colorto#1b1b32. Then, set thedisplayto use Flexbox.Change the
h1font 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:
INFOHTMLCSS
The list items text should be wrapped in anchor tags.
<li>中内容需要用a标签进行嵌套Target unordered list elements within
navelements, 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
sectionelements.Within the
mainelement, create a form with three nestedsectionelements. Then, make the form submit tohttps://freecodecamp.org/practice-project/accessibility-quiz, using the correct method.To increase the page accessibility, the
roleattribute can be used to indicate the purpose behind an element on the page to assistive technologies. Theroleattribute is a part of the Web Accessibility Initiative (WAI), and accepts preset values.Give each of the
sectionelements theregionrole.Every
regionrole 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-labelledbyattribute.Add the following
aria-labelledbyattributes to thesectionelements:student-infohtml-questionscss-questions
Then, within each
sectionelement, nest oneh2element with anidmatching the correspondingaria-labelledbyattribute. Give eachh2suitable 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
h1andh2elements toVerdana, and use another web-safe font in the sans-serif family as a fallback.Also, add a
border-bottomof4px solid #dfdfe2toh2elements to make the sections distinct.To be able to navigate within the page, give each anchor element an
hrefcorresponding to theidof theh2elements.Filling out the content of the quiz, below
#student-info, add threedivelements with aclassofinfo.Then, within each
divnest onelabelelement, and oneinputelement.It is important to link each
inputto the correspondinglabelelement. This provides assistive technology users with a visual reference to the input.This is done by giving the
labelaforattribute, which contains theidof theinput.This section will take a student’s name, email address, and date of birth. Give the
labelelements appropriateforattributes, as well as text content. Then, link theinputelements to the correspondinglabelelements.Keeping in mind best-practices for form inputs, give each
inputan appropriatetypeandnameattribute. Then, give the firstinputaplaceholderattribute.Even though you added a
placeholderto the firstinputelement 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
inputelement, relying on thelabelbeing 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
spanelement with a class ofsr-onlyto the current text content of the thirdlabelelement.Within the
spanelement, add the text(Date of Birth).The
.sr-onlytext 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-onlyclass.Within the second
sectionelement, add twodivelements with a class ofquestion-block.Then, within each
div.question-blockelement, add onepelement with text of incrementing numbers, starting at1, and onefieldsetelement with a class ofquestion.Each
fieldsetwill contain a true/false question.Within each
fieldset, nest onelegendelement, and oneulelement with two options.Give each
fieldsetan adequatenameattribute. Then, give both unordered lists aclassofanswers-list.Finally, use the
legendto caption the content of thefieldsetby 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
labelelement, and within eachlabelelement, nest oneinputelement with the appropriatetype.Add an
idto all of your radioinputs so you can link your labels to them. Give the first one anidofq1-a1. Give the rest of themids ofq1-a2,q2-a1, andq2-a2, respectively.Although not required for
labelelements with a nestedinput, it is still best-practice to explicitly link alabelwith its correspondinginputelement.Now, add a
forattribute to each of your fourlabels that links thelabelto its corresponding radioinput.Give the
labelelements text such that theinputcomes before the text. Then, give theinputelements avaluematching the text.The text should either be
TrueorFalse.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
beforepseudo-element of thepelement, and give it acontentproperty of"Question #".The final section of this quiz will contain a dropdown, and a text box.
Begin by nesting a
divwith aclassofformrow, and nest fourdivelements inside of it, alternating theirclassattributes withquestion-blockandanswer.Within the
div.question-blockelements, nest onelabelelement, and give thelabelelements text contentWithin the first
div.answerelement, nest one requiredselectelement with threeoptionelements.Give the first
optionelement avalueof"", and the textSelect an option. Give the secondoptionelement avalueofyes, and the textYes. Give the thirdoptionelement avalueofno, and the textNo.Link the first
labelelement to theselectelement, and give theselectelement anameattribute.Nest one
textareaelement within the seconddiv.answerelement, and set the number of rows and columns it has.Then, give the
textareaplaceholder text describing an example answer.As with the other
inputandlabelelements, link thetextareato its correspondinglabelelement, and give it anameattribute.Do not forget to give your
forma submit button with the textSend.Two final semantic HTML elements for this project are the
footerandaddresselements. Thefooterelement is a container for a collection of content that is related to the page, and theaddresselement is a container for contact information for the author of the page.After the
mainelement, add onefooterelement, and nest oneaddresselement within it.Within the
addresselement, add the following:1
2
3
4freeCodeCamp<br />
San Francisco<br />
California<br />
USAThe
addresselement 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
headerto the top of the viewport.When the screen width is small, the
h1does not wrap its text content how it should. Align the text for theh1element in the center.Then, give the
mainpadding such that theStudent Infosection 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
ulcontent. Then, set the following CSS properties to correctly align the text:Set the width of the
sectionelements to80%of their parent container. Then, use margins to center thesectionelements, adding10pxto the bottom margin.Also, ensure the
sectionelements cannot be larger than600pxin width.Replace the top margin of the
h2elements with60pxof top padding.Add padding to the top and left of the
.infoelements, and set the other values to0.Give the
.formrowelements top margin, and left and right padding. The other padding values should be0.Then, increase the font size for all
inputelements.To make the first section look more inline, target only the
inputelements within.infoelements, and set theirwidthto50%, and left-align their text.Target all
labelelements within.infoelements, and set theirwidthto10%, 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
inputandlabelelements within an.infoelement and set thedisplayproperty toinline-block.Also, align the
labelelement’s text to the right.To neaten the
.question-blockelements, 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
fieldsetelements, during development. However, it might not be the style you want.Remove the border and bottom padding on the
.questionelements.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
footerbackground color to#2a2a40, and use Flexbox to horizontally center the text.Now, we cannot read the text. Target the
footerand the anchor element within to set the font color to a color of adequate contrast ratio.5Horizontally center all the text within the
addresselement, 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-behaviortosmooth.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
@mediaat-rule has a media feature calledprefers-reduced-motionto set CSS based on the user’s preferences. It can take one of the following values:reduceno-preference
1
2
3
4
5@media (feature: value) {
selector {
styles
}
}
Wrap the style rule that sets
scroll-behavior: smoothwithin an@mediaat-rule with the media featureprefers-reduced-motionhavingno-preferenceset as the value.Finally, the navigation accessibility can be improved by providing keyboard shortcuts.
The
accesskeyattribute 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) { |