Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In modern web development, you will frequently need to hide and show elements. 👁️
Think about everyday interactive web patterns:
CSS offers multiple ways to make elements disappear, but the two most famous techniques—display: none and visibility: hidden—behave very differently under the hood!
In this lesson, you will master:
display: none removes elements from the document flowvisibility: hidden hides elements while preserving layout spaceopacity: 0Imagine a row of desks in your School Classroom:
+-------------------------------------------------------------------------+ | THE 3 WAYS TO HIDE A STUDENT IN CLASS | +-------------------------------------------------------------------------+ | 1. display: none (STUDENT ABSENT & DESK REMOVED) | | Rohan is absent today. The teacher removes his wooden desk entirely. | | Amit and Priya (who were sitting behind Rohan) slide their desks | | forward to close the empty gap. It is as if Rohan was never there! | | | | 2. visibility: hidden (HARRY POTTER'S INVISIBILITY CLOAK) | | Rohan puts on an invisibility cloak while sitting at his desk. | | Nobody can see Rohan, BUT HIS DESK AND CHAIR ARE STILL THERE! | | Amit cannot slide forward because Rohan's empty space is occupied! | | | | 3. opacity: 0 (THE TRANSPARENT GLASS PANE) | | Rohan stands behind a perfectly clean sheet of clear glass. | | He is invisible, his space is preserved, AND if you throw a paper | | plane, it hits the glass (it still receives mouse clicks!). | +-------------------------------------------------------------------------+
display: none; (The Complete Eraser)When you apply display: none; to an element:
visibility: hidden; (The Invisible Space Holder)When you apply visibility: hidden; to an element:
opacity: 0; (The Glass Window)opacity sets the transparency level of an element between 1 (fully solid) and 0 (fully transparent).
opacity: 0:Unlike visibility: hidden;, an element with opacity: 0; can still receive mouse clicks, hovers, and keyboard focus unless you disable pointer events with pointer-events: none;!
| Property | Takes Up Space? | Clickable / Interactive? | Supports CSS Transitions? | Accessible to Screen Readers? |
|---|---|---|---|---|
display: none; | ❌ No (0px space) | ❌ No | ❌ No (abrupt on/off) | ❌ No (ignored) |
visibility: hidden; | ✅ Yes (space reserved) | ❌ No | ⚠️ Limited (visibility delay) | ❌ No |
opacity: 0; | ✅ Yes (space reserved) | ✅ Yes (unless disabled) | ✅ Yes (smooth fades) | ⚠️ Yes (still in DOM) |
Use display: none;! When a modal dialog or navigation submenu is closed, you do not want an empty 300px white space pushing the rest of your website down.
Use visibility: hidden;! If your form reserves a 20px spot for a red error message ("Invalid roll number!"), using visibility: hidden; prevents the entire submit button from jumping up and down every time an error appears or disappears!
| Bad Practice ❌ | Why It Fails ⚠️ | Better Approach ✅ |
|---|---|---|
Trying to animate display: none to display: block with transition | display is a binary property; browsers cannot smoothly interpolate between "non-existent" and "block". | Animate opacity: 0 to opacity: 1 or use CSS keyframe animations. |
Using visibility: hidden for a popup modal | The closed modal leaves a giant invisible blank block in the middle of the webpage! | Use display: none; so closed modals consume zero layout space. |
Leaving opacity: 0 on an invisible button without pointer-events: none | Visitors accidentally click an invisible button floating above your text. | Add pointer-events: none; whenever using opacity: 0;. |
Confusing visibility: collapse with hidden | collapse is specifically designed for table rows/columns to hide them without recalculating cell widths. | Use visibility: hidden for general elements. |
display: none; rips the element out of document flow; it takes up zero pixels of space and pushes nothing.visibility: hidden; renders the element invisible while preserving its exact width, height, and layout position.opacity: 0; makes an element completely transparent, preserves space, allows smooth fade transitions, and remains clickable by default.display: none; for toggling menus/modals, and visibility: hidden; when you need to avoid layout shifts.display: none;?visibility: hidden; differ from display: none;?display: none to display: block with a CSS transition?visibility: hidden; over display: none; for an error message label?hide-and-seek-lab.html.display: none vs visibility: hidden:In the next lesson, we will continue your web styling journey with CSS Overflow: Handling Content Spills with Visible, Hidden, Scroll, and Auto (Display and Visibility).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.