7

I am having a problem where I scale a div to fit it's parent. This problem only seems to show up in Internet Explorer. I've only tested on versions 9 and 11 but I'm sure it exists in others.

Whenever I scale the window down the div's scale as they are supposed to. However, in internet explorer, they leave this weird white space to the right. This seems to be happening inside #pageContent and the scaling of #formScale seem's to be the issue.

Does anyone have any ideas on why this may be happening? Because I can not figure it out for the life of me.

note - I do not want to solve this by hiding overflow

JSFiddle | JSFiddle Full Screen

Here is an image showing the white space when IE 9's window is shrunk: enter image description here

HTML

<div id="pageContent">
    <div id="formBox">
        <div id="formScale">
            <div class="box"></div>
            <div class="box"></div>
            <div class="box"></div>
            <div class="box"></div>
        </div>
    </div>
</div>

CSS

#pageContent {
    width:100%;
    overflow:auto;
    padding-bottom:20px;
    border:1px solid red;
}

#formBox { display:block;height:auto; padding:15px 10px;border-bottom:5px solid red;}


#formScale::after {
  display: block;
  content: '';
  padding-bottom:5px;
}

#formScale
{
    display:block;
    position:relative;
    width:940px;
    left:50%;
    margin-left:-470px;
    -webkit-transform-origin: top center;
    -moz-transform-origin: top center;
    transform-origin: top center;
    -ms-transform-origin: top center;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.box { height:1178px;width:940px;background-color:yellow;margin-bottom:10px; }

JS

resize();
zoomProject();
$(window).resize(function (e) {
    resize();
    zoomProject();
});

function resize() {
    $("#pageContent").css('height', ($(window).height()-30) + 'px');
}

function zoomProject()
    {
        var maxWidth = $("#formBox").width(),
        percent = maxWidth/930;

        $("#formScale").css({
            'transform': 'scale(' + percent + ')',
            '-moz-transform': 'scale(' + percent + ')',
            '-webkit-transform': 'scale(' + percent + ')',
            '-ms-transform': 'scale(' + percent + ')'
        });
    }
bryan
  • 8,879
  • 18
  • 83
  • 166
  • This same effect seems to be occurring in Chrome too. Compare: http://imgur.com/69aZobH,gNg2ECL (Chrome) vs http://imgur.com/ptVkXOr,05r4W1F (IE). Both seem to have extra whitespace when the window is resized. – Joeytje50 May 12 '14 at 23:30
  • @Joeytie50 I am talking about horizontal white space in particular. When the width of the window get's smaller and the white space to the right of the container that appears – bryan May 12 '14 at 23:33
  • Is it a possibility to just start out with really small elements, and scale everything up instead? Scaling up doesn't generate this whitespace, so that could be a solution, as long as it wouldn't complicate things for some reason. – Joeytje50 May 12 '14 at 23:52
  • May I ask, why is using `overflow-x:hidden` not an option? It seems to me this is the ideal situation to use that. – Joeytje50 May 12 '14 at 23:53
  • @Joeytje50 I answered this in Niet's answer. I'm open to changes's in structure but I really do need the x scroll. It would complicate things a lot of I scaled up. – bryan May 12 '14 at 23:59
  • Have you tried applying a reset to the page? Specifically, reset the body {padding:0;}. – GlennG May 19 '14 at 17:08
  • yea, there is a reset. @GlennG – bryan May 20 '14 at 13:08

3 Answers3

6

When you transform an element, it's somewhat like using position:relative. The element still takes up its original place in the document flow, it just gets rendered differently.

By that reasoning, I'd be inclined to say that the better question is why does Chrome not have this empty space? Technically, it's supposed to!

To resolve the issue, you can simply use overflow-x:hidden to remove horizontal scrolling from your container element. I know you said you don't want to, but that's the only solution I can think of.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thanks for putting it in perspective. The reason for the zoom is I allow users to zoom into view. If I want to zoom over 100% the window size, I am going to need the overflow-x scroll so that a user can look at the content in a scaled environment. I would still be open to changing the structure of how it is done if there is a better way to not get the white space. Safari, Chrome, & Firefox do not have white space occurring. – bryan May 12 '14 at 23:35
  • Have you tried applying `scale` to the container element? – Niet the Dark Absol May 12 '14 at 23:38
  • @NiettheDarkAbsol that would cause the very same problem, but then for the red bordered `#pageContent` box. It would stretch way to the bottom, while the contents would be scaled down to much less. – Joeytje50 May 12 '14 at 23:51
  • Your reasoning doesn't explain why this sample https://plnkr.co/edit/LtHU8RJH5AntIXWAhwwT?p=preview has both scrollbars in IE, does it? Original vertical placement fits perfectly, so where did vertical scroll came from? – Atomosk Apr 11 '17 at 04:14
4

I GOT IT

I changed the transform origin to top left, used automatic margins on the trimSpace box to center instead, and made a few changes to the JavaScript. Works for me in IE.

I wrapped the formScale box in another div called trimSpace and set the width of it to the new width of the scaled elements. I hide the X overflow for this new div to trim the whitespace. This div can still be larger than the pageContainer box, and scrollbars are functional.

Here, check this fiddle: http://fiddle.jshell.net/bUY75/24/

HTML

<div id="pageContent">
    <div id="formBox">
        <div class="trimSpace">
            <div id="formScale">
                <div class="box"></div>
                <div class="box"></div>
                <div class="box"></div>
                <div class="box"></div>
            </div>
        </div>
    </div>
</div>

JavaScript

zoomProject();
resize();
$(window).resize(function (e) {
    resize();
    zoomProject();
});

function resize() {
    $("#pageContent").css('height', window.innerHeight - 60 + 'px');
}

function zoomProject() {
    var maxWidth = $("#formBox").width(),
        percent = maxWidth / 930;

    $("#formScale").css({
        'transform': 'scale(' + percent + ')',
            '-moz-transform': 'scale(' + percent + ')',
            '-webkit-transform': 'scale(' + percent + ')',
            '-ms-transform': 'scale(' + percent + ')'
    });

    $(".trimSpace").css('width', (930 * percent) + 'px');

    $("#formBox, .trimSpace").css('height', ($("#formScale").height() * percent) + 'px');
}

CSS

#pageContent {
    width:100%;
    overflow:auto;
    padding-bottom:20px;
    border:1px solid red;
}
#formBox {
  display: block;
  position: relative;
  height: 100%;
  padding: 0;
}
#formScale::after {
    display: block;
    content:'';
    padding-bottom:5px;
}
#formScale {
    display:block;
    position:relative;
    width:940px;
    margin: 0; //This was the cause of the left whitespace
    /*left:50%;*/
    /*margin-left:-480px;*/
    -webkit-transform-origin: top left;
    -moz-transform-origin: top left;
    transform-origin: top left;
    -ms-transform-origin: top left;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.trimSpace {
    display: block;
    position: relative;
    margin: 0 auto;
    padding: 0;
    height: 100%;
    /*width: 100%;*/
    overflow: hidden;
}

