title: Dynamically resize iframe
body: <p><strong>Situation:</strong>
  I'm working on a responsive design that involves the typical HTML/CSS combo. Everything is working nicely except in one case where there is an iframe inside of a div. The iframe should adjust automatically to the size of the parent div. A purely css solution has not presented itself so I'm going with a JQuery approach. It works nicely except in one scenario, when resizing from a smaller width to a larger width screen.</p>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;iframe class="iframe-class" src="http://www.cnn.com/"&gt;&lt;/iframe&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>CSS:</strong></p>
  
  <pre><code>.container {
      width: 100%;
      height: 250px;
  }
  .iframe-class {
      width: 100%;
      height: 100%;
      border: 1px solid red;
      overflow: auto;
  }
  </code></pre>
  
  <p><strong>Javascript:</strong></p>
  
  <pre><code>$(function () {
      setIFrameSize();
      $(window).resize(function () {
          setIFrameSize();
      });
  });
  
  function setIFrameSize() {
      var ogWidth = 700;
      var ogHeight = 600;
      var ogRatio = ogWidth / ogHeight;
  
      var windowWidth = $(window).width();
      if (windowWidth &lt; 480) {
          var parentDivWidth = $(".iframe-class").parent().width();
          var newHeight = (parentDivWidth / ogRatio);
          $(".iframe-class").addClass("iframe-class-resize");
          $(".iframe-class-resize").css("width", parentDivWidth);
          $(".iframe-class-resize").css("height", newHeight);
      } else {
          // $(".iframe-class-resize").removeAttr("width");
          // $(".iframe-class-resize").removeAttr("height");
          $(".iframe-class").removeClass("iframe-class-resize");
      }
  }
  </code></pre>
  
  <p><a href="http://jsfiddle.net/TBJ83/" rel="noreferrer">http://jsfiddle.net/TBJ83/</a></p>
  
  <p><strong>Problem:</strong>
  As the window is resized smaller, it continually checks the window width and once it hits &lt; 480 px, the code adds a class called <code>iframe-class-resize</code> and sets the width and height to that class. As the window is resized larger, it removes the class once the size hits 480 px. The problem is that setting the width and height attributes adds them directly to the element and not the class itself. Therefore, removing the class does not remove the new width and heights. I tried to force removing the attributes using <code>removeAttr()</code> (commented out above) but that didn't work.</p>
  
  <p>Anyone see where the code above went wrong? Or any suggestions on how to accomplish having a responsive iframe more effectively? The main things that are required are that the iframe has to be inside the <code>&lt;div&gt;&lt;/div&gt;</code> and the div may not necessarily have a width or height defined. Ideally, the parent div should have the width and height explicitly defined but the way this site is currently setup, that won't always be possible.</p>
  
  <p><strong>Additional:</strong>
  In case the above wasn't clear enough, try the following to reproduce the issue:</p>
  
  <ul>
  <li>Open up a browser on a desktop machine. I'm using Chrome on a Windows machine. Don't maximize the browser.</li>
  <li>Open up the jsfiddle above (<a href="http://jsfiddle.net/TBJ83/" rel="noreferrer">http://jsfiddle.net/TBJ83/</a>). You'll notice that the iframe content spans the entire width of the Preview panel.</li>
  <li>Manually resize the width down until the entire window is &lt; 480px. At this point, the iframe content will be pretty tiny.</li>
  <li>Manually resize the width back up until the entire window is >> 480px. The goal is to have that iframe content to regain the entire width of the Preview panel. Instead, the content is retaining the resized width and height since the <code>.css()</code> function applies css changes directly to elements rather than to the classes.</li>
  </ul>
  
  <p>Thanks in advance!</p>