search
HomeWeb Front-endCSS TutorialHow to Create a 'Skip to Content” Link

How to Create a “Skip to Content” Link

Skip links are small internal navigation links that help users move between pages. You may never really see it because they are often hidden, used as accessibility enhancements that allow keyboard users and screen readers to jump from the top of the page to content without having to traverse other elements on the page first.

In fact, if you open DevTools, you can find one on CSS-Tricks.

In my opinion, the best way to implement skipping a link is to hide it and then show it when it gets focus. So, suppose we have a link in HTML:

<a href="https://www.php.cn/link/3292c5c2ca71351a9406a9614e147ad3">
  Skip to content</a>

…We can give it absolute position and move it away from the screen:

 .skip-to-content-link {
  left: 50%;
  position: absolute;
  transform: translateY(-100%);
}

Then when it gets focus, we can re-display it and style it some way in the process:

 .skip-to-content-link {
  background: #e77e23;
  height: 30px;
  left: 50%;
  padding: 8px;
  position: absolute;
  transform: translateY(-100%);
  transition: transform 0.3s;
}

.skip-to-content-link:focus {
  transform: translateY(0%);
}

This will hide our link until it gets focus and then display it when it gets focus.

Now, let me explain in detail, starting with this famous quote from Miles Davis:

Time is not the main factor, it is the only factor .

As I was writing this in Ireland in the rain, I thought of the challenges many users face when using the network I was used to. We put a lot of effort into creating a great user experience without taking into account all users and how they can meet their needs. Granted, I never heard of skipping links before I finished my Marcy Sutton’s course at Frontend Masters. Ever since learning about the power and simplicity of using skip links, I decided to make it my mission to raise awareness – what other platforms are better than CSS-Tricks!

The solution is the answer to the question, so what is the solution to help keyboard users and screen readers find the content of the page quickly? In short, the solution is time . Enable users to navigate to the part of the website they are most interested in, allowing them to save valuable time.

Take Sky News website as an example. It provides a "Skip to Content" button that allows users to skip all navigation items and jump directly to the main content.

You can use the keyboard to navigate to the top of the page to view this button. This is similar to the implementation shown above. The link is always in the document, but is only visible when it gets focus.

This is the type of skip links we will create together in this article.

Our sample website

I built a sample website that we will use to demonstrate skip links.

The site has many navigation links, but to save time, there are only two pages: "Home" and "Blog Page". This is enough to give us an idea of ​​how things work.

Determine the problem

Here is the navigation we are using:

There are eight navigation items in total, and keyboard users and screen readers must first traverse these items to reach the main content below the navigation.

This is the problem. Navigation may not matter to the user. Maybe users get direct links to articles and they just want to access the content.

This is a perfect use case to use skip links.

There are several ways to create a "Skip to Content" link. What I like to do is a similar example to Sky News, i.e. hide the link until it gets focus. This means we can put the link at or near the top of the page, for example<header></header> Inside the element.

 <a href="https://www.php.cn/link/3292c5c2ca71351a9406a9614e147ad3">Skip to content</a>

This link has a .skip-link class so we can style it. href attribute points to https://www.php.cn/link/3292c5c2ca71351a9406a9614e147ad3 , which we will add to the bottom of the page.<main></main> The element's ID. This is where the link will jump to when clicked.

 <a href="https://www.php.cn/link/3292c5c2ca71351a9406a9614e147ad3">Skip to content</a>

<main></main>

If we just put the link in the title without the style, this is what we have.

This doesn't look great, but the functionality exists. Try navigating to the link using the keyboard and press Enter when it gains focus.

Now it's time to make it look beautiful. We have to fix the positioning first and show it only if our skip link gets focus.

 .skip-link {
  background: #319795;
  color: #ffff;
  font-weight: 700;
  left: 50%;
  padding: 4px;
  position: absolute;
  transform: translateY(-100%);
}

.skip-link:focus {
  transform: translateY(0%);
}