.box {
    height:1178px;
    width:940px;
    background-color:yellow;
    margin-bottom:10px;
}


Previous

This happens because Internet Explorer does not scale the CSS width attribute when the elements are transformed while other browsers do. The whitespace you're seeing is where the scaled items extended before they were reduced in appearance. You don't need to disable overflow entirely, only on the scaled item. That simply clips off the excess whitespace, not affecting the visibility of the content. I made a few modifications to clean up the demo. Here's a fiddle to demonstrate: http://fiddle.jshell.net/bUY75/2/

and here's the code:

HTML

<input type="range" name="percent" min="1" max="300" step="1" value="100" onchange="zoomProject(this.value)">&nbsp;Zoom (1 to 300%)
<div id="pageContent">
  <div id="formBox">
      <div id="formScale">
        <div class="box">test1<br><input type="text"></div>
        <div class="box">test2</div>
        <div class="box">test3</div>
        <div class="box">test4</div>
      </div>
  </div>
</div>

CSS

#pageContent {
  width: 100%;
  overflow: auto;
  padding-bottom: 20px;
  border: 1px solid red;
}

#formBox {
  display: block;
  position: relative;
  height: 100%;
  padding: 0;
}

#formScale {
  display: block;
  position: relative;
  padding: 15px 10px;
  margin: 0;
  width: 100%;
  overflow-x: hidden;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-transform-origin: top left;
  -moz-transform-origin: top left;
  transform-origin: top left;
  -ms-transform-origin: top left;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.box {
  height: 110px;
  background-color: yellow;
  margin-bottom: 10px;
}

.more-content {
  height: 400px;
  background-color: #2bde73;
  padding: 10px;
  font-size: 18px;
  color: white;
  margin-top: 20px;
}

JavaScript

var height = $("#formScale").height();

window.onload = function() {
  resize();
  $(window).resize(function (e) {
    resize();
  });
};

