The visibility property lets you control if an element is seen or hidden on the page without changing the layout around it.
Visibility property in CSS
selector {
visibility: visible | hidden | collapse | initial | inherit;
}p {
visibility: visible;
}div {
visibility: hidden;
}tr {
visibility: collapse;
}This example shows three boxes stacked vertically. The green box is visible, the red box is hidden but still takes space, and the black border box is visible. You can see the space where the red box is even though you can't see the box itself.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Visibility Property Example</title> <style> .visible-box { width: 8rem; height: 8rem; background-color: #4caf50; visibility: visible; margin-bottom: 1rem; } .hidden-box { width: 8rem; height: 8rem; background-color: #f44336; visibility: hidden; margin-bottom: 1rem; } .border-box { width: 8rem; height: 8rem; border: 2px solid #000; margin-bottom: 1rem; } </style> </head> <body> <div class="visible-box" aria-label="Visible green box"></div> <div class="hidden-box" aria-label="Hidden red box"></div> <div class="border-box" aria-label="Black border box"></div> <p>Notice the red box is invisible but space is still reserved for it.</p> </body> </html>
Unlike display: none;, visibility: hidden; keeps the space reserved on the page.
The collapse value mainly works for table rows and columns, hiding them and removing their space.
Use aria-label or other accessibility features to describe hidden elements if needed.
The visibility property controls if an element is seen or hidden.
hidden hides the element but keeps its space.
This helps keep page layout stable when hiding elements.
Practice
visibility: hidden; do to an element on a webpage?Solution
Step 1: Understand the visibility property
Thevisibilityproperty controls whether an element is visible or hidden but does not affect layout space.Step 2: Analyze the effect of
When set tohiddenhidden, the element is not shown but still occupies space on the page.Final Answer:
It hides the element but keeps its space reserved on the page. -> Option BQuick Check:
visibility: hidden hides but keeps space [OK]
- Confusing visibility: hidden with display: none
- Thinking hidden elements are clickable
- Assuming hidden elements disappear completely
Solution
Step 1: Recall correct CSS property syntax
CSS properties use a colon:between property and value, ending with a semicolon.Step 2: Match syntax to visibility property
The correct syntax isvisibility: hidden;to hide but keep space.Final Answer:
visibility: hidden; -> Option AQuick Check:
Property: value; is correct CSS syntax [OK]
- Using equals sign instead of colon
- Swapping property and value order
- Using non-existent CSS properties
<div style="visibility: hidden; background: red; width: 100px; height: 100px;">Box</div>
What will you see on the webpage?
Solution
Step 1: Analyze the visibility property effect
The stylevisibility: hidden;hides the element but keeps its space on the page.Step 2: Understand the visual output
The red box and text are hidden, so nothing is visible, but the space of 100x100 pixels remains empty.Final Answer:
No red box or text, but space is reserved where the box would be. -> Option DQuick Check:
visibility: hidden hides content but keeps space [OK]
- Expecting the box to be visible
- Confusing with display:none which removes space
- Thinking text remains visible
.hidden-box { visibility = hidden; }What is the error?
Solution
Step 1: Check CSS property syntax
CSS uses a colon:to assign values, not an equals sign=.Step 2: Identify the syntax error
The code usesvisibility = hidden;which is invalid syntax and ignored by browsers.Final Answer:
The syntax uses '=' instead of ':'. -> Option AQuick Check:
Use colon for CSS property assignment [OK]
- Using '=' instead of ':' in CSS
- Confusing class selector '.' with id selector '#'
- Assuming 'hidden' is invalid value
Solution
Step 1: Understand layout impact of hiding methods
display: none;removes element and space,opacity: 0;hides visually but keeps clickable,position: absolute; left: -9999px;moves element off screen.Step 2: Choose method that hides but keeps space
visibility: hidden;hides element but keeps its space, keeping layout stable.Final Answer:
visibility: hidden; -> Option CQuick Check:
visibility: hidden hides but keeps space [OK]
- Using display:none which removes space
- Using opacity:0 but element remains interactive
- Moving element off screen breaks accessibility
Ready to prove it?
Pass the test and this pattern moves unknown
