Reputation: 11
How to add Keyboard navigation to an existing website using JavaScript, CSS, or HTML? I don't want to add any paid apps. if you know free javascript library or something let me know.
similar techs I used: tabindex="0", a:focus{border : solid 2px blue}
Upvotes: 1
Views: 1696
Reputation: 6524
Some concrete advice:
Use standard HTML5 elements where possible. Read up on HTML5 semantics, in particular headings and navigation. Before reaching for custom classes and CSS display/visibility features, make use of HTML attributes like hidden
and disabled
to control the visibility and availability of content and UI.
By all means use <div>
and <span>
elements for presentation and layout, but pay attention to document structure. Let semantic markup reflect/express the meaning you intend. ARIA landmark roles can help, although most of them are implicit in HTML already.
Note that the "Outline algorithm" mentioned in the HTML5 spec is not implemented on any browser, so don't get too hung up on perfectly sequential heading levels, just be consistent and logical. I find that starting each landmark with a new <h1>
tends to be easiest to manage, but others prefer to have only one <h1>
per page (thereafter starting distinctly meaningful areas or landmarks with <h2>
). Both approaches are acceptable, and the exact choice should depend on complexity and did I mention consistency?
Add ARIA attributes sparingly, and only when HTML does not already express the intended semantic. (e.g. adding aria-pressed
to a <button>
turns it into a toggle button). No need to add aria-disabled="true"
if the element already has a HTML5 disabled
attribute.
It's worth knowing that ARIA attributes work well in CSS attribute selectors. If you use them, you can style your content based on their values, which keeps things synchronised.
Ensure that all operable/interactive elements are clearly labeled using standard HTML mechanisms like <label>
if possible. If a visible label is not desired, or is ambiguous without visual cues, you may use aria-label
to provide an 'offscreen' one.
More complex interactions may need some special keyboard handling in javascript.
Pay attention to the many articles offering guidance about color and contrast (for users with low-vision or color blindness). In particular, make sure the keyboard focus indicator is always clear and obvious. (You mentioned the CSS outline
property in your question, so you are on the right track).
Provide text alternatives for all non-decorative images. If you're using an <img>
tag, the alt
attribute works very well. Decorative images need this too, but with a null value. (e.g. alt=""
)
Bear in mind different screen sizes and orientations. Consider using responsive CSS features such as media queries, grid and flex layouts. Avoid re-ordering content visually when it contradicts the meaning of the sequence in the underlying HTML code.
Consider that users may want to increase text size or line spacing using custom settings. This means leaning on relative units such as %
, em
and rem
, rather than px
, which should be reserved for hairline borders, or media query breakpoints.
VALIDATE YOUR CODE using an HTML validator. Free dev tools such as axe-core will also give very useful feedback and helpful guidance.
Use stack overflow for concrete questions. It's pretty reliable. Many good accessibility questions have already been asked and answered. Search first!
If you follow this advice, and if your product is not too outlandish in design, you will most likely be able to approach a high level of conformance with WCAG A/AA without too much pain.
Finally: Transparency is more important than full conformance. If you have WCAG violations you can't fix in the current iteration, be open and honest about it, and you will be in the clear, legally speaking. Perfect conformance is rare, and especially so for a first time accessibility implementation. Welcome to this challenging and rewarding field, and good luck!
Upvotes: 1