Check Whether an Element Is Visible in the DOM

Last Updated : 10 Sep, 2026

Checking whether an element is visible in the DOM is useful when dynamically showing or hiding content, validating UI states, or performing actions based on an element's visibility.

Below are the approaches to check whether an element is visible in the DOM:

Approach 1: Using offsetWidth, offsetHeight, and getClientRects()

In this approach, we use the offsetWidth, offsetHeight, and getClientRects() properties to determine whether an element is rendered on the page.

If the element has dimensions or client rectangles, the function returns true; otherwise, it returns false.

Example: In this example, we check the visibility of a visible element and an element hidden using display: none.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        How to check if an element
        is visible in DOM?
    </title>
    
    <style>
        .visible {
            height: 100px;
            width: 100px;
            background-color: green;
        }
    
        .invisible {
            height: 100px;
            width: 100px;
            background-color: green;
            display: none;
        }
    </style>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    
    <b>
        How to check if element
        is visible in DOM?
    </b>
    
    <p>
        Below is a visible object
    </p>
    
    <div class="visible"></div>
    
    <p>
        Below is a invisible
        object
    </p>
    
    <div class="invisible"></div>
    
    <p>
        Click on the button to check for the
        visibility of the objects.
    </p>
    
    <p>Output for visible object: 
        <span class="outputVisible"></span>
    </p>
    
    <p>
        Output for non visible object: 
        <span class="outputInvisible"></span>
    </p>

    <button onclick="checkVisibility()">
        Click here
    </button>
    
    <script type="text/javascript">
        function isElementVisible(element) {
            if (element.offsetWidth || 
               element.offsetHeight || 
               element.getClientRects().length)
                return true;
            else
                return false;
        }
    
        function checkVisibility() {
            visibleObject = 
            document.querySelector(".visible");
            
            invisibleObject = 
            document.querySelector(".invisible");
    
            document.querySelector(".outputVisible").textContent
                    = isElementVisible(visibleObject);
                    
            document.querySelector(".outputInvisible").textContent
                        = isElementVisible(invisibleObject);
        }
    </script>
</body>

</html>

Output:

output

Approach 2: Using getComputedStyle() Method

In this approach, we use the getComputedStyle() method to retrieve the computed CSS properties of an element. We then check properties such as display and visibility to determine whether the element is visible.

An element with display: none or visibility: hidden is considered hidden.

Example: In this example, we use getComputedStyle() to check whether each element is visible.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        How to check if element
        is visible in DOM?
    </title>
    
    <style>
        .visible {
            height: 100px;
            width: 100px;
            background-color: green;
        }
    
        .invisible {
            height: 100px;
            width: 100px;
            background-color: green;
            display: none;
        }
    </style>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    
    <b>
        How to check if element
        is visible in DOM?
    </b>
    
    <p>Below is a visible object</p>
    <div class="visible"></div>
    
    <p>Below is a invisible object</p>
    <div class="invisible"></div>
    
    <p>
        Click on the button to check for the
        visibility of the objects.
    </p>
    
    <p>
        Output for visible object: 
        <span class="outputVisible"></span>
    </p>
    
    <p>
        Output for non visible object: 
        <span class="outputInvisible"></span>
    </p>

    <button onclick="checkVisibility()">
        Click here
    </button>
    
    <script type="text/javascript">
        function isElementVisible(element) {
            let style = window.getComputedStyle(element);
            
            if (style.display == 'none') 
                return false;
            else
                return true;
        }
    
        function checkVisibility() {
            visibleObject = 
                document.querySelector(".visible");

            invisibleObject = 
                document.querySelector(".invisible");
    
            document.querySelector(".outputVisible").textContent
                    = isElementVisible(visibleObject);
            
            document.querySelector(".outputInvisible").textContent
                    = isElementVisible(invisibleObject);
        }
    </script>
</body>

</html>

Output:

output
Comment