Code Snippets
/

CSS Utility Tricks: Invisible Text and Filters

CSS Utility Tricks: Invisible Text and Filters

Two utility recipes that come up over and over: hiding content correctly (visually-hidden but readable to screen readers, plus a decision matrix for the four ways to make something disappear) and using `filter` for cheap visual effects like blur, grayscale, and drop-shadow. Both are short on their own but easy to get wrong, so they live together as a quick reference.

CSS
Easy
3 snippets
css-display
css-filters
accessibility

471 views

11

/* The .sr-only / .visually-hidden recipe.
   Hides the element visually but keeps it in the accessibility tree so
   screen readers and search engines still see it. */
.sr-only,
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Variant that becomes visible when focused (skip-to-main-content links). */
.sr-only-focusable:not(:focus):not(:focus-within) {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Example HTML where this matters:
   <button>
     <span class="sr-only">Close modal</span>
     <svg aria-hidden="true">...</svg>
   </button>

   The visible label is the icon, but assistive tech reads "Close modal". */

The visually-hidden technique is the standard pattern for making content invisible without removing it from the accessibility tree. Crucially, it uses position: absolute, a 1x1 size, and clip: rect(0,0,0,0) rather than display: none, because display: none removes the element entirely; screen readers will not see it. Use this every time an icon-only button needs a label, or when you want extra context (like "opens in a new tab") that a sighted user does not need. The focusable variant is for skip-links: hidden by default, but visible when a keyboard user tabs to them, which is a baseline accessibility requirement.