Cover Image for CSS Visibility
151 views

CSS Visibility

The CSS visibility property is used to control the visibility of an element in the document layout. It allows you to hide or show elements without affecting the layout of other elements on the page.

The syntax for the visibility property is as follows:

selector {
  visibility: value;
}
  • selector: Represents the HTML element or class to which the visibility property will be applied.
  • value: Specifies the visibility state of the element. It can take one of the following values:
  • visible (default): The element is visible and takes up space in the layout.
  • hidden: The element is hidden, but it still takes up space in the layout. The space occupied by the hidden element will be empty, and other elements will not fill in the space.

Example:

p {
  visibility: hidden;
}

In this example, all paragraphs (<p> elements) will have their visibility set to hidden, making them invisible on the page. However, the space they occupy in the layout will remain, and other elements will not shift to fill the empty space left by the hidden paragraphs.

To completely remove the element from the layout (making it invisible and not taking up any space), you can use the display property with a value of none.

Example:

div {
  display: none;
}

In this example, all <div> elements will have their display set to none, effectively hiding the elements and removing them from the layout. Other elements will shift to fill the space left by the hidden divs.

It’s essential to use the visibility property and the display property appropriately, depending on the desired behavior. If you want to hide an element but maintain its space in the layout, use visibility: hidden;. If you want to hide an element and remove it from the layout flow, use display: none;.

Keep in mind that if an element is hidden with visibility: hidden; or display: none;, it will not be accessible to screen readers or other assistive technologies. Consider the accessibility implications when hiding elements on your website.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS