Read scrollHeight of div with a display:none parent





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







12















With a div element in a parent div that is hidden with display:none.




  • I'm dumping the jQuery textarea element to the console. I see that the scrollHeight property of the 0th element is 88.

  • I try to read this property to a var (using $(element)[0].scrollHeight or $(element).prop('scrollHeight') and I'm getting 0.


I also tried to set the textarea to position: absolute and display: block with jQuery, before the read, with the same result.



How can I read the property correctly?










share|improve this question




















  • 2





    you should share your code, I tried in Chrome and FF this: console.log($("textarea")); and scrollHeight = 0

    – Alex Angelico
    Jan 3 at 23:27




















12















With a div element in a parent div that is hidden with display:none.




  • I'm dumping the jQuery textarea element to the console. I see that the scrollHeight property of the 0th element is 88.

  • I try to read this property to a var (using $(element)[0].scrollHeight or $(element).prop('scrollHeight') and I'm getting 0.


I also tried to set the textarea to position: absolute and display: block with jQuery, before the read, with the same result.



How can I read the property correctly?










share|improve this question




















  • 2





    you should share your code, I tried in Chrome and FF this: console.log($("textarea")); and scrollHeight = 0

    – Alex Angelico
    Jan 3 at 23:27
















12












12








12


2






With a div element in a parent div that is hidden with display:none.




  • I'm dumping the jQuery textarea element to the console. I see that the scrollHeight property of the 0th element is 88.

  • I try to read this property to a var (using $(element)[0].scrollHeight or $(element).prop('scrollHeight') and I'm getting 0.


I also tried to set the textarea to position: absolute and display: block with jQuery, before the read, with the same result.



How can I read the property correctly?










share|improve this question
















With a div element in a parent div that is hidden with display:none.




  • I'm dumping the jQuery textarea element to the console. I see that the scrollHeight property of the 0th element is 88.

  • I try to read this property to a var (using $(element)[0].scrollHeight or $(element).prop('scrollHeight') and I'm getting 0.


I also tried to set the textarea to position: absolute and display: block with jQuery, before the read, with the same result.



How can I read the property correctly?







jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 9 '13 at 9:09









Rory McCrossan

250k29217254




250k29217254










asked Jan 9 '13 at 8:57









RolandGRolandG

4193719




4193719








  • 2





    you should share your code, I tried in Chrome and FF this: console.log($("textarea")); and scrollHeight = 0

    – Alex Angelico
    Jan 3 at 23:27
















  • 2





    you should share your code, I tried in Chrome and FF this: console.log($("textarea")); and scrollHeight = 0

    – Alex Angelico
    Jan 3 at 23:27










2




2





you should share your code, I tried in Chrome and FF this: console.log($("textarea")); and scrollHeight = 0

– Alex Angelico
Jan 3 at 23:27







you should share your code, I tried in Chrome and FF this: console.log($("textarea")); and scrollHeight = 0

– Alex Angelico
Jan 3 at 23:27














4 Answers
4






active

oldest

votes


















4














What is the size of an element when display:none? 0px by 0px



The element hasn't been rendered or painted anywhere on the screen, and so it truly occupies zero pixels worth of space. The question of if it was visible, how much space it would consume is tricky to answer, because answering it accurately means turning the hypothetical into a reality. We would need to actually render it to get a reliable measure of its size.



Generally speaking, this involves either:





  1. Toggling - Quickly making the element visible, checking the height, and restore back



    When toggling, you might be able to make visible and undo in between paint cycles, but there's no guarantee. Especially if the element you're looking to measure is deeply nested inside a hidden parent.




  2. Cloning - Emulate the element's properties as best you can & rendering off screen



    When cloning, an element's dimensions are based on a large number of other elements and properties surrounding it. Depending on the complexity of your page, there's also no guarantee that creating an element offscreen will yield the same exact dimensions that the original would.




There are several solutions laid out in the following questions, but most follow that basic pattern:




  • Need to find height of hidden div set to display:none

  • Get the size of an element when inside a display:none parent

  • Check if element is visible in DOM


There's also a library called jQuery.Actual (2.4KB) which abstracts away some of that work:



Here's their own Demo Page and also a Stack Snippet:






console.log("width: ", $("#inner").actual("width"))
console.log("height: ", $("#inner").actual("height"))

#outer, #inner {
margin: 10px;
padding: 10px;
}
#outer {
background: #d1e3ef;
}
#inner {
background: #9ebae9;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

<div id="outer" style="display:none">
<code>#outer</code>
<div id="inner">
<code>#inner</code>
</div>
</div>





Example






share|improve this answer





















  • 4





    don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

    – Alex Angelico
    Jan 3 at 23:32






  • 2





    @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

    – KyleMit
    Jan 4 at 0:45





















2














You need to set dispaly:block property for parent div. And after getting scrollheight by using $(element)[0].scrollHeight you can set display:none to main div.



So your code will be



$('#mainDiv').show();
// Dump html to div
// Read height
var heightOfDiv = $('#mainDiv')[0].scrollHeight
// Hide the div
$('#mainDiv').hide();





share|improve this answer
























  • I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

    – John Fantastico
    Jan 8 at 14:06



















1














An element with display: none not only will not be displayed, but also will not be processed by the DOM tree. That's why the scrollHeight will be 0.



Otherwise, with visibility: hidden, the element won't be visible, however it will have his space reserved in the DOM. So it will have a scrollHeight different to 0.



Then, to read the property and keep the element hidden you could use $('#elementId').prop('visibility','hidden'); $('#elementId').prop('scrollHeight').






share|improve this answer
























  • Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

    – surajck
    Jan 10 at 14:39













  • display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

    – Byron Rodríguez
    Jan 10 at 16:11



















1















No it is unfortunately not possible.




I suggest you read these links: MDN | Display or Article from nomensa , that evoke the notion of DOM accessibility and visibility.
In fact, if you put an element to "display: none", it will not be visible and will exit the DOM accessibility tree.
However, there are some techniques that will allow you to make an DOM element completely invisible and no longer considered to exist on the DOM.



Then there are obviously techniques that you should surely know



The first if you want to completely mask your parent div without worrying about its dimensions, I propose the code below. The clip property is depreciating but without it, it's still going to hide our item.






console.log(document.getElementById('child').scrollHeight);

.hidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
white-space: nowrap;
width: 1px;
z-index: -1;
}

<div class="hidden parent" href="#main">
I am ignored by the DOM
<div id="child">I am child</div>
</div>
<span style="color:red">I am the N°1</span>





Otherwise, if you want to keep the dimensions of your parent div, you can float it out of the DOM stream.






console.log(document.getElementById('child').scrollHeight);

.hidden {
height: auto;
width: auto;
visibility: hidden;
position: absolute;
z-index: -1;
}

<div class="hidden parent" href="#main">
I am ignored by the DOM
<div id="child">I am child</div>
</div>
<span style="color:red">I am the N°1</span>





I hope this answer will be helpful.






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14231481%2fread-scrollheight-of-div-with-a-displaynone-parent%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    What is the size of an element when display:none? 0px by 0px



    The element hasn't been rendered or painted anywhere on the screen, and so it truly occupies zero pixels worth of space. The question of if it was visible, how much space it would consume is tricky to answer, because answering it accurately means turning the hypothetical into a reality. We would need to actually render it to get a reliable measure of its size.



    Generally speaking, this involves either:





    1. Toggling - Quickly making the element visible, checking the height, and restore back



      When toggling, you might be able to make visible and undo in between paint cycles, but there's no guarantee. Especially if the element you're looking to measure is deeply nested inside a hidden parent.




    2. Cloning - Emulate the element's properties as best you can & rendering off screen



      When cloning, an element's dimensions are based on a large number of other elements and properties surrounding it. Depending on the complexity of your page, there's also no guarantee that creating an element offscreen will yield the same exact dimensions that the original would.




    There are several solutions laid out in the following questions, but most follow that basic pattern:




    • Need to find height of hidden div set to display:none

    • Get the size of an element when inside a display:none parent

    • Check if element is visible in DOM


    There's also a library called jQuery.Actual (2.4KB) which abstracts away some of that work:



    Here's their own Demo Page and also a Stack Snippet:






    console.log("width: ", $("#inner").actual("width"))
    console.log("height: ", $("#inner").actual("height"))

    #outer, #inner {
    margin: 10px;
    padding: 10px;
    }
    #outer {
    background: #d1e3ef;
    }
    #inner {
    background: #9ebae9;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

    <div id="outer" style="display:none">
    <code>#outer</code>
    <div id="inner">
    <code>#inner</code>
    </div>
    </div>





    Example






    share|improve this answer





















    • 4





      don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

      – Alex Angelico
      Jan 3 at 23:32






    • 2





      @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

      – KyleMit
      Jan 4 at 0:45


















    4














    What is the size of an element when display:none? 0px by 0px



    The element hasn't been rendered or painted anywhere on the screen, and so it truly occupies zero pixels worth of space. The question of if it was visible, how much space it would consume is tricky to answer, because answering it accurately means turning the hypothetical into a reality. We would need to actually render it to get a reliable measure of its size.



    Generally speaking, this involves either:





    1. Toggling - Quickly making the element visible, checking the height, and restore back



      When toggling, you might be able to make visible and undo in between paint cycles, but there's no guarantee. Especially if the element you're looking to measure is deeply nested inside a hidden parent.




    2. Cloning - Emulate the element's properties as best you can & rendering off screen



      When cloning, an element's dimensions are based on a large number of other elements and properties surrounding it. Depending on the complexity of your page, there's also no guarantee that creating an element offscreen will yield the same exact dimensions that the original would.




    There are several solutions laid out in the following questions, but most follow that basic pattern:




    • Need to find height of hidden div set to display:none

    • Get the size of an element when inside a display:none parent

    • Check if element is visible in DOM


    There's also a library called jQuery.Actual (2.4KB) which abstracts away some of that work:



    Here's their own Demo Page and also a Stack Snippet:






    console.log("width: ", $("#inner").actual("width"))
    console.log("height: ", $("#inner").actual("height"))

    #outer, #inner {
    margin: 10px;
    padding: 10px;
    }
    #outer {
    background: #d1e3ef;
    }
    #inner {
    background: #9ebae9;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

    <div id="outer" style="display:none">
    <code>#outer</code>
    <div id="inner">
    <code>#inner</code>
    </div>
    </div>





    Example






    share|improve this answer





















    • 4





      don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

      – Alex Angelico
      Jan 3 at 23:32






    • 2





      @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

      – KyleMit
      Jan 4 at 0:45
















    4












    4








    4







    What is the size of an element when display:none? 0px by 0px



    The element hasn't been rendered or painted anywhere on the screen, and so it truly occupies zero pixels worth of space. The question of if it was visible, how much space it would consume is tricky to answer, because answering it accurately means turning the hypothetical into a reality. We would need to actually render it to get a reliable measure of its size.



    Generally speaking, this involves either:





    1. Toggling - Quickly making the element visible, checking the height, and restore back



      When toggling, you might be able to make visible and undo in between paint cycles, but there's no guarantee. Especially if the element you're looking to measure is deeply nested inside a hidden parent.




    2. Cloning - Emulate the element's properties as best you can & rendering off screen



      When cloning, an element's dimensions are based on a large number of other elements and properties surrounding it. Depending on the complexity of your page, there's also no guarantee that creating an element offscreen will yield the same exact dimensions that the original would.




    There are several solutions laid out in the following questions, but most follow that basic pattern:




    • Need to find height of hidden div set to display:none

    • Get the size of an element when inside a display:none parent

    • Check if element is visible in DOM


    There's also a library called jQuery.Actual (2.4KB) which abstracts away some of that work:



    Here's their own Demo Page and also a Stack Snippet:






    console.log("width: ", $("#inner").actual("width"))
    console.log("height: ", $("#inner").actual("height"))

    #outer, #inner {
    margin: 10px;
    padding: 10px;
    }
    #outer {
    background: #d1e3ef;
    }
    #inner {
    background: #9ebae9;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

    <div id="outer" style="display:none">
    <code>#outer</code>
    <div id="inner">
    <code>#inner</code>
    </div>
    </div>





    Example






    share|improve this answer















    What is the size of an element when display:none? 0px by 0px



    The element hasn't been rendered or painted anywhere on the screen, and so it truly occupies zero pixels worth of space. The question of if it was visible, how much space it would consume is tricky to answer, because answering it accurately means turning the hypothetical into a reality. We would need to actually render it to get a reliable measure of its size.



    Generally speaking, this involves either:





    1. Toggling - Quickly making the element visible, checking the height, and restore back



      When toggling, you might be able to make visible and undo in between paint cycles, but there's no guarantee. Especially if the element you're looking to measure is deeply nested inside a hidden parent.




    2. Cloning - Emulate the element's properties as best you can & rendering off screen



      When cloning, an element's dimensions are based on a large number of other elements and properties surrounding it. Depending on the complexity of your page, there's also no guarantee that creating an element offscreen will yield the same exact dimensions that the original would.




    There are several solutions laid out in the following questions, but most follow that basic pattern:




    • Need to find height of hidden div set to display:none

    • Get the size of an element when inside a display:none parent

    • Check if element is visible in DOM


    There's also a library called jQuery.Actual (2.4KB) which abstracts away some of that work:



    Here's their own Demo Page and also a Stack Snippet:






    console.log("width: ", $("#inner").actual("width"))
    console.log("height: ", $("#inner").actual("height"))

    #outer, #inner {
    margin: 10px;
    padding: 10px;
    }
    #outer {
    background: #d1e3ef;
    }
    #inner {
    background: #9ebae9;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

    <div id="outer" style="display:none">
    <code>#outer</code>
    <div id="inner">
    <code>#inner</code>
    </div>
    </div>





    Example






    console.log("width: ", $("#inner").actual("width"))
    console.log("height: ", $("#inner").actual("height"))

    #outer, #inner {
    margin: 10px;
    padding: 10px;
    }
    #outer {
    background: #d1e3ef;
    }
    #inner {
    background: #9ebae9;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

    <div id="outer" style="display:none">
    <code>#outer</code>
    <div id="inner">
    <code>#inner</code>
    </div>
    </div>





    console.log("width: ", $("#inner").actual("width"))
    console.log("height: ", $("#inner").actual("height"))

    #outer, #inner {
    margin: 10px;
    padding: 10px;
    }
    #outer {
    background: #d1e3ef;
    }
    #inner {
    background: #9ebae9;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.actual/1.0.19/jquery.actual.min.js"></script>

    <div id="outer" style="display:none">
    <code>#outer</code>
    <div id="inner">
    <code>#inner</code>
    </div>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    answered Jan 3 at 23:17


























    community wiki





    KyleMit









    • 4





      don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

      – Alex Angelico
      Jan 3 at 23:32






    • 2





      @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

      – KyleMit
      Jan 4 at 0:45
















    • 4





      don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

      – Alex Angelico
      Jan 3 at 23:32






    • 2





      @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

      – KyleMit
      Jan 4 at 0:45










    4




    4





    don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

    – Alex Angelico
    Jan 3 at 23:32





    don't understand, it's a almost 6 years old question, you offer the bounty and you answer it?

    – Alex Angelico
    Jan 3 at 23:32




    2




    2





    @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

    – KyleMit
    Jan 4 at 0:45







    @AlexAngelico, offered a bounty because I was looking for an answer, and then kept searching around and thought I'd share what I pieced together. You can't really recall a bounty, but thought it was still valuable to update with info.

    – KyleMit
    Jan 4 at 0:45















    2














    You need to set dispaly:block property for parent div. And after getting scrollheight by using $(element)[0].scrollHeight you can set display:none to main div.



    So your code will be



    $('#mainDiv').show();
    // Dump html to div
    // Read height
    var heightOfDiv = $('#mainDiv')[0].scrollHeight
    // Hide the div
    $('#mainDiv').hide();





    share|improve this answer
























    • I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

      – John Fantastico
      Jan 8 at 14:06
















    2














    You need to set dispaly:block property for parent div. And after getting scrollheight by using $(element)[0].scrollHeight you can set display:none to main div.



    So your code will be



    $('#mainDiv').show();
    // Dump html to div
    // Read height
    var heightOfDiv = $('#mainDiv')[0].scrollHeight
    // Hide the div
    $('#mainDiv').hide();





    share|improve this answer
























    • I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

      – John Fantastico
      Jan 8 at 14:06














    2












    2








    2







    You need to set dispaly:block property for parent div. And after getting scrollheight by using $(element)[0].scrollHeight you can set display:none to main div.



    So your code will be



    $('#mainDiv').show();
    // Dump html to div
    // Read height
    var heightOfDiv = $('#mainDiv')[0].scrollHeight
    // Hide the div
    $('#mainDiv').hide();





    share|improve this answer













    You need to set dispaly:block property for parent div. And after getting scrollheight by using $(element)[0].scrollHeight you can set display:none to main div.



    So your code will be



    $('#mainDiv').show();
    // Dump html to div
    // Read height
    var heightOfDiv = $('#mainDiv')[0].scrollHeight
    // Hide the div
    $('#mainDiv').hide();






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 20 '13 at 11:59









    mujaffarsmujaffars

    8661030




    8661030













    • I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

      – John Fantastico
      Jan 8 at 14:06



















    • I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

      – John Fantastico
      Jan 8 at 14:06

















    I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

    – John Fantastico
    Jan 8 at 14:06





    I believe this works, and momentarily showing the control and almost instantaneously hiding it again is not going to be visible to the user. In fact the browse may not even actually try to update what the user sees until the function block completes, thus in effect the control was always hidden while getting you the measurement you need.

    – John Fantastico
    Jan 8 at 14:06











    1














    An element with display: none not only will not be displayed, but also will not be processed by the DOM tree. That's why the scrollHeight will be 0.



    Otherwise, with visibility: hidden, the element won't be visible, however it will have his space reserved in the DOM. So it will have a scrollHeight different to 0.



    Then, to read the property and keep the element hidden you could use $('#elementId').prop('visibility','hidden'); $('#elementId').prop('scrollHeight').






    share|improve this answer
























    • Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

      – surajck
      Jan 10 at 14:39













    • display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

      – Byron Rodríguez
      Jan 10 at 16:11
















    1














    An element with display: none not only will not be displayed, but also will not be processed by the DOM tree. That's why the scrollHeight will be 0.



    Otherwise, with visibility: hidden, the element won't be visible, however it will have his space reserved in the DOM. So it will have a scrollHeight different to 0.



    Then, to read the property and keep the element hidden you could use $('#elementId').prop('visibility','hidden'); $('#elementId').prop('scrollHeight').






    share|improve this answer
























    • Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

      – surajck
      Jan 10 at 14:39













    • display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

      – Byron Rodríguez
      Jan 10 at 16:11














    1












    1








    1







    An element with display: none not only will not be displayed, but also will not be processed by the DOM tree. That's why the scrollHeight will be 0.



    Otherwise, with visibility: hidden, the element won't be visible, however it will have his space reserved in the DOM. So it will have a scrollHeight different to 0.



    Then, to read the property and keep the element hidden you could use $('#elementId').prop('visibility','hidden'); $('#elementId').prop('scrollHeight').






    share|improve this answer













    An element with display: none not only will not be displayed, but also will not be processed by the DOM tree. That's why the scrollHeight will be 0.



    Otherwise, with visibility: hidden, the element won't be visible, however it will have his space reserved in the DOM. So it will have a scrollHeight different to 0.



    Then, to read the property and keep the element hidden you could use $('#elementId').prop('visibility','hidden'); $('#elementId').prop('scrollHeight').







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 8 at 23:42









    Byron RodríguezByron Rodríguez

    416




    416













    • Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

      – surajck
      Jan 10 at 14:39













    • display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

      – Byron Rodríguez
      Jan 10 at 16:11



















    • Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

      – surajck
      Jan 10 at 14:39













    • display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

      – Byron Rodríguez
      Jan 10 at 16:11

















    Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

    – surajck
    Jan 10 at 14:39







    Setting visibility: hidden; to an element that is already display: none; wont give you the value of scroll height, since display: none; would take preference, isn't it?

    – surajck
    Jan 10 at 14:39















    display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

    – Byron Rodríguez
    Jan 10 at 16:11





    display: none; takes precedence because it affects the DOM structure. visibility: hidden; does not affect the DOM structure, is like setting properties like color or opacity.

    – Byron Rodríguez
    Jan 10 at 16:11











    1















    No it is unfortunately not possible.




    I suggest you read these links: MDN | Display or Article from nomensa , that evoke the notion of DOM accessibility and visibility.
    In fact, if you put an element to "display: none", it will not be visible and will exit the DOM accessibility tree.
    However, there are some techniques that will allow you to make an DOM element completely invisible and no longer considered to exist on the DOM.



    Then there are obviously techniques that you should surely know



    The first if you want to completely mask your parent div without worrying about its dimensions, I propose the code below. The clip property is depreciating but without it, it's still going to hide our item.






    console.log(document.getElementById('child').scrollHeight);

    .hidden {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    white-space: nowrap;
    width: 1px;
    z-index: -1;
    }

    <div class="hidden parent" href="#main">
    I am ignored by the DOM
    <div id="child">I am child</div>
    </div>
    <span style="color:red">I am the N°1</span>





    Otherwise, if you want to keep the dimensions of your parent div, you can float it out of the DOM stream.






    console.log(document.getElementById('child').scrollHeight);

    .hidden {
    height: auto;
    width: auto;
    visibility: hidden;
    position: absolute;
    z-index: -1;
    }

    <div class="hidden parent" href="#main">
    I am ignored by the DOM
    <div id="child">I am child</div>
    </div>
    <span style="color:red">I am the N°1</span>





    I hope this answer will be helpful.






    share|improve this answer






























      1















      No it is unfortunately not possible.




      I suggest you read these links: MDN | Display or Article from nomensa , that evoke the notion of DOM accessibility and visibility.
      In fact, if you put an element to "display: none", it will not be visible and will exit the DOM accessibility tree.
      However, there are some techniques that will allow you to make an DOM element completely invisible and no longer considered to exist on the DOM.



      Then there are obviously techniques that you should surely know



      The first if you want to completely mask your parent div without worrying about its dimensions, I propose the code below. The clip property is depreciating but without it, it's still going to hide our item.






      console.log(document.getElementById('child').scrollHeight);

      .hidden {
      border: 0;
      clip: rect(0 0 0 0);
      height: 1px;
      margin: -1px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      white-space: nowrap;
      width: 1px;
      z-index: -1;
      }

      <div class="hidden parent" href="#main">
      I am ignored by the DOM
      <div id="child">I am child</div>
      </div>
      <span style="color:red">I am the N°1</span>





      Otherwise, if you want to keep the dimensions of your parent div, you can float it out of the DOM stream.






      console.log(document.getElementById('child').scrollHeight);

      .hidden {
      height: auto;
      width: auto;
      visibility: hidden;
      position: absolute;
      z-index: -1;
      }

      <div class="hidden parent" href="#main">
      I am ignored by the DOM
      <div id="child">I am child</div>
      </div>
      <span style="color:red">I am the N°1</span>





      I hope this answer will be helpful.






      share|improve this answer




























        1












        1








        1








        No it is unfortunately not possible.




        I suggest you read these links: MDN | Display or Article from nomensa , that evoke the notion of DOM accessibility and visibility.
        In fact, if you put an element to "display: none", it will not be visible and will exit the DOM accessibility tree.
        However, there are some techniques that will allow you to make an DOM element completely invisible and no longer considered to exist on the DOM.



        Then there are obviously techniques that you should surely know



        The first if you want to completely mask your parent div without worrying about its dimensions, I propose the code below. The clip property is depreciating but without it, it's still going to hide our item.






        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        border: 0;
        clip: rect(0 0 0 0);
        height: 1px;
        margin: -1px;
        overflow: hidden;
        padding: 0;
        position: absolute;
        white-space: nowrap;
        width: 1px;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        Otherwise, if you want to keep the dimensions of your parent div, you can float it out of the DOM stream.






        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        height: auto;
        width: auto;
        visibility: hidden;
        position: absolute;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        I hope this answer will be helpful.






        share|improve this answer
















        No it is unfortunately not possible.




        I suggest you read these links: MDN | Display or Article from nomensa , that evoke the notion of DOM accessibility and visibility.
        In fact, if you put an element to "display: none", it will not be visible and will exit the DOM accessibility tree.
        However, there are some techniques that will allow you to make an DOM element completely invisible and no longer considered to exist on the DOM.



        Then there are obviously techniques that you should surely know



        The first if you want to completely mask your parent div without worrying about its dimensions, I propose the code below. The clip property is depreciating but without it, it's still going to hide our item.






        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        border: 0;
        clip: rect(0 0 0 0);
        height: 1px;
        margin: -1px;
        overflow: hidden;
        padding: 0;
        position: absolute;
        white-space: nowrap;
        width: 1px;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        Otherwise, if you want to keep the dimensions of your parent div, you can float it out of the DOM stream.






        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        height: auto;
        width: auto;
        visibility: hidden;
        position: absolute;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        I hope this answer will be helpful.






        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        border: 0;
        clip: rect(0 0 0 0);
        height: 1px;
        margin: -1px;
        overflow: hidden;
        padding: 0;
        position: absolute;
        white-space: nowrap;
        width: 1px;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        border: 0;
        clip: rect(0 0 0 0);
        height: 1px;
        margin: -1px;
        overflow: hidden;
        padding: 0;
        position: absolute;
        white-space: nowrap;
        width: 1px;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        height: auto;
        width: auto;
        visibility: hidden;
        position: absolute;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>





        console.log(document.getElementById('child').scrollHeight);

        .hidden {
        height: auto;
        width: auto;
        visibility: hidden;
        position: absolute;
        z-index: -1;
        }

        <div class="hidden parent" href="#main">
        I am ignored by the DOM
        <div id="child">I am child</div>
        </div>
        <span style="color:red">I am the N°1</span>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 10 at 1:04

























        answered Jan 3 at 23:44









        soumaresoumare

        32527




        32527






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14231481%2fread-scrollheight-of-div-with-a-displaynone-parent%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'