Beginner2 min read

CSS Visibility Property

2 min read
260 words
11 sections

The visibility property controls whether an element is visible while maintaining its space in the document layout, offering distinct behavior from the display property.

Understanding Visibility Values

The visibility property accepts three main values, each serving different use cases in web development.

Visibility: Visible

The default state where elements are fully rendered and visible to users.

Visibility: Hidden

Hides the element while preserving its layout space. The element becomes invisible but still occupies the same area in the document flow.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Visibility: Collapse

Primarily used for table rows and columns, removing the element while collapsing the space it occupied.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Visibility vs Display: None

The key distinction between visibility: hidden and display: none lies in layout preservation. Hidden elements retain their space while display: none removes elements entirely from the flow.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Practical Use Cases

Placeholder Layouts

Visibility hidden works perfectly for maintaining layout stability when content loads dynamically.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Hover Effects

Create interactive effects where hidden content appears on user interaction.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Child Element Visibility

Unlike display: none, visibility can be overridden by child elements. A visible child inside a hidden parent can still appear.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Accessibility Considerations

Elements with visibility: hidden are removed from the accessibility tree, making them invisible to screen readers. For accessible hiding that preserves screen reader access, consider alternative techniques covered in our CSS Accessibility guide.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Combining with Transitions

Since visibility is an animatable property, combine it with opacity for smooth fade effects as demonstrated in our CSS Transitions article.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Understanding when to use visibility versus display gives you precise control over element rendering and layout behavior in your designs.