Overview
Hidden is a set of CSS tools that remove an element from view. The native hidden attribute hides an element at every width. data-hidden hides it only inside the screen widths it names. The sr token in data-hidden, and the nds-sr-only class, hide it on screen but keep it for screen readers.
Pick another component when:
- the content shows and hides when the user clicks a heading: Accordion
- the content changes with a tab: Tabs
- actions that do not fit on a phone move into a menu: Dropmenu
- long text stops after a number of lines: Truncate Text
Markup
<button class="nds-btn nds-neutral" type="button">
<i class="nds-icon nds-hgi-share-01" aria-hidden="true"></i>
<span class="nds-label" data-hidden="sm">Share</span>
</button>
Variants
The canon is a Share button, and every option goes on its label. The label starts hidden on phones (SM). SM, MD and LG add tokens to one data-hidden attribute, and any mix can be on: SM and MD together write data-hidden="sm md". Always writes the hidden attribute instead, so it is off while a width is chosen. Keep for Screen Readers adds the sr token to data-hidden, so it needs a width first.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Hide At (any) | SM (default) (hint: Hidden on phones) | [data-hidden~="sm"] |
.nds-label:not([hidden]) |
Hides the element on screens narrower than 600px |
| Hide At (any) | MD (hint: Hidden on tablets) | [data-hidden~="md"] |
.nds-label:not([hidden]) |
Hides the element on screens from 600px to 959px |
| Hide At (any) | LG (hint: Hidden on desktops) | [data-hidden~="lg"] |
.nds-label:not([hidden]) |
Hides the element on screens 960px and wider |
| Hide At (any) | Always (hint: The hidden attribute: hidden at every width) | [hidden] |
.nds-label:not([data-hidden]) |
Hides the element at every width and from screen readers. Use it for state your script turns on and off. Never on a button’s only label: the button loses its name |
| Screen Readers | Keep for Screen Readers (hint: Hidden on screen only. Screen readers still read it) | [data-hidden~="sr"] |
.nds-label[data-hidden] |
Hides the element on screen only, and screen readers still read it. On a button label, the button shows only its icon and keeps its name |
Built-in Features
-
The hidden Attribute Wins
An element with
hiddenstays hidden even when a component or a utility gives it a flex or grid display. The rule usesdisplay: none !important. -
Exact Width Ranges
Each
data-hiddentoken hides the element only inside its own range. Hiding at MD does not also hide at SM. -
Combined Ranges
Tokens separated by spaces add up.
data-hidden="sm md"hides the element below 960px, anddata-hidden="md lg"shows it on phones only. -
Screen Reader Text
The
srtoken andnds-sr-onlyremove the element from the screen, but screen readers still read it. A button that shows only its icon keeps its name. -
CSS Only
The
hiddenrule loads with the page's first styles, so a hidden element never shows before the main CSS arrives. There is nothing to initialize.
Best Practices
- Use
hiddenfor state: a panel, a menu or a step that your script shows and hides. Usedata-hiddenfor an element that has no place at some screen widths, such as a topbar widget or secondary details. - One element can carry
hiddenanddata-hidden. Either one hides it. - Use
data-hiddeninstead of your own media query. Its ranges match the NDS breakpoints. hiddenanddata-hiddenwithoutsralso hide the element from screen readers. Addsrwhen the text must still be read.- To show only a button’s icon on phones, write
data-hidden="sm sr"on its label. Never hide the label withoutsr: the button loses its name. - A button whose label
data-hiddenhides is a square icon button in that range, asnds-icon-onlymakes it. - Use
nds-sr-onlyfor text that only screen readers need at every width, such as a word that tells screen readers what a number badge counts. - Do not hide a main action on phones. Move it into a Dropmenu, so the user can still reach it.
- A hidden element still loads its images and frames. Leave heavy content out of the markup instead of hiding it.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-sr-only |
Any element | Hides the element on screen at every width. Screen readers still read it |
nds-hidden |
Any element | Hides the element, the same as hidden. NDS scripts write it to match data-state="hidden": write hidden or data-state yourself, not this class |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
hidden |
Any element | Hides the element at every width and from screen readers, over any display value. Set it yourself, or let a component’s script set it |
data-hidden |
Any element | Hides the element inside each width range it names: sm (narrower than 600px), md (600px to 959px), lg (960px and wider), or print (on paper only, see Print). Separate tokens with spaces. Add sr to hide it on screen only |
data-state |
Any element | The hidden token hides the element, the same as hidden. Component scripts write it through NDS.state |
Related
- Manage Records: Search and Filter buttons that show only their icons on phones.
- Search Template: the same toolbar buttons above search results.
- FAQ Template: the same toolbar buttons above the questions.