One of the most neglected aspects of the web is accessibility, unless you are part of a group dedicated to this subject. As web designers and web developers, we rarely think about handicapped users accessing the web and using our websites or apps with screen readers and other assistive technologies. We actually think first about supporting legacy browsers rather than increasing the accessibility of our products.
In this chapter, we're going to touch on what WAI-ARIA landmark roles are and how they can be easily implemented in our markup, enhancing the semantics of our documents to provide those users with assistive technology a better and pleasant experience when they navigate our websites/apps with their keyboards on any modern browser.
WAI-ARIA landmark roles can also be referred to as ARIA roles, so that's the term we're going to use.
An ARIA role looks like this when implemented in an HTML5 element:
<header role="banner">There are really multiple ARIA roles at our disposal, but in this book we're going to focus on the ones that are easier to implement and that will enhance the accessibility of our websites/apps efficiently.
Here are a few important points to remember:
<header> of the page.role="banner" appears constantly across the site rather than in a single specific page.role="banner" is allowed per page/document.Consider the following example:
<header class="masthead" role="banner"> <div class="logo">Mastering RWD with HTML5 & CSS3</div> <div class="search" role="search"> <form> <label>Search: <input type="text" class="field"> <button>Search Now!</button> </label> </form> </div> </header>
Here are a few important points to remember:
<nav> element, but it can also be applied to other containers such as <div> or <ul>.role="navigation" per page.Consider the following example where the role is applied to the main <nav> element:
<nav class="main-nav" role="navigation"> <ul class="nav-container"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li><a href="#">Link 4</a></li> </ul> </nav>
Consider the following example where the role is applied to the <ul> element of the footer navigation:
<footer class="main-footer" role="contentinfo">
<p>Copyright ©</p>
<ul class="nav-container" role="navigation">
<li><a href="#">Footer Link 1</a></li>
<li><a href="#">Footer Link 2</a></li>
<li><a href="#">Footer Link 3</a></li>
<li><a href="#">Footer Link 4</a></li>
<li><a href="#">Footer Link 5</a></li>
</ul>
</footer>There is no particular preference as to which element we add the navigation role to. It's the same if we add it to the <nav> element or the <ul> element.
Here are a few important points to remember:
<main> element of the page.role="main" is allowed per page/document.Consider the following example:
<body>
<main class="main-container" role="main">Content goes here
</main>
</body>Here are a few important points to remember:
<footer> element of the page.role="contentinfo".role="contentinfo" is allowed per page/document.Consider the following example:
<footer class="main-footer" role="contentinfo"> <p>Copyright ©</p> <ul class="nav-container" role="navigation"> <li><a href="#">Footer Link 1</a></li> <li><a href="#">Footer Link 2</a></li> <li><a href="#">Footer Link 3</a></li> <li><a href="#">Footer Link 4</a></li> <li><a href="#">Footer Link 5</a></li> </ul> </footer>
Here are a few important points to remember:
<form> element that belongs to the search feature of the page/app.<div> element, this role can also be applied to that <div> element. If this is the case, then there's no need to add it to the child <form> element.role="search" per page as long as the control is an actual search feature. For example, using the role="search" on a contact form is incorrect and unsemantic.Consider the following example where the role is applied to the site's search <form> element:
<div class="search"> <form role="search"> <label>Search: <input type="text" class="field"> <button>Search Now!</button> </label> </form> </div>
Here are a few important points to remember:
<div> element that contains some type of form, except the main search form of the site/app, for example, contact forms, registration forms, payment forms, and so on.<form> element, because this element already has default role semantics that assist technology support.Consider the following example:
<div class="contact-form" role="form"> <header> <h2>Have Questions About HTML5?</h2> </header> <form> <div class="flex-container"> <label class="label-col">Name: <input type="text" class="field name" id="name" required></label> <label class="label-col">Email: <input type="email" class="field email" id="email" required></label> </div> <label for="comments">Comments:</label> <textarea class="comments" id="comments" cols="50" required></textarea> <button>Send Question!</button> </form> </div>
Here are a few important points to remember:
<aside> element.<aside> element.role="complementary" per page.Consider the following example:
<aside class="side-content" role="complementary"> <h2>What Does "Semantic HTML" Mean?</h2> <p>Semantic markup basically means that we use HTML tags to describe what a specific piece of content is.</p> </aside>
WAI-ARIA roles explained
If you're curious about the list of ARIA roles, you can visit the Web Platform website where the explanations are simple and very easy to understand: https://specs.webplatform.org/html-aria/webspecs/master/#docconformance
There are many ways web designers and developers use meta tags, but those extensive explanations are outside the scope of this book, so we're going to focus on the bits and pieces that are relevant and work as intended for RWD.
The following meta tags are very important for our responsive site/app. These meta tags are not just for HTML5 pages, they will work with any version of HTML.
Let's get started.
The viewport meta tag is the most important meta tag for RWD. It was introduced by Apple in their mobile Safari browser. Now, other mobile browsers support it as well. Oddly enough, this meta tag is not part of any web standards of any kind, yet it is mandatory if we want our responsive sites/apps to display correctly on small screens.
The recommended syntax of this meta tag is as follows:
<meta name="viewport" content="width=device-width, initial-scale=1">
Here are a few important points to remember:
name="viewport" directive describes the type of meta tag.content="width=device-width, initial-scale=1" directive does several things:width property defines the size of the viewport meta tag. We can also use specific pixel widths, for example, width=960.device-width value is the width of the screen at 100 percent zoom in CSS pixels.initial-scale value defines the zoom level the page should be shown at when it's first loaded. 1 equals 100 percent zoom and 1.5 equals 150 percent zoom.This book strongly discourages the use of the following viewport properties: maximum-scale=1 and user-scalable=no. By using these viewport properties, we deny the users the ability to zoom in our website/app. We never know when zooming may be important for anyone, so it's best to steer away from including those viewport properties.
To help websites that are not responsive (yet) display a bit better on small screens, add the specific pixel width the site was built at. For example, if a website is as wide as 960px, add this viewport meta tag to it:
<meta name="viewport" content="width=960">
If you're interested in reading in more detail about the viewport meta tag, MDN explains it very well: https://developer.mozilla.org/en/docs/Mozilla/Mobile/Viewport_meta_tag.
The X-UA-Compatible meta tag targets only Internet Explorer and its Compatibility View feature. As we all know, Microsoft introduced Compatibility View in IE8.
The recommended syntax of this meta tag looks like this:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Here are a few important points to remember:
http-equiv="X-UA-Compatible" directive tells IE that a certain rendering engine needs to be used to render a page.content="IE=edge" directive tells IE that it should use its latest rendering HTML and JavaScript engines.The charset meta tag tells the browser which character set to use to interpret the content. Some say it isn't that important to include because the server itself sends the character set to the browsers via HTTP headers anyway. But it's always a good measure to include it in our pages as well.
If charset is not declared in the HTML and the server doesn't send the character set to the browser, it's likely that some special characters may display incorrectly.
The recommended syntax of this meta tag in HTML5 is like this:
<meta charset="utf-8">
Here are a few important points to remember:
:<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
ISO-8859-1, but UTF-8 is more widely used because there is a better chance of the browser interpreting the content correctly.