CSS Visibility

By Codes With Pankaj


The visibility property in CSS controls whether an element is visible or hidden on a webpage. Unlike the display property, which removes the element from the document flow, the visibility property allows you to hide an element while still preserving its space in the layout. This tutorial will explore the different values of the visibility property, explain how they work, and provide practical examples.

Table of Contents

  1. What is the CSS Visibility Property?

  2. Visibility vs Display: Key Differences

  3. CSS Visibility Property Values

    • Visible

    • Hidden

    • Collapse

  4. Practical Examples

    • Example 1: Using Visibility to Toggle Content

    • Example 2: Preserving Space with Visibility

    • Example 3: Using Visibility with JavaScript

  5. Advanced Techniques with Visibility

    • Accessibility Considerations

    • Animating Visibility

  6. Conclusion


1. What is the CSS Visibility Property?

The visibility property in CSS determines whether an element is visible or hidden on the webpage. However, unlike display: none, which removes the element from the document flow, visibility: hidden hides the element but still takes up the same space on the page.

Syntax:

selector {
  visibility: value;
}

Common Values:

  • visible

  • hidden

  • collapse (used primarily for table elements)


2. Visibility vs Display: Key Differences

The visibility and display properties both affect how an element appears on the page, but they work in different ways:

  • Visibility:

    • visibility: hidden hides the element but preserves its space in the layout.

    • The element remains in the document flow, meaning other elements are not reflowed or repositioned.

  • Display:

    • display: none removes the element from the document flow entirely.

    • The element takes up no space, and surrounding elements are reflowed to fill the gap.

Key Takeaway: Use visibility when you want to hide an element without affecting the layout, and use display when you want to remove the element from the layout entirely.


3. CSS Visibility Property Values

The visibility property has three primary values that control how an element is displayed.

a. Visible

visible is the default value of the visibility property. When set to visible, the element is fully rendered on the page.

Example:

.visible-element {
  visibility: visible;
  background-color: lightgreen;
  padding: 10px;
}

Behavior:

  • The element is fully visible and takes up space in the layout.

Use Cases:

  • The default behavior for most elements, ensuring they are visible on the page.

b. Hidden

hidden hides the element from view but still preserves its space in the document flow. The element is not rendered, but it still affects the layout as if it were visible.

Example:

.hidden-element {
  visibility: hidden;
  background-color: lightcoral;
  padding: 10px;
}

Behavior:

  • The element is hidden but still takes up space in the layout.

Use Cases:

  • Temporarily hiding content without causing layout shifts or reflows.

  • Maintaining the structure of a layout when elements are conditionally shown or hidden.

c. Collapse

The collapse value is used primarily for table rows and columns. When set to collapse, the row or column is removed from the layout, similar to display: none. For other elements, collapse behaves the same as hidden.

Example:

.collapsed-row {
  visibility: collapse;
}

Behavior:

  • For table rows or columns, the element is removed from the layout.

  • For other elements, it behaves like visibility: hidden.

Use Cases:

  • Collapsing table rows or columns while preserving the overall table structure.


4. Practical Examples

Let's explore some practical examples of how to use the visibility property effectively.

Example 1: Using Visibility to Toggle Content

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Toggle Visibility Example - codeswithpankaj</title>
  <style>
    .toggle-content {
      visibility: hidden;
      background-color: lightblue;
      padding: 20px;
      margin: 10px 0;
    }

    .visible {
      visibility: visible;
    }
  </style>
</head>
<body>

<button onclick="toggleVisibility()">Toggle Content Visibility</button>
<div id="content" class="toggle-content">This content is toggled using visibility.</div>

<script>
  function toggleVisibility() {
    var content = document.getElementById('content');
    if (content.style.visibility === 'hidden') {
      content.style.visibility = 'visible';
    } else {
      content.style.visibility = 'hidden';
    }
  }
</script>

</body>
</html>

Explanation:

  • The content starts hidden, but clicking the button toggles its visibility without affecting the layout.

Example 2: Preserving Space with Visibility

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Preserving Space with Visibility - codeswithpankaj</title>
  <style>
    .box {
      width: 200px;
      height: 100px;
      margin: 10px;
      background-color: lightyellow;
    }

    .hidden-box {
      visibility: hidden;
    }
  </style>
</head>
<body>

<div class="box">Box 1</div>
<div class="box hidden-box">Box 2 (Hidden)</div>
<div class="box">Box 3</div>

</body>
</html>

Explanation:

  • Box 2 is hidden, but it still takes up space between Box 1 and Box 3, preserving the layout.

Example 3: Using Visibility with JavaScript

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Visibility with JavaScript - codeswithpankaj</title>
  <style>
    .message {
      visibility: hidden;
      background-color: lightpink;
      padding: 10px;
      margin-top: 10px;
    }
  </style>
</head>
<body>

<button onclick="showMessage()">Show Message</button>
<div id="message" class="message">Hello, this message is now visible!</div>

<script>
  function showMessage() {
    document.getElementById('message').style.visibility = 'visible';
  }
</script>

</body>
</html>

Explanation:

  • The message is initially hidden, but clicking the button reveals it without altering the layout of the page.


5. Advanced Techniques with Visibility

Beyond the basics, the visibility property can be used in more advanced ways to enhance the user experience and accessibility of a webpage.

a. Accessibility Considerations

When hiding content with visibility: hidden, it’s important to consider accessibility:

  • Screen Readers: Some screen readers may still announce elements with visibility: hidden. To ensure content is truly hidden from assistive technologies, consider using aria-hidden="true" in conjunction with visibility: hidden.

Example:

<div class="hidden-element" aria-hidden="true">This content is hidden from screen readers.</div>

Explanation:

  • The aria-hidden="true" attribute ensures that the hidden content is not announced by screen readers.

b. Animating Visibility

While you cannot directly animate the visibility property, you can create similar effects by combining it with other properties like opacity and transition.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Animating Visibility - codeswithpankaj</title>
  <style>
    .fade {
      visibility: hidden;
      opacity: 0;
      transition: opacity 0.5s ease-in-out;
    }

    .fade.visible {
      visibility: visible;
      opacity: 1;
   

 }
  </style>
</head>
<body>

<button onclick="fadeIn()">Fade In Content</button>
<div id="fadeContent" class="fade">This content fades in with visibility and opacity.</div>

<script>
  function fadeIn() {
    document.getElementById('fadeContent').classList.add('visible');
  }
</script>

</body>
</html>

Explanation:

  • The content fades in smoothly when the visible class is added, creating a visually appealing transition.


6. Conclusion

The visibility property in CSS is a versatile tool for controlling the visibility of elements on a webpage without altering the layout. By understanding the differences between visibility and display, you can choose the right approach for your design needs. Whether you’re hiding content temporarily, preserving layout space, or creating advanced animations, the visibility property offers a range of possibilities.

  • Visible: The element is visible and takes up space.

  • Hidden: The element is hidden but still occupies its space in the layout.

  • Collapse: Primarily for table elements, removing them from the layout.

By mastering these concepts, you can create more dynamic and user-friendly web designs.


For more tutorials and insights, visit Codes With Pankaj.

Last updated