How to Check if an Element is Visible in DOM? Last Updated : 11 Oct, 2024 Comments Improve Suggest changes Like Article Like Report To check if an element is visible in the DOM, the process involves determining its presence and whether it is displayed on the page.Below are the approaches to check if an element is visible in DOM or not: Table of ContentUsing offsetHeight and offsetWidthUsing getComputedStyle() methodApproach 1: Using offsetHeight and offsetWidthThe offsetHeight property retrieves the height of an element, including vertical padding and borders, and returns an integer in pixels.The offsetWidth property obtains the width of an element, including horizontal padding and borders, and returns an integer in pixels.The getClientRects() method provides a collection of bounding rectangles for the element, returning a list of DOMRect objects, with a length of 0 indicating no rectangles found.Visibility is assessed by combining the results from these properties; a true value indicates the element is visible, while a false value signifies it is invisible.Example: This example shows the implementation of the above-mentioned approach. 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: Approach 2: Using getComputedStyle() methodThe getComputedStyle() method returns an object containing all the CSS properties of an element.Each property can be checked for specific values as needed.The display property specifies how an element is displayed.A value of 'none' for the display property completely hides the element from the page.Checking the display property against 'none' indicates visibility; a true return value means the element is invisible, while a false value signifies it is visible.Example: This example shows the implementation of the above-mentioned appraoch. 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: Comment More infoAdvertise with us Next Article How to Check if an Element is Visible in DOM? S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies HTML-DOM HTML-Misc Similar Reads How to check an element with ng-if is visible on DOM ? ng-if directive: The ng-if directive in AngularJS is used to remove or recreate a portion of the HTML element based on an expression. If the expression inside it is false then the element is removed and if it is true then the element is added to the DOM.Syntax:<element ng-if="expression"> Cont 2 min read How to Check a DOM Element is Visible in Current Viewport ? In this article, we will see how to find DOM element is visible in the current viewport or not. To determine if a DOM element is visible in the current viewport, we can compare its position and dimensions using the getBoundingClientRect() function with the dimensions of the viewport and also we can 3 min read How to Check if element exists in the visible DOM in JavaScript ? This article will show you how to check whether an element exists in the visible DOM or not. For that purpose, there are several methods used but we're going to look at a few of them. Example 1: In this example, the element is searched by document.getElementById('Id') and !! operator is used before 2 min read How to use protractor to check if an element is visible ? Protractor is an end-to-end test framework developed for AngularJS applications, however, it also works for non-Angular JS applications. It runs tests against the application interacting with it as a real user would, running in a real browser. In this article, we are going to use Protractor to check 3 min read How to check if an element is hidden in jQuery? To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element. Syntax:$(element).is(":hidden");Example:html<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Jquery Hid 1 min read Like