The main difference between display: none and visibility: hidden is layout impact and accessibility. 1. display: none removes elements completely from the page, and does not take up space, and screen readers usually ignore it; 2. visibility: hidden hides elements but retains their space, and may still be read by screen readers in some cases. When choosing, consider whether the layout structure needs to be preserved or whether access to content is allowed: the former is suitable for scenarios that completely hide and exclude interactions, such as inactive tabs, and the latter is suitable for situations where the layout is stable and may be redisplayed quickly, such as animation transitions. In addition, visibility: hidden can achieve animation effects through opacity, while display cannot transition. Neither of these affects JavaScript operations, but the child elements of visibility: hidden will be forced to be hidden.
When you're hiding elements on a webpage, two common CSS properties come into play: display: none
and visibility: hidden
. While they may seem similar at first glance, they actually behave quite differently in how they affect layout and accessibility.

How display: none
Works
Using display: none
completely removes an element from the page. It won't take up any space, and the browser acts as if it's not there at all.

- Layout impact : The element doesn't occur any space.
- Accessibility : Screen readers usually ignore elements with
display: none
. - Use cases : Great for conditionally removing elements entirely, like hiding tabs that aren't active.
This method is often used when you don't want an element to be part of the layout or interacted with at all.
What Happens with visibility: hidden
On the other hand, visibility: hidden
makes an element invisible but keeps its space in the layout.

- Layout impact : The element still takes up space.
- Accessibility : Some screen readers might still read the content depending on context.
- Use cases : Useful when you want to hide something visually but keep its position involve, such as preloading UI states.
For example, this can be handy for animations where an element will appear later without shifting surrounding content.
When to Choose One Over the Other
Your choice depends on what you need the hidden element to do:
-
Use
display: none
if:- You want the element completely out of sight and layout.
- You don't want it to be accessible via screen readers.
- You're toggling content that shouldn't affect surrounding elements.
-
Use
visibility: hidden
if:- You want to maintain layout structure.
- You plan to show the element again soon.
- You want the element to remain accessible in some contexts.
Also consider transitions — visibility
can be animated with opacity, while display
cannot.
A Few Subtle Gotchas
One thing people often miss is how these properties interact with JavaScript:
- Elements with
display: none
can still be targeted and manipulated via JS. - Setting
visibility: hidden
doesn't prevent click events if the element still exists in the DOM and has handlers attached.
Also, visibility: hidden
affects child elements too — they become hidden even if their own visibility is set to visible.
So depending on your use case, either approach could be better. Just remember that display: none
fully removes the element from rendering, while visibility: hidden
hides it but keeps its place.
Basically that's it.
The above is the detailed content of Comparing CSS `display: none` and `visibility: hidden`. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

In the following tutorial, I will show you how to create Lottie animations in Figma. We'll use two colorful designs to exmplify how you can animate in Figma, and then I'll show you how to go from Figma to Lottie animations. All you need is a free Fig

We put it to the test and it turns out Sass can replace JavaScript, at least when it comes to low-level logic and puzzle behavior. With nothing but maps, mixins, functions, and a whole lot of math, we managed to bring our Tangram puzzle to life, no J

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.
