Element.offsetHeight for Visibility

By  on  

One of the (perceived) tricky tasks within front-end coding is checking if an element is visible or not.  The very naive way of checking if an element is visible (i.e. has presence or takes up space on the page) is by checking its display style value:

var incorrectIsVisible = window.getComputedStyle(someElement, null).getPropertyValue('display'); // "inline", "inline-block", "block", etc.

Notice I'm not checking the opacity as well because an invisible element still takes up space on the screen.  The problem with the code above is that you can gain the style of a child but that may not matter if its parent is set to display: none.  For example, if the child's display style value is inline-block, but the element's parent display style is none, the child element is still not visible.  Oddly enough, checking the child element's offsetHeight value will signal if the element is likely visible:

var correctIsVisible = someElement.offsetHeight; // 0 for hidden, more than 0 for displaying

If the element is a child of an element which is display: none, the offsetHeight will be 0 and thus you know the element is not visible despite its display value. Again, remember that opacity is not considered and an element which is opacity: 0 is still technically visible, taking up space.

Recent Features

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    Pure CSS Slide Up and Slide Down

    If I can avoid using JavaScript for element animations, I'm incredibly happy and driven to do so.  They're more efficient, don't require a JavaScript framework to manage steps, and they're more elegant.  One effect that is difficult to nail down with pure CSS is sliding up...

  • By
    Fancy FAQs with jQuery Sliders

    Frequently asked questions can be super boring, right? They don't have to be! I've already shown you how to create fancy FAQs with MooTools -- here's how to create the same effect using jQuery. The HTML Simply a series of H3s and DIVs wrapper...

Discussion

  1. René

    Unfortunately there are a lot of caveats to this.
    First of all, it’s not just opacity that will render an element invisible but still taking up space.(filters and visibility:hidden)

    Another that comes to mind right away is checking elements that have no padding or border and have just floating/absolute/fixed children.

    Also, in some cases you could set an element’s height to 0 and having overflowing children (don’t have a use case popping up though)

    Solution; somebody else probably figured that out. My first guess would be either setting height to 1px for the test or bubbling up and use the display none check..

  2. Fabrizio

    What about checking for El.clientwidth === 0

  3. Fabrizio

    and are there other way to check is an element is visible without triggering a reflow/repaint?

  4. Christoph

    The best bet seems to me to check Element.offsetParent. If it returns null, the element is not visible.

  5. Javier

    What about SVG elements? They don’t have offsetHeight property, but they can be set to display:none;

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!