Element: checkVisibility() method

Baseline 2024
Newly available

Since March 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The checkVisibility() method of the Element interface checks whether the element is visible.

The method returns false in either of the following situations:

  • The element doesn't have an associated box, for example because the CSS display property is set to none or contents.
  • The element is not being rendered because the element or an ancestor element sets the content-visibility property to hidden.

The optional parameter enables additional checks to test for other interpretations of what "visible" means. For example, you can further check whether an element has an opacity of 0, if the value of the element visibility property makes it invisible, or if the element content-visibility property has a value of auto and its rendering is currently being skipped.

Syntax

js
checkVisibility(options)

Parameters

options Optional

An object indicating additional checks to run. The possible options are:

contentVisibilityAuto

true to check if the element content-visibility property has (or inherits) the value auto, and it is currently skipping its rendering. false by default.

opacityProperty

true to check if the element opacity property has (or inherits) a value of 0. false by default.

visibilityProperty

true to check if the element is invisible due to the value of its visibility property. false by default.

Note: Invisible elements include those that have visibility: hidden, and some element types that have visibility: collapse.

checkOpacity

A historic alias for opacityProperty.

checkVisibilityCSS

A historic alias for visibilityProperty.

Return value

false if any of the following conditions are met, otherwise true:

  • The element doesn't have an associated box.
  • The element content-visibility property has (or inherits) a value of hidden.
  • opacityProperty (or checkOpacity) is true and the element opacity property has (or inherits) a value of 0.
  • visibilityProperty (or checkVisibilityCSS) is true and the element is invisible due to the value of its visibility property.
  • contentVisibilityAuto is true, the content-visibility property has (or inherits) a value of auto, and element rendering is being skipped.

Examples

Test checkVisibility() with varied CSS

The following example allows you to test how the result of checkVisibility() might change with different values for display, content-visibility, visibility, and opacity CSS properties.

HTML

The HTML defines a

Next we have a

 that is used to output the result of the checkVisibility() check when no options are passed in the parameter, and for each separate option value.
At the end we have the element that will be tested (to which we will apply the selected CSS property values).

html

The element to be checked for visibility.

CSS

The CSS just highlights the element to be tested.

css
#test_element {
  border: solid;
  border-color: blue;
}

JavaScript

The code below gets each of the