I'm currently starting on an animation project. In the project I'll have more than 40000 div
s and animate them iteratively. If any of div
s are in passive state (i.e. it's not animating at least for 2 seconds), I won't display them to increase animation performance.
The question is: which css property is the most suitable for this?
.passive1{ display:none } .passive2{ visibility:hidden; } .passive3{ opacity:0; }
And how can I measure rendering performance like fps, gpu usage?
The visibility: hidden style behaves like a combination of opacity: 0 and pointer-events: none . Regarding the accessibility, opacity: 0 is the only property which makes the element accessible in the tab order, and the element's content can be read by screen readers.
opacity:0 will still allow click, hover, and other mouse events on the element. It just won't be visible to the user. display:none does what it implies—the element still exists but is completely not visible, as though it's width and height are 0.
display:none will hide the whole element and remove that from layout space whereas visibility:hidden hides an element but take up the same space as before. Opacity can be used if you want to create transparency or fade effect.
display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). ... visibility:hidden means that unlike display:none , the tag is not visible, but space is allocated for it on the page. The tag is rendered, it just isn't seen on the page.
While all 3 properties make an element's box seem invisible, there are crucial differences between them:
Property | Painted | In layout | Stacking context | Pointer events | Keyboard events |
---|---|---|---|---|---|
opacity: 0; | No | Yes | New | Yes | Yes |
visibility: hidden; | No | Yes | Varies | No | No |
display: none; | No | No | Varies | No | No |
background-image
), #text
content, and so on. display: none;
, as with opacity: 0;
and visibility: hidden;
the browser will still determine the size of the element so it can correctly layout other elements relative to the current element (e.g. if you have span.hidden { visibility: hidden; display: inline; }
).opacity
(except opacity: 1.0;
) will create a new stacking-context, which complicates use of the position
property.visibility: hidden;
then the :hover
state won't work, and clicking the same element won't apply :focus
or :active
either.visibility: hidden;
won't raise mouseclick
, touchstart
, etc - note that the click
event can still be raised by certain elements, like <button>
if invoked by the user using a non-pointer input method, such as with keyboard or voice (accessible) navigation means. pointer-events: none;
to block pointer events, but this won't block keyboard and other non-pointer input and so should not be used to disable an element because the user can still use the keyboard to interact with it (especially <button>
, <input />
, <select>
, and <textarea>
).<form>
elements (as this uses tabindex
).pointer-events: none;
, there is no CSS property to disable keyboard interaction.This table shows a more complete comparison between the main values of those 3 properties:
Property | Painted | In layout | Stacking context | Pointer events | Keyboard events | Animatable |
---|---|---|---|---|---|---|
Opacity | ||||||
opacity: 0; | No | Yes | New | Yes | Yes | Yes |
opacity: 0.1; | Yes | Yes | New | Yes | Yes | Yes |
opacity: 0.9; | Yes | Yes | New | Yes | Yes | Yes |
opacity: 1; | Yes | Yes | Varies | Yes | Yes | Yes |
Visibility | ||||||
visibility: hidden; | No | Yes | Varies | No | No | Yes, with caveats |
visibility: visible; | Yes | Yes | Varies | Yes | Yes | Yes, with caveats |
Display | ||||||
display: none; | No | No | Varies | No | No | No |
display: contents; | Text and children only | Text and children only | Varies | Yes | Yes | No |
Other | ||||||
pointer-events: none; | N/A | N/A | N/A | No | Yes | No |
The "Animatable" column indicates if that property can be used with a CSS transition (transition:
) or CSS animation (@keyframes
).
display:
property cannot be animated, which is why we can't use a @keyframes
timeline to completely hide an element after the animation is complete. visibility:
property despite being non-continuous, albeit with caveats.Also, don't get confused by the similarly-named backface-visibility
and content-visibility
properties.
backface-visibility
is only applicable to 3D transform
operations.content-visibility
is an optimization to speed-up page rendering during initial page-load, but requires CSS Containment first, which is out-of-scope for this QA.The answer found here will answer your first question (most likely display:none
as the space is collapsed completely).
To your second question, tools such as this will probably be useful for you. However 40,000 divs sounds like way too many and you will probably have better performance using canvas or SVG (for example, using the KineticJS library as this handles animations - transformation, rotation, scale, etc.) for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With