1

I gave height of 0px to an element as shown below

#sample{
    height: 0px;
    overflow: hidden;
    transition-duration: 1.2s;
}

<div id="sample">
    <h1>Hello World main headinh</h1>
    <h2>Hello World sub heading</h2>
    <p>Hello world paragraph</p>
</div>

If I'd not specified height so it will take height of content (say 50px). Now initially I dont know the height of #sample. I want to set its height to its original calculated height (i.e. 50px), I can do it by giving height 100%.

document.getElementById("sample").style.height = "100%";

But transition-duration property is not working in that case, so when I change percent into pixel so transition-duration property worked well.

document.getElementById("sample").style.height = "50px";

But problem here is the content inside the div is dyanamic, so its height is changing too.

So how can I give height to the #sample in pixel as much as that content need. Solution should only have Javascript not any other framework like JQuery.

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65

7 Answers7

1

If you're wanting to animate the height, what you want to do is start out by setting max-height: 0, and then transition the max-height by changing a class and adding a transition or setting an animation, such that max-height: 500px or some value that's definitely bigger than the content inside will be.

EDIT: This might not be exactly what you're doing, but here is a decent example: https://davidwalsh.name/css-slide

thesublimeobject
  • 1,393
  • 1
  • 17
  • 22
  • That leaves the OP with the exact same problem that he has now. He's asking how to determine what the appropriate pixel value should be for the element when it is at full size. – Scott Marcus Feb 14 '17 at 15:57
  • The thing is, when you set `max-height: 0` instead of `height: 0`, and then change `max-height` to anything greater than you will need, the content will automatically fill to the height that it needs to take. You don't need to calculate the height using JS by doing it in this manner. – thesublimeobject Feb 14 '17 at 16:00
  • True, but if the content changes over time, your `max-height` value may become inadequate. It works, but it's a hack. The correct approach is to get the actual height of the content with `scrollHeight`. – Scott Marcus Feb 14 '17 at 16:02
  • I agree that is the _most_ correct approach, no doubt, it's just overkill for many situations where usually you, more or less, no max and min content sizes to some hypothetical degree. – thesublimeobject Feb 14 '17 at 16:11
  • I don't think it's accurate to say that "usually" you know "more or less" content sizes. Many sites/apps have dynamically generated content. While this approach does work, it wouldn't be categorized as "scalable". – Scott Marcus Feb 14 '17 at 16:21
1

try this.

var element = document.getElementById('sample');
element.style.height= element.scrollHeight+"px"

https://jsfiddle.net/y0g22540/

vijay
  • 633
  • 5
  • 11
1

Percentage heights will only work when the parent element has had its height set (you can't have a percentage of something that hasn't had its height set).

However, you can set the height back to its original value using scrollHeight.

// Get references to the elements:
var btn = document.getElementById("show");
var sample = document.getElementById("sample");

// Set up an event handler to trigger the code:
btn.addEventListener("click", function(){
  // Set the height to the height of the element's content:
  sample.style.height = sample.scrollHeight + "px";
  
  // Just for testing purposes:
  console.log(sample.style.height);
});
#sample{
    height: 0px;
    overflow: hidden;
    transition-duration: 1.2s;
}
<button id="show">Show</button>
<div id="sample">
    <h1>Hello World main headinh</h1>
    <h2>Hello World sub heading</h2>
    <p>Hello world paragraph</p>
</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

If you just want to show and hide shrinking or expanding the width and height then here it is and example that will point you in that direction!

#click:checked ~ #sample{  
   -webkit-transform: scale(1);
     -moz-transform: scale(1);
          transform: scale(1);   
}
#sample{
    overflow: hidden;
    -webkit-transition: transform 0.125s;
       -moz-transition: transform 0.125s;
            transition: transform 0.125s;
  
  -webkit-transform: scale(0);
     -moz-transform: scale(0);
          transform: scale(0);
}
<input type="checkbox" id="click"/>
<label for="click"> Click me </label>

<div id="sample">
    <h1>Hello World main headinh</h1>
    <h2>Hello World sub heading</h2>
    <p>Hello world paragraph</p>
</div>
Gacci
  • 1,388
  • 1
  • 11
  • 23
  • Nice. You could also use `scaleY()` to just work with the height, which is closer to what the OP id doing. – Scott Marcus Feb 14 '17 at 16:07
  • I am aware, however you are assuming he only needs to scale on the Y-Axis. I am giving a general example ... as I understand that it is required to show the minimum relevant code. But thanks for commenting! – Gacci Feb 14 '17 at 16:51
  • It's not an assumption. The OP is transforming the height property. – Scott Marcus Feb 14 '17 at 18:09
1

You could use a wrapper inside the div, and read its height.

document.getElementById('toggle').onclick = function() {

  const sample = document.getElementById("sample");
  const wrapper = sample.getElementsByClassName('wrapper');

  if (parseInt(sample.style.height) > 0) {
    sample.style.height = 0;

  } else {

    const style = window.getComputedStyle(wrapper[0], null);
    const height = style.getPropertyValue("height");


    sample.style.height = height;


  }



};
#sample {
  height: 0px;
  overflow: hidden;
  transition-duration: 1.2s;
}
<div id="sample">
  <div class="wrapper">
    <h1>Hello World main headinh</h1>
    <h2>Hello World sub heading</h2>
    <p>Hello world paragraph</p>
  </div>
</div>

<button id="toggle">Toggle</button>
dschu
  • 4,992
  • 5
  • 31
  • 48
0

I think what you want is

var s = document.getElementById('sample');
s.style.height = s.offsetHeight + 'px';

P.S. It should be noted that offsetHeight includes vertical padding and borders. And is rounded to an integer.
More info at: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight

  • I write this to check the calculated height. `var ht = document.getElementById("sample").offsetHeight; alert(ht);` But it is showing 0. – Siraj Alam Feb 14 '17 at 15:54
  • 1
    But, if the element starts off with a `height` of `0`, then `offsetHeight` will return `0`. – Scott Marcus Feb 14 '17 at 15:54
  • You'd first get the elment's height with `offsetHeight`, and store it in a variable. Then you'd set the `height: 0;`, and then you'd change the height to the value you stored before, if you wanted to transition the element's height to its content's height. – comesuccingfuccslot Feb 14 '17 at 15:57
  • @user6003859 But, the point is that you can't *"first get the elment's height with `offsetHeight`"* if the element has a height of 0 at first. – Scott Marcus Feb 14 '17 at 15:59
  • @user6003859 `.scrollHeight` is the solution. It's done!! – Siraj Alam Feb 14 '17 at 16:02
  • Uh.. if the element has `height: 0;`, then `offsetHeight` returns `0`.. `0` as in the height of the element. You could also just set the height of the element to `auto`, before getting the `offsetHeight`, to get the content's height, if that's what you wanted. – comesuccingfuccslot Feb 14 '17 at 16:02
  • Or, you could just use the method designed to do the job: `scrollHeight` and not have to use any hacks. – Scott Marcus Feb 14 '17 at 16:04
  • I wouldn't call this method a "hack". But you're right. This can be done with scrollHeight. – comesuccingfuccslot Feb 14 '17 at 16:06
  • It is a hack. It's code to work around a problem to get to a solution instead of using code specifically designed for solving a problem like this with no work around. – Scott Marcus Feb 14 '17 at 16:09
0

You can use JS to get the height and height of an html tag.

var example = document.getElementById("yourTag").offsetHeight;

Then you can do:

document.getElementById("sample").style.height = example;
Daniel
  • 95
  • 8