- visibility
- Try it
- Syntax
- Values
- Accessibility concerns
- Interpolation
- Notes
- Formal definition
- Formal syntax
- CSS Layout — The display Property
- The display Property
- Block-level Elements
- Inline Elements
- Display: none;
- Override The Default Display Value
- Example
- Example
- hidden
- Try it
- Description
- The hidden state
- The hidden until found state
- Examples
- Using until-found
- HTML
- CSS
- JavaScript
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- HTMLElement: hidden property
- Value
- Examples
- HTML
- CSS
- JavaScript
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
visibility
The visibility CSS property shows or hides an element without changing the layout of a document. The property can also hide rows or columns in a .
Try it
To both hide an element and remove it from the document layout, set the display property to none instead of using visibility .
Syntax
/* Keyword values */ visibility: visible; visibility: hidden; visibility: collapse; /* Global values */ visibility: inherit; visibility: initial; visibility: revert; visibility: revert-layer; visibility: unset;
The visibility property is specified as one of the keyword values listed below.
Values
The element box is visible.
The element box is invisible (not drawn), but still affects layout as normal. Descendants of the element will be visible if they have visibility set to visible . The element cannot receive focus (such as when navigating through tab indexes).
The collapse keyword has different effects for different elements:
- For rows, columns, column groups, and row groups, the row(s) or column(s) are hidden and the space they would have occupied is removed (as if display : none were applied to the column/row of the table). However, the size of other rows and columns is still calculated as though the cells in the collapsed row(s) or column(s) are present. This value allows for the fast removal of a row or column from a table without forcing the recalculation of widths and heights for the entire table.
- Collapsed flex items and ruby annotations are hidden, and the space they would have occupied is removed.
- For other elements, collapse is treated the same as hidden .
Accessibility concerns
Using a visibility value of hidden on an element will remove it from the accessibility tree. This will cause the element and all its descendant elements to no longer be announced by screen reading technology.
Interpolation
When animated, visibility values are interpolated between visible and not-visible. One of the start or ending values must therefore be visible or no interpolation can happen. The value is interpolated as a discrete step, where values of the easing function between 0 and 1 map to visible and other values of the easing function (which occur only at the start/end of the transition or as a result of cubic-bezier() functions with y values outside of [0, 1]) map to the closer endpoint.
Notes
- Support for visibility: collapse is missing or partially incorrect in some modern browsers. It may not be correctly treated like visibility: hidden on elements other than table rows and columns.
- visibility: collapse may change the layout of a table if the table has nested tables within the cells that are collapsed, unless visibility: visible is specified explicitly on nested tables.
Formal definition
Formal syntax
CSS Layout — The display Property
The display property is the most important CSS property for controlling layout.
The display Property
The display property specifies if/how an element is displayed.
Every HTML element has a default display value depending on what type of element it is. The default display value for most elements is block or inline .
This panel contains a element, which is hidden by default ( display: none ).
It is styled with CSS, and we use JavaScript to show it (change it to ( display: block ).
Block-level Elements
A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).
Examples of block-level elements:
Inline Elements
An inline element does not start on a new line and only takes up as much width as necessary.
This is an inline element inside a paragraph.
Examples of inline elements:
Display: none;
display: none; is commonly used with JavaScript to hide and show elements without deleting and recreating them. Take a look at our last example on this page if you want to know how this can be achieved.
The element uses display: none; as default.
Override The Default Display Value
As mentioned, every element has a default display value. However, you can override this.
Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow the web standards.
Example
Note: Setting the display property of an element only changes how the element is displayed, NOT what kind of element it is. So, an inline element with display: block; is not allowed to have other block elements inside it.
The following example displays elements as block elements:
Example
The following example displays elements as block elements:
hidden
The hidden global attribute is an enumerated attribute indicating that the browser should not render the contents of the element. For example, it can be used to hide elements of the page that can’t be used until the login process has been completed.
Try it
Description
The hidden attribute is used to indicate that the content of an element should not be presented to the user. This attribute can take any one of the following values:
There are two states associated with the hidden attribute: the hidden state and the hidden until found state.
- An empty string, or the keyword hidden , set the element to the hidden state. Additionally, invalid values set the element to the hidden state.
- The keyword until-found sets the element to the hidden until found state.
Thus, all the following set the element to the hidden state:
span hidden>I'm hiddenspan> span hidden="hidden">I'm also hiddenspan> span hidden="something else">I'm hidden too!span>
The following sets the element to the hidden until found state:
span hidden="until-found">I'm hidden until foundspan>
The hidden attribute must not be used to hide content just from one presentation. If something is marked hidden, it is hidden from all presentations, including, for instance, screen readers.
Hidden elements shouldn’t be linked from non-hidden elements. For example, it would be incorrect to use the href attribute to link to a section marked with the hidden attribute. If the content is not applicable or relevant, then there is no reason to link to it.
It would be fine, however, to use the ARIA aria-describedby attribute to refer to descriptions that are themselves hidden. While hiding the descriptions implies that they are not useful on their own, they could be written in such a way that they are useful in the specific context of being referenced from the element that they describe.
Similarly, a canvas element with the hidden attribute could be used by a scripted graphics engine as an off-screen buffer, and a form control could refer to a hidden form element using its form attribute.
Elements that are descendants of a hidden element are still active, which means that script elements can still execute and form elements can still submit.
The hidden state
The hidden state indicates that the element is not currently relevant to the page, or that it is being used to declare content for reuse by other parts of the page and should not be directly presented to the user. The browser will not render elements that are in the hidden state.
Web browsers may implement the hidden state using display: none , in which case the element will not participate in page layout. This also means that changing the value of the CSS display property on an element in the hidden state will override the state. For instance, elements styled display: block will be displayed despite the hidden attribute’s presence.
The hidden until found state
In the hidden until found state, the element is hidden but its content will be accessible to the browser’s «find in page» feature or to fragment navigation. When these features cause a scroll to an element in a hidden until found subtree, the browser will:
- fire a beforematch event on the hidden element
- remove the hidden attribute from the element
- scroll to the element
This enables a developer to collapse a section of content, but make it searchable and accessible via fragment navigation.
Note that browsers typically implement hidden until found using content-visibility: hidden . This means that unlike elements in the hidden state, elements in the hidden until found state will have generated boxes, meaning that:
- the element will participate in page layout
- margin, borders, padding, and background for the element will be rendered.
Also, the element needs to be affected by layout containment in order to be revealed. This means that if the element in the hidden until found state has a display value of none , contents , or inline , then the element will not be revealed by find in page or fragment navigation.
Examples
Using until-found
The hidden until found element has a dotted red border and a gray background.
We also have some JavaScript that listens for the beforematch event firing on the hidden until found element. The event handler changes the text content of the box.
HTML
a href="#until-found-box">Go to hidden contenta> div>I'm not hiddendiv> div id="until-found-box" hidden="until-found">Hidden until founddiv> div>I'm not hiddendiv>
button id="reset">Resetbutton>
CSS
div height: 40px; width: 300px; border: 5px dashed black; margin: 1rem 0; padding: 1rem; font-size: 2rem; > div#until-found-box color: red; border: 5px dotted red; background-color: lightgray; >
#until-found-box scroll-margin-top: 200px; >
JavaScript
const untilFound = document.querySelector("#until-found-box"); untilFound.addEventListener( "beforematch", () => (untilFound.textContent = "I've been revealed!"), );
.querySelector("#reset").addEventListener("click", () => document.location.hash = ""; document.location.reload(); >);
Result
Note that although the content of the element is hidden, the element still has a generated box, occupying space in the layout and with background and borders rendered.
Clicking the «Go to hidden content» button navigates to the hidden until found element. The beforematch event fires, the text content is updated, and the element content is displayed.
To run the example again, click «Reset».
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 17, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
HTMLElement: hidden property
The HTMLElement property hidden reflects the value of the element’s hidden attribute.
Value
This attribute may have one of three values:
The element is not hidden. This is the default value for the attribute.
The element is hidden until found, meaning that it is hidden but will be revealed if found through in page search or reached through fragment navigation.
For details on the usage of this attribute, see the page for the hidden HTML attribute that this property reflects.
Examples
Here’s an example where a hidden block is used to contain a ‘thank you’ message that is displayed after a user agrees to an unusual request.
HTML
The HTML contains two panels: a welcome panel, that asks users to agree to be awesome, and a follow-up panel, which is initially hidden.
div id="welcome" class="panel"> h1>Welcome to Foobar.com!h1> p>By clicking "OK" you agree to be awesome today!p> button class="button" id="okButton">OKbutton> div> div id="awesome" class="panel" hidden> h1>Thanks!h1> p>Thanks for agreeing to be awesome today!p> div>
CSS
The content is styled using the CSS below.
.panel font: 16px "Open Sans", Helvetica, Arial, sans-serif; border: 1px solid #22d; padding: 12px; width: 500px; text-align: center; > .button font: 22px "Open Sans", Helvetica, Arial, sans-serif; padding: 5px 36px; > h1 margin-top: 0; font-size: 175%; >
JavaScript
The JavaScript adds an event listener to the «OK» button, which hides the «welcome» panel and shows the «awesome» panel:
.getElementById("okButton").addEventListener( "click", () => document.getElementById("welcome").hidden = true; document.getElementById("awesome").hidden = false; >, false, );
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.