Animate change in size of div when content changes











up vote
1
down vote

favorite












First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.



Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.



It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:






    function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};

    .foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}

    <div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>





I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.










share|improve this question




























    up vote
    1
    down vote

    favorite












    First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.



    Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.



    It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:






        function rightClick() {
    fgContent = document.querySelector('#fgContent');
    rtButton = document.querySelector('#rightButton');
    fgDiv = document.querySelector('#fgDiv');
    spacing = fgDiv.clientHeight - fgContent.clientHeight;
    if (rtButton.textContent === "What's a Grimmage?") {
    nextButton = "How's it work?";
    nextDiv = document.querySelector('#whatIsIt');
    }
    // I removed a handful of similar if statements.
    fgDiv.maxHeight = fgDiv.clientHeight;
    fgDiv.minHeight = fgDiv.clientHeight;
    fgContent.innerHTML = nextDiv.innerHTML;
    fgDiv.maxHeight = nextDiv.clientHeight + spacing;
    console.log('Max height')
    fgContent.minHeight = 0;
    rtButton.textContent = nextButton;
    };

        .foreground {
    position: relative;
    z-index: 1;
    width: 33%;
    margin-top: 10%;
    margin-left: 10%;
    padding: 10px;
    color: black;
    background: whitesmoke;
    height: auto;
    opacity: .85;
    border-radius: 10px;
    -moz-transition: height 1s ease;
    -webkit-transition: height 1s ease;
    -o-transition: height 1s ease;
    transition: height 1s ease;
    }

        <div class="foreground" id="fgDiv">
    <div id="fgContent">
    <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
    <p class="text">Balh.</p>
    </div>
    <p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
    </p>
    </div>
    <div class="hidden" id="whatIsIt">
    <h1 class="headline">What is a Grimmage?</h1>
    <p class="text">More blah.</p>
    <p class="text">So much blah.</p>
    </div>





    I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.



      Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.



      It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:






          function rightClick() {
      fgContent = document.querySelector('#fgContent');
      rtButton = document.querySelector('#rightButton');
      fgDiv = document.querySelector('#fgDiv');
      spacing = fgDiv.clientHeight - fgContent.clientHeight;
      if (rtButton.textContent === "What's a Grimmage?") {
      nextButton = "How's it work?";
      nextDiv = document.querySelector('#whatIsIt');
      }
      // I removed a handful of similar if statements.
      fgDiv.maxHeight = fgDiv.clientHeight;
      fgDiv.minHeight = fgDiv.clientHeight;
      fgContent.innerHTML = nextDiv.innerHTML;
      fgDiv.maxHeight = nextDiv.clientHeight + spacing;
      console.log('Max height')
      fgContent.minHeight = 0;
      rtButton.textContent = nextButton;
      };

          .foreground {
      position: relative;
      z-index: 1;
      width: 33%;
      margin-top: 10%;
      margin-left: 10%;
      padding: 10px;
      color: black;
      background: whitesmoke;
      height: auto;
      opacity: .85;
      border-radius: 10px;
      -moz-transition: height 1s ease;
      -webkit-transition: height 1s ease;
      -o-transition: height 1s ease;
      transition: height 1s ease;
      }

          <div class="foreground" id="fgDiv">
      <div id="fgContent">
      <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
      <p class="text">Balh.</p>
      </div>
      <p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
      </p>
      </div>
      <div class="hidden" id="whatIsIt">
      <h1 class="headline">What is a Grimmage?</h1>
      <p class="text">More blah.</p>
      <p class="text">So much blah.</p>
      </div>





      I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.










      share|improve this question















      First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.



      Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.



      It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:






          function rightClick() {
      fgContent = document.querySelector('#fgContent');
      rtButton = document.querySelector('#rightButton');
      fgDiv = document.querySelector('#fgDiv');
      spacing = fgDiv.clientHeight - fgContent.clientHeight;
      if (rtButton.textContent === "What's a Grimmage?") {
      nextButton = "How's it work?";
      nextDiv = document.querySelector('#whatIsIt');
      }
      // I removed a handful of similar if statements.
      fgDiv.maxHeight = fgDiv.clientHeight;
      fgDiv.minHeight = fgDiv.clientHeight;
      fgContent.innerHTML = nextDiv.innerHTML;
      fgDiv.maxHeight = nextDiv.clientHeight + spacing;
      console.log('Max height')
      fgContent.minHeight = 0;
      rtButton.textContent = nextButton;
      };

          .foreground {
      position: relative;
      z-index: 1;
      width: 33%;
      margin-top: 10%;
      margin-left: 10%;
      padding: 10px;
      color: black;
      background: whitesmoke;
      height: auto;
      opacity: .85;
      border-radius: 10px;
      -moz-transition: height 1s ease;
      -webkit-transition: height 1s ease;
      -o-transition: height 1s ease;
      transition: height 1s ease;
      }

          <div class="foreground" id="fgDiv">
      <div id="fgContent">
      <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
      <p class="text">Balh.</p>
      </div>
      <p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
      </p>
      </div>
      <div class="hidden" id="whatIsIt">
      <h1 class="headline">What is a Grimmage?</h1>
      <p class="text">More blah.</p>
      <p class="text">So much blah.</p>
      </div>





      I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.






          function rightClick() {
      fgContent = document.querySelector('#fgContent');
      rtButton = document.querySelector('#rightButton');
      fgDiv = document.querySelector('#fgDiv');
      spacing = fgDiv.clientHeight - fgContent.clientHeight;
      if (rtButton.textContent === "What's a Grimmage?") {
      nextButton = "How's it work?";
      nextDiv = document.querySelector('#whatIsIt');
      }
      // I removed a handful of similar if statements.
      fgDiv.maxHeight = fgDiv.clientHeight;
      fgDiv.minHeight = fgDiv.clientHeight;
      fgContent.innerHTML = nextDiv.innerHTML;
      fgDiv.maxHeight = nextDiv.clientHeight + spacing;
      console.log('Max height')
      fgContent.minHeight = 0;
      rtButton.textContent = nextButton;
      };

          .foreground {
      position: relative;
      z-index: 1;
      width: 33%;
      margin-top: 10%;
      margin-left: 10%;
      padding: 10px;
      color: black;
      background: whitesmoke;
      height: auto;
      opacity: .85;
      border-radius: 10px;
      -moz-transition: height 1s ease;
      -webkit-transition: height 1s ease;
      -o-transition: height 1s ease;
      transition: height 1s ease;
      }

          <div class="foreground" id="fgDiv">
      <div id="fgContent">
      <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
      <p class="text">Balh.</p>
      </div>
      <p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
      </p>
      </div>
      <div class="hidden" id="whatIsIt">
      <h1 class="headline">What is a Grimmage?</h1>
      <p class="text">More blah.</p>
      <p class="text">So much blah.</p>
      </div>





          function rightClick() {
      fgContent = document.querySelector('#fgContent');
      rtButton = document.querySelector('#rightButton');
      fgDiv = document.querySelector('#fgDiv');
      spacing = fgDiv.clientHeight - fgContent.clientHeight;
      if (rtButton.textContent === "What's a Grimmage?") {
      nextButton = "How's it work?";
      nextDiv = document.querySelector('#whatIsIt');
      }
      // I removed a handful of similar if statements.
      fgDiv.maxHeight = fgDiv.clientHeight;
      fgDiv.minHeight = fgDiv.clientHeight;
      fgContent.innerHTML = nextDiv.innerHTML;
      fgDiv.maxHeight = nextDiv.clientHeight + spacing;
      console.log('Max height')
      fgContent.minHeight = 0;
      rtButton.textContent = nextButton;
      };

          .foreground {
      position: relative;
      z-index: 1;
      width: 33%;
      margin-top: 10%;
      margin-left: 10%;
      padding: 10px;
      color: black;
      background: whitesmoke;
      height: auto;
      opacity: .85;
      border-radius: 10px;
      -moz-transition: height 1s ease;
      -webkit-transition: height 1s ease;
      -o-transition: height 1s ease;
      transition: height 1s ease;
      }

          <div class="foreground" id="fgDiv">
      <div id="fgContent">
      <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
      <p class="text">Balh.</p>
      </div>
      <p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
      </p>
      </div>
      <div class="hidden" id="whatIsIt">
      <h1 class="headline">What is a Grimmage?</h1>
      <p class="text">More blah.</p>
      <p class="text">So much blah.</p>
      </div>






      javascript css css-animations






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 at 21:22









      Scott Marcus

      37.9k51936




      37.9k51936










      asked Nov 17 at 21:14









      toby.in.dresden

      529




      529
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Use CSS transitions, (as you have done) and JS .scrollHeight.



          Longer explanation:




          • Set the dimensions on page load, because Transitions can't animate something from auto to ##px.

          • Set the scrollHeight dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)


          Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.






          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>








          share|improve this answer























          • Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
            – toby.in.dresden
            Nov 17 at 22:22










          • @toby.in.dresden Gotcha. Updated.
            – Chris Happy
            Nov 17 at 22:51










          • I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
            – toby.in.dresden
            Nov 18 at 22:47










          • @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
            – Chris Happy
            Nov 19 at 22:57











          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',
          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%2f53355649%2fanimate-change-in-size-of-div-when-content-changes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Use CSS transitions, (as you have done) and JS .scrollHeight.



          Longer explanation:




          • Set the dimensions on page load, because Transitions can't animate something from auto to ##px.

          • Set the scrollHeight dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)


          Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.






          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>








          share|improve this answer























          • Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
            – toby.in.dresden
            Nov 17 at 22:22










          • @toby.in.dresden Gotcha. Updated.
            – Chris Happy
            Nov 17 at 22:51










          • I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
            – toby.in.dresden
            Nov 18 at 22:47










          • @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
            – Chris Happy
            Nov 19 at 22:57















          up vote
          0
          down vote













          Use CSS transitions, (as you have done) and JS .scrollHeight.



          Longer explanation:




          • Set the dimensions on page load, because Transitions can't animate something from auto to ##px.

          • Set the scrollHeight dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)


          Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.






          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>








          share|improve this answer























          • Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
            – toby.in.dresden
            Nov 17 at 22:22










          • @toby.in.dresden Gotcha. Updated.
            – Chris Happy
            Nov 17 at 22:51










          • I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
            – toby.in.dresden
            Nov 18 at 22:47










          • @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
            – Chris Happy
            Nov 19 at 22:57













          up vote
          0
          down vote










          up vote
          0
          down vote









          Use CSS transitions, (as you have done) and JS .scrollHeight.



          Longer explanation:




          • Set the dimensions on page load, because Transitions can't animate something from auto to ##px.

          • Set the scrollHeight dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)


          Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.






          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>








          share|improve this answer














          Use CSS transitions, (as you have done) and JS .scrollHeight.



          Longer explanation:




          • Set the dimensions on page load, because Transitions can't animate something from auto to ##px.

          • Set the scrollHeight dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)


          Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.






          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>








          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>





          var fgDiv = document.querySelector('#fgDiv');
          var fgContent = document.querySelector('#fgContent');
          var rtButton = document.querySelector('#rightButton');

          updateDimesions(fgContent, fgContent); // First update of the dimensions

          function rightClick() {
          if (rtButton.textContent === "What's a Grimmage?") {
          nextButton = "How's it work?";
          nextDiv = document.querySelector('#whatIsIt');
          }
          // I removed a handful of similar if statements.
          fgContent.innerHTML = nextDiv.innerHTML;
          rtButton.textContent = nextButton;

          updateDimesions(fgContent, nextDiv); // Second update of the dimensions
          };

          function updateDimesions(el, replace) {
          el.style.width = replace.scrollWidth + 'px';
          el.style.height = replace.scrollHeight + 'px';
          }

          .foreground {
          width: 33%;
          background: whitesmoke;
          }

          .foreground-inner {
          overflow: hidden;
          /* Hide the content initially */
          -moz-transition: all 1s ease;
          -webkit-transition: all 1s ease;
          -o-transition: all 1s ease;
          transition: all 1s ease;
          }

          .hidden {
          height: 0;
          overflow: hidden;
          }

          .hidden > div {
          overflow: hidden;
          /* This accounts for margin in ".scrollHeight" */
          padding: 0;
          /* Padding messes with .scrollHeight */
          }

          <div class="foreground" id="fgDiv">
          <div id="fgContent" class="foreground-inner">
          <h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
          <p class="text">Balh.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          <p>
          <div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
          </p>
          <div class="hidden">
          <div id="whatIsIt">
          <h1 class="headline">What is a Grimmage?</h1>
          <p class="text">More blah.</p>
          <p class="text">So much blah.</p>
          </div>
          </div>
          </div>

          </div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 at 22:50

























          answered Nov 17 at 21:43









          Chris Happy

          4,3881833




          4,3881833












          • Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
            – toby.in.dresden
            Nov 17 at 22:22










          • @toby.in.dresden Gotcha. Updated.
            – Chris Happy
            Nov 17 at 22:51










          • I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
            – toby.in.dresden
            Nov 18 at 22:47










          • @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
            – Chris Happy
            Nov 19 at 22:57


















          • Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
            – toby.in.dresden
            Nov 17 at 22:22










          • @toby.in.dresden Gotcha. Updated.
            – Chris Happy
            Nov 17 at 22:51










          • I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
            – toby.in.dresden
            Nov 18 at 22:47










          • @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
            – Chris Happy
            Nov 19 at 22:57
















          Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
          – toby.in.dresden
          Nov 17 at 22:22




          Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
          – toby.in.dresden
          Nov 17 at 22:22












          @toby.in.dresden Gotcha. Updated.
          – Chris Happy
          Nov 17 at 22:51




          @toby.in.dresden Gotcha. Updated.
          – Chris Happy
          Nov 17 at 22:51












          I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
          – toby.in.dresden
          Nov 18 at 22:47




          I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
          – toby.in.dresden
          Nov 18 at 22:47












          @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
          – Chris Happy
          Nov 19 at 22:57




          @toby.in.dresden Your hidden divs can have display: none, otherwise, JS will think it has height: 0. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;.
          – Chris Happy
          Nov 19 at 22:57


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355649%2fanimate-change-in-size-of-div-when-content-changes%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?