The magic here lies in the transform property, which hides and displays it based on whether our skip link gets focus or not. Let's make it look a little better by quickly converting it on the transform property.

 .skip-link {
  /* Same as before*/
  transition: transform 0.3s;
}

It will now convert to the view, which makes it a little better.

You should now (hopefully) have what I have below:

As you can see, skip the link bypasses the navigation and jump directly to the<main></main> element.

Currently, this link serves only one purpose, which is to skip content to our website. But we don't have to stop there.

We can go a step further and create a skip link with more options, such as a way to jump to the website footer. As you might imagine, this is very similar to what we have done.

Let's make the blog page of the sample website easier to use by using multiple skip links. Blogs usually use AJAX, and more posts are loaded when they reach the bottom of the page. This makes it difficult to reach the footer of the website. This is the problem we want to solve.

So let's add a second link which bypasses all autoloading business and jumps the user directly to the https://www.php.cn/link/8248f16fa738b0bfe6013edf69d873bf element on the page.

<div>
  Skip to <a href="https://www.php.cn/link/3292c5c2ca71351a9406a9614e147ad3">content</a> or <a href="https://www.php.cn/link/8248f16fa738b0bfe6013edf69d873bf">footer</a>
</div>

We also need to modify our CSS a little bit and use :focus-within pseudo-selectr.

 .skip-link {
  transform: translateY(-100%);
}

.skip-link:focus-within {
  transform: translateY(0%);
}

This means that if anything in our .skip-link element gets focus, then we will display it. Unfortunately, neither Internet Explorer nor Opera Mini supports focus-within , but it has very good coverage and can use polyfill.

This browser supports data from Caniuse, which contains more details. The number indicates that the browser supports this feature in this version and later.

The last thing we need to do is make sure that we have an ID on our footer element so that the link has something to jump to.

<footer id="footer"></footer>

This is what this brings to us:

If we want to go a step further (I encourage this), we can style each link differently so that the user can differentiate between the two. Both links in this example are pure white, which is very effective for a single button that performs a single operation, but it makes it more clear that we are dealing with both links if they are rendered differently.

Skip to conclusion

Do you use skip links on your website? Or, if not, does this convince you to use one? I hope it is clear that skipping links is an excellent added value when it comes to accessibility of the website. While it is not a panacea to solve all accessibility issues, it does solve some use cases, thus creating a more complete user experience.

Here are some well-known websites that use this technology or similar technologies:

  • Amazon
  • Chase Bank
  • Google (not skip links, but links that provide accessibility feedback)
  • Target
  • New York Times
  • Zillow

The above is the detailed content of How to Create a 'Skip to Content” Link. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Footnotes That Work in RSS ReadersFootnotes That Work in RSS ReadersApr 21, 2025 am 10:03 AM

Feedbin is the RSS reader I'm using at the moment. I was reading one of Harry's blog posts on it the other day, and I noticed a nice little interactive touch

Clever codeClever codeApr 21, 2025 am 10:01 AM

This week, Chris Ferdinandi examined a clever JavaScript snippet, one that's written creatively with new syntax features, but is perhaps less readable and

Integrating Third-Party Animation Libraries to a ProjectIntegrating Third-Party Animation Libraries to a ProjectApr 21, 2025 am 10:00 AM

Creating CSS-based animations and transitions can be a challenge. They can be complex and time-consuming. Need to move forward with a project with little time

Weekly Platform News: Favicon Guidelines, Accessibility Testing, Web AlmanacWeekly Platform News: Favicon Guidelines, Accessibility Testing, Web AlmanacApr 20, 2025 am 11:28 AM

In this week's news, Google defines guidelines for favicons, a new a11y testing tool from The Paciello Group, and changes to how the W3C plans to engage the community, plus more.

A Little Reminder That Pseudo Elements are Children, Kinda.A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AM

Here's a container with some child elements:

Menus with 'Dynamic Hit Areas'Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AM

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

Improving Video Accessibility with WebVTTImproving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteWeekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AM

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft