0

Why getComputedStyle returns element visibility as visible even if you never set visibility to be hidden or visible.

Example: getComputedStyle($('#block1')[0],null).visibility; --- "visible"

and at the same time: $('#block1')[0].style.visibility --- ""

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
Rantiev
  • 2,121
  • 2
  • 32
  • 56
  • 7
    Because elements are visible by default? – Frédéric Hamidi Aug 20 '13 at 08:23
  • so when you will hide parent, it became to be hidden, but real element property is "" And yes it is visible by default. But i don't think it's related. – Rantiev Aug 20 '13 at 08:25
  • 4
    That's two different things. The `visibility` style rule is the empty string (inherit from parent), but `getComputedStyle()` actually computes the actual style value (in this case, `visible`). – Frédéric Hamidi Aug 20 '13 at 08:28
  • Hmmm, yes you are right, we need to get height and width counted automatically from any place too, i just thought that if property isn't set it should be "" in computed style as well. I was wrong. – Rantiev Aug 20 '13 at 08:33
  • Frédéric, put your answer as a real answer so Rantiev can accept it :) – David Mårtensson Aug 20 '13 at 08:39

1 Answers1

0

The default for most existing DOM elements that require rendering to the document is visibility: visible. These properties only apply themselves once the element is rendered and are computed without being applied to the style object of a DOM element. The getComputedStyle method is there to pick up these rendered properties which are either from the browser's default configuration or from the stylesheet.

marksyzm
  • 5,281
  • 2
  • 29
  • 27
  • Unrelated to this post; I think the information you provided [here](https://softwareengineering.stackexchange.com/review/suggested-edits/229477) is valuable but IMHO it should be posted as another answer. – Glorfindel Jan 07 '21 at 21:09
  • Thanks... I would do this but this guy covers it: https://softwareengineering.stackexchange.com/a/302656/190171 And the answer given is out of date and might be misread so I felt it was worth highlighting in the post itself? If that's not the way of stackoverflow then skip the edit – marksyzm Jan 08 '21 at 10:50
  • Ah, right. That's indeed some kind of flaw in the Stack Exchange system. It would help if the OP accepted the other answer. Or if we had a system where old votes 'fade away' - they would count towards reputation but not towards sorting answers by votes. – Glorfindel Jan 08 '21 at 10:53
  • 1
    I added a comment instead – marksyzm Jan 08 '21 at 11:35