How to add "SKIP TO MAIN CONTENT" to websites
Tags: a11y, webdev, programming, html
Published: 2021-08-21 01:23:37

As part of this blog post let's see what is web accessibility, and how to add "skip to main content" link for web pages and why?.


### Accessibility

As per w3.org


> Web accessibility means that websites, tools, and technologies are designed and developed so that people with disabilities can use them. More specifically, people can: perceive, understand, navigate, and interact with the Web
contribute to the Web

For a normal user when he visits a page, he/she directly jumps to the section of the page where they want to read. But if we consider visually impaired users or someone who uses screen reader, the assistive device has to read through the entire page before they can reach a point which interests them. 

Let's assume when a user lands on the page , the screen reader will read the entire navigation/header and then goes to the actual content of the page. The user reads through the first page of your website once, now he/she goes to the second page and the screen reader will again start reading the entire navigation for them before they can go to the main content. This is where "SKIP TO MAIN CONTENT" link comes in handy for them.

### How to add "SKIP TO MAIN CONTENT" on the page

#### HTML
- Inside the body tag add an anchor tag as the first element. 
- Add an id to the content you want the screenreader to jump/skip to. eg: 'main-content'
- Add the same id as the href value to the anchor tag eg: 'main-content'

```
<body>
    <a class="skip-link" href="#main-content">SKIP TO MAIN CONTENT</a>
    <header> .....</header>
    <main id="main-content">
         Main Body Content .....
    </main>
    <footer>......</footer>
</body>
``` 

#### CSS
- Add CSS to the link to hide it from view.
 **[P.S Do not use display:none or visibility hidden because screen readers will not read the element. Our main focus is for the screen reader to read the link]**
- When the screenreader focuses on that element, make the element visible and also the screen reader will read the content inside the anchor tag . i.e "SKIP TO MAIN CONTENT" in our example.


```
.skip-link {
    position: absolute;
    left: -999px;
    width: 1px;
    height: 1px;
    top: auto;
}

.skip-link:active, .skip-link:focus {
    color: #000;
    display: inline-block;
    height: auto;
    width: auto;
    position: static;
    margin: auto
}
``` 


#### References
- [Accessibility](https://www.w3.org/WAI/fundamentals/accessibility-intro/) 


### Checkout my other blog posts

-  [Getting Started with JavaScript - Ultimate Free Resources](https://dev.to/kritikapattalam/getting-started-with-javascript-ultimate-free-resources-59p6) 
-  [10 Things you can do in Chrome Developer Tools ](https://dev.to/kritikapattalam/10-things-you-can-do-in-chrome-developer-tools-333g) 
-  [2 Simple ways you can truncate text using CSS](https://dev.to/kritikapattalam/2-simple-ways-you-can-truncate-text-using-css-1n18) 


Please share your feedbacks and suggestions in the comments below.

Lets connect on  [Twitter](https://twitter.com/KritikaPattalam)  | [LinkedIn](https://www.linkedin.com/in/kritika-p-296739155/) for more web development related tips & tricks.