DEV Community

Cover image for Invisible Yet Focusable
Tryggvi Gylfason
Tryggvi Gylfason

Posted on

1 2

Invisible Yet Focusable

These two mixins are one of my favorites. Flexible and useful for creating accessible interfaces.

/*
* Hide only visually, but have it available for screen readers:
* https://snook.ca/archives/html_and_css/hiding-content-for-accessibility
*/
@mixin screen-reader-only() {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

@mixin undo-screen-reader-only($position: unset) {
  position: $position;
  opacity: 1;
  pointer-events: auto;
}
Enter fullscreen mode Exit fullscreen mode

They are quite handy when hiding something visually.

By using mixins rather than classes we can keep working in pure CSS when showing/hiding the elements instead of reaching for JS to toggle classes.

Another benefit these mixins have (over display:none and visibility:hidden) is that the element can still receive focus, and when it does, we can undo the mixin to show the element. Again, no JS needed. In the cards below, try pressing Shift+Tab to jump to the play button on the previous card.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay