0

How can I get the size of the bounding box of an element with pure JS? And by bounding box I mean the size of the square that will be created if I surround all the visible elements in that element.

offsetWidth/offsetHeight and scrollWidth/scrollHeight won't do because the size of that square depends on the overflow value of the element and they don't.

Example with and without overflow:hidden:

var first = document.getElementsByClassName('first')[0];
var second = document.getElementsByClassName('second')[0];

console.log(first.offsetHeight)
console.log(first.scrollHeight)
console.log(first.getBoundingClientRect())

console.log(second.offsetHeight)
console.log(second.scrollHeight)
console.log(second.getBoundingClientRect())
.out {
  outline: 1px solid red;
}
.in {
  position: relative;
  width: 50px;
  height: 50px;
  background: red;
  top: 150px;
}
.second {
  overflow: hidden;
  outline: 1px solid blue;
}
<div class="first out">
  <div class="in">
  </div>
</div>
<div class="second out">
  <div class="in">
  </div>
</div>

.in in the .first element is visible, so the .first bounding box height should include it ( it should be 200, which is actually the scrollHeight ). But, .in in the .second element, is not visible, so the .second bounding box height shouldn't include it ( it should be 50, which is actually the offsetHeight)

So, neither offsetHeight nor scrollHeight can give you the bounding box height, unless you know the overflow value of the element.

xpy
  • 5,481
  • 3
  • 29
  • 48
  • 1
    does `.getBoundingClientRect` give you what you need? ... or `.getClientRects` – Jaromanda X Feb 09 '16 at 12:03
  • 1
    "*..Although the red and blue outlines are different in height...*" -- how? – Abhitalks Feb 09 '16 at 12:03
  • @Abhitalks What do you mean _"how"_? – xpy Feb 09 '16 at 12:06
  • something is not quite right with the question i agree with Abhitalks, also why overflow matter, there is no outer box for "second". – Vitaliy Terziev Feb 09 '16 at 12:07
  • @JaromandaX No, they won't do, if you run the snippet and look at the console you will see they produce the same results. – xpy Feb 09 '16 at 12:07
  • sorry, I didn't even see you already tried those – Jaromanda X Feb 09 '16 at 12:08
  • @vtz There is a red outline around the `.first` and a blue around `.second` both have the same content but the `.second` has `overflow:hidden` thus it's outline is smaller than the `.first` – xpy Feb 09 '16 at 12:09
  • @xpy: By "how" I mean, how do you think they are of different heights? They are the same height i.e. 50px. The size will change only if you have an overflow `auto` or `scroll`, which you don't. Still, the question is very unclear as it stands. What is it that you are having a problem with? What are you trying to solve? – Abhitalks Feb 09 '16 at 12:12
  • 1
    From MDN: "Outlines differ from borders in the following ways: Outlines do not take up space, they are drawn above the content. ..." - Which would explain why they don't add to their size. https://developer.mozilla.org/en/docs/Web/CSS/outline – somethinghere Feb 09 '16 at 12:14
  • @Abhitalks My bad, I'm looking it in Firefox, obviously you are looking it in chrome, didn't expect that big difference between the two. – xpy Feb 09 '16 at 12:15
  • 1
    How is Firefox and Chrome different? I see the same console outputs in both. And as @somethinghere pointed out, outline has no effect on the size of the element. – epascarello Feb 09 '16 at 12:26
  • @epascarello I am not talking about how the outlines affect the size of an element, I am rephrasing my question right now. – xpy Feb 09 '16 at 12:27
  • You are wondering why Firefox draws the outline differently. I thought you were talke Basically they think that the outline should wrap all of the content if it is absolutely or relatively positioned. The other browsers do not believe that. This caused me to have to do some PITA code to get around the "non bug" in their eyes. https://bugzilla.mozilla.org/show_bug.cgi?id=687311 – epascarello Feb 09 '16 at 12:34
  • This is a known bug regarding outline in Gecko. See [here](http://stackoverflow.com/questions/10662902/css-outline-different-behavior-behavior-on-webkit-gecko) and [also here](https://bugzilla.mozilla.org/show_bug.cgi?id=687311) – Abhitalks Feb 09 '16 at 12:38
  • Wait why are you assuming that `overflow: hidden` actually hides things by default? Without a `width` or `height` set, it responds to elements in the same way as an `overflow: initial` would, by growing to contain them. – somethinghere Feb 09 '16 at 12:38
  • @epascarello Actually I am wondering how can I measure the `size of the visible content` inside an element – xpy Feb 09 '16 at 12:38
  • 1
    So what do you expect the numbers to be actually? – epascarello Feb 09 '16 at 12:39
  • @epascarello 200 for the `.first` and 50 for the `.second` – xpy Feb 09 '16 at 12:40
  • Problem is when you take things out of the flow, you have a different ballgame. There is no method for what you want to do. (At least I do not know of any) You would need to figure out the positions of the elements and calculate the bounding box. – epascarello Feb 09 '16 at 12:42
  • `[].forEach.call(first.children, function(child) { .. get the highest bottom from child.getBoundingClientRect ... })` – Abhitalks Feb 09 '16 at 12:45
  • @Abhitalks I thought of that too but I thing it should be recursive since the same rules will apply for the containing elements. – xpy Feb 09 '16 at 12:47

0 Answers0