Accessible Offscreen Element Hiding

CSSCSSBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the concept of hiding elements offscreen in CSS. This technique allows us to make elements inaccessible visually and positionally while still allowing them to be accessible to assistive technologies. By the end of this lab, you will have a solid understanding of how to use this technique to create more accessible and layout-friendly web pages.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("`CSS`")) -.-> css/BasicConceptsGroup(["`Basic Concepts`"]) css(("`CSS`")) -.-> css/CoreLayoutGroup(["`Core Layout`"]) css/BasicConceptsGroup -.-> css/selectors("`Selectors`") css/CoreLayoutGroup -.-> css/margin_and_padding("`Margin and Padding`") css/CoreLayoutGroup -.-> css/borders("`Borders`") css/CoreLayoutGroup -.-> css/width_and_height("`Width and Height`") css/CoreLayoutGroup -.-> css/positioning("`Positioning`") subgraph Lab Skills css/selectors -.-> lab-35227{{"`Accessible Offscreen Element Hiding`"}} css/margin_and_padding -.-> lab-35227{{"`Accessible Offscreen Element Hiding`"}} css/borders -.-> lab-35227{{"`Accessible Offscreen Element Hiding`"}} css/width_and_height -.-> lab-35227{{"`Accessible Offscreen Element Hiding`"}} css/positioning -.-> lab-35227{{"`Accessible Offscreen Element Hiding`"}} end

Offscreen

index.html and style.css have already been provided in the VM.

This technique completely hides an element in the DOM while still making it accessible. To achieve this, you can follow these steps:

  • Remove all borders and padding and hide the element's overflow.
  • Use clip to ensure that no part of the element is shown.
  • Set the height and width of the element to 1px and negate them using margin: -1px.
  • Use position: absolute to prevent the element from taking up space in the DOM.
  • Note that this technique is a better alternative to display: none (not readable by screen readers) and visibility: hidden (takes up physical space in the DOM) in terms of accessibility and layout-friendliness.

Here's an example of how you can use this technique in HTML and CSS:

<a class="button" href="https://google.com">
  Learn More <span class="offscreen">about pants</span>
</a>
.offscreen {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

Summary

Congratulations! You have completed the Offscreen lab. You can practice more labs in LabEx to improve your skills.

Other CSS Tutorials you may like