function resize() {
  $("#pageContent").css('height', window.innerHeight - 60 + 'px');
}

function zoomProject(amount) {
  var maxWidth = $("#formBox").width(),
  percent = amount / 100;

  $("#formScale").css({
    'transform': 'scale(' + percent + ')',
    '-moz-transform': 'scale(' + percent + ')',
    '-webkit-transform': 'scale(' + percent + ')',
    '-ms-transform': 'scale(' + percent + ')'
  });
}
aecend
  • 2,432
  • 14
  • 16
  • It doesn't work when I apply it to my demo. I would really love to give you this bounty, but it doesn't look like you did anything different then what I did. My width's need be consistent with my jsfiddle and that is all I saw you change. If you can figure this out in the 2 hours before my bounty expires, I'd love to reward you with it. http://fiddle.jshell.net/bUY75/4/ – bryan May 20 '14 at 21:39
  • I can send you a screenshot of what happens in IE 9 if you need, let me know. – bryan May 20 '14 at 21:44
  • That would be helpful. Also, on line 14 of the JavaScript in the jsFiddle, you divide by 930 while in the CSS, the box is 940px wide. Is that intentional? – aecend May 20 '14 at 21:45
  • Thanks, I did it for a reason ascetically on my actual site so it left a bit of space. Here is the image, notice the white space when the window is made smaller. http://i.imgur.com/1riX49P.jpg – bryan May 20 '14 at 21:51
  • I didn't want to add another tag in the HTML, but I did in order to preserve the ability to pad the formBox element if you'd like. Otherwise, if you don't need to do that, only adding overflow-x: hidden; to the formBox class would achieve the same thing without clipping any of the content in the box. – aecend May 20 '14 at 22:05
  • Thanks for your help ascend. I really appreciate it. I'm assuming you aren't able to test this in internet explorer? I just wanted to reiterate that the goal right now is to not disable the use of a scroll bar if the scaled content is larger than the window. The end goal is to be able to scale it above 100% and be able to scroll within `pageContent` if the scale is larger than the div itself. – bryan May 20 '14 at 22:31
  • I found the solution. Scrollbars should still be working properly, I only used overflow-x: hidden to clip the whitespace. – aecend May 20 '14 at 22:41
  • it's almost there! Do you know why there is big whitespace on the left? http://i.imgur.com/a0zf969.png – bryan May 20 '14 at 22:50
  • Yep. I made a mistake in the CSS. Here, its corrected, I'll update the answer: http://fiddle.jshell.net/bUY75/22/ – aecend May 20 '14 at 22:56
  • It looks amazing man. Thank you. The only thing I see is in IE 9, there is a slight horizontal scrollbar that scrolls maybe like 2-4px – bryan May 20 '14 at 23:00
  • It's probably a margin thing. If you'll go ahead and award the bounty (if you're satisfied with the answer), I'll figure that out real quick. – aecend May 20 '14 at 23:03
  • Yea, of course I am satisfied. Your help was amazing. Thanks so much ascend, youda man! – bryan May 20 '14 at 23:04
  • Thanks, I'm glad I could help. That horizontal scrollbar is scrolling exactly the width of the vertical scrollbar itself on my end, I'm trying a workaround to fix it right now. – aecend May 20 '14 at 23:13
  • I fixed the scrollbar issue. Run the zoomProject function first, then the resize function at the top of the JavaScript. It was setting pageContent to the screen height and not accounting for the scrollbar on load. You were previously doing the resize() first and THEN the zoomProject() – aecend – aecend May 20 '14 at 23:27
  • i had to come back here and thank you again...you came through with 40min left man. I appreciate it so much. – bryan May 27 '14 at 15:40
  • 1
    Glad I could help. That did turn out to be quite the problem. – aecend May 27 '14 at 15:41
-2

Unfortunately, if you want to target IE specifically - you may have to use IE CSS hacks. http://www.webdevout.net/css-hacks

http://dimox.net/personal-css-hacks-for-ie6-ie7-ie8/

  • 1
    It's going to be hard to target IE when I don't know what's causing the problem. I feel like you didn't even read my question :\ – bryan May 11 '14 at 00:03
  • 2
    Besides what bryan said, I'd also like to point out there are [lots and lots more ways](http://stackoverflow.com/a/20982264/1256925) of targeting IE, all with valid CSS. Both links you included in your answer have loads of invalid CSS hacks, so it's best not to use them. – Joeytje50 May 12 '14 at 23:37