CSS Visibility

The visibility property in CSS is used to control whether an element is visible or hidden. However, unlike the display property, visibility does not remove the element from the document flow. An element with visibility: hidden will still occupy space in the layout, but it will not be visible.

Key Points on CSS Visibility:

Syntax for CSS Visibility:

Syntax Example

/* Visibility syntax */
        visibility: visible | hidden | collapse;
                
        /* Example */
        visibility: hidden;

Examples of CSS Visibility:

The following examples demonstrate the usage of the visibility property:

Code Example: Visibility Visible

div {
            visibility: visible;
        }

Output

This div is visible.

Code Example: Visibility Hidden

div {
            visibility: hidden;
        }

Output

This div is hidden but still occupies space.

Code Example: Visibility Collapse (for table rows)

table tr {
            visibility: collapse;
        }

Output

Visible Row 1
This row is collapsed and not visible.
Visible Row 2

Common Visibility Property Values:

When using the visibility property, it's important to remember that the element will still affect the document flow, unlike using display: none. Use visibility when you want to hide elements without disrupting the overall page layout.