Skip to content

Keep hidden components hidden#1889

Open
frankieroberto wants to merge 2 commits into
mainfrom
keep-hidden-component-hidden
Open

Keep hidden components hidden#1889
frankieroberto wants to merge 2 commits into
mainfrom
keep-hidden-component-hidden

Conversation

@frankieroberto

Copy link
Copy Markdown
Contributor

Browsers by default will hide any element with a hidden attribute using display: none.

However if you set a different display property on a class then the item will no longer be hidden even if the attribute is set.

This updates our CSS so that we don't set a display property on components which have a hidden attribute.

This is mainly useful for any pages using JavaScript to dynamically show or hide elements.

Also adds a test page with all the components hidden.

Browsers by default will hide any element with a `hidden` attribute using `display: none`.

However if you set a different `display` property on a class then the item will no longer be hidden even if the attribute is set.

This updates our CSS so that we don't set a display property on components which have a hidden attribute.

This is mainly useful for any pages using JavaScript to dynamically show or hide elements.
@colinrotherham
colinrotherham temporarily deployed to nhsuk-frontend-pr-1889 April 15, 2026 10:03 Inactive
Comment on lines +7 to +10
// Hide hidden button groups
.nhsuk-button-group[hidden] {
display: none;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would ideally be a :not([hidden]) suffix in the css below, but that doesn't work for some Sass reason to do with the other nested selectors.

@colinrotherham

colinrotherham commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Thanks @frankieroberto, this is great 🙌

We need to take care when it comes to specificity as :not([hidden]) is like adding another class

  • .nhsuk-back-link:not([hidden]) now overrides .nhsuk-back-link--reverse
  • .nhsuk-button:not([hidden]) now overrides .nhsuk-button--icon

The latter is only on main but you'd see display: flex display: inline-block for icon buttons

From our Slack chat, I wonder if your [hidden] { display: none !important; } idea is better?

Backstop report showing back link specificity issue

@frankieroberto

Copy link
Copy Markdown
Contributor Author

@colinrotherham ah!

I’m now thinking that a global *[hidden] { } selector would be better - less likely that we forget to exclude hidden elements in other css.

@frankieroberto
frankieroberto temporarily deployed to nhsuk-frontend-pr-1889 April 15, 2026 11:12 Inactive
@frankieroberto

Copy link
Copy Markdown
Contributor Author

Switched to a global !important rule in d7c3623.

@sonarqubecloud

Copy link
Copy Markdown


// Add shadow to bottom of header when open
&:has(.nhsuk-header__menu-list:not([hidden])) {
&:has(.nhsuk-header__menu-list) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is drawing a grey border below the menu even when it's closed, sorry 😣

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants