Think of a website that has these two features: a menu that unfolds when you click on it, and a pop-up window with a hint that activates when you hover the cursor over it. What do the menu and the hint have in common? Both change the visibility of the element. When you think about how to hide elements or make them visible, it may seem quite difficult to implement. However, the CSS visibility property is actually quite easy to use. Let's study this property in more detail.
Syntax
The visibility property is intended to hide or show the element without changing the markup of the HTML document. You can set it to the following values:
visibleis the default value, meaning that the element is displayed as visible.hiddenmakes the element invisible.collapsecan hide rows and columns of a table, and the table will be reconstructed taking the missing elements into account. If you try to apply this value to something that is not a table element, the result will be the same as if you usedhidden.inheritwill allow you to inherit the value of the parent element.initialsets the property to its default value.unsetis basically a combination of theinitialandinheritvalues. Theunsetkeyword sets the property value asinheritif the property is inherited from the parent element; otherwise, the value is set asinitial.
Examples of property usage
Take a look at an example of using the visibility property. Try to understand what happens in this code:
h1 {
visibility: hidden;
}In this example, it is specified that the <h1> element should be invisible.
Let's apply this to the following HTML code:
<h1>Chess online</h1>
<p>Start a new game</p>This is the result that we will see in the browser:
Note that although the <h1> element is hidden, it can still be found in the HTML markup. You can use the developer tools (Ctrl+Shift+I or Cmd+Opt+I) to verify this:
Even if you hide elements using the visibility property, they remain in the markup. If you want to skip completely when rendering the document as if the element did not exist at all, then instead of using visibility, set the display property to the value none.
If you need more examples, check out the interactive page about the visibility property at MDN.
Conclusion
Skillful magicians can make objects appear on the stage or disappear completely. Now you can compete with them, as in this topic you have learned the property that changes the visibility of elements. Abracadabra.