White space around css3 scale

后端 未结 6 476
感情败类
感情败类 2020-11-27 16:35

I have a small issue I want to fix, but can\'t find any good answer :

When I use a scale on a div (which contains other divs), it leave white space around, from the

相关标签:
6条回答
  • 2020-11-27 17:20

    how transform works is:

    1. your element gets rendered
    2. your element gets transformed (moved, rotated, scaled)
    3. other elements stay where they got rendered - around the "original element"

    so the white space is really just the way the element was rendered in the first place.

    You should use width and height in CSS if you want to render the size of elements differently and have the surrounding elements respond to it.

    Or you could use something like javascript to resize things.

    0 讨论(0)
  • 2020-11-27 17:28

    Another idea for white spaces when you transform objects

    <div class="outer">
        <div class="inner">
            transformme
        </div>
    </div>
    

    css

    .outer { overflow:hidden }
    .inner {
        transform-origin:left top;
    }
    

    js

    var rate = 0.5;
    var outerheight = $('.inner').outerHeight()*rate;
    $('.inner').css({ transform: "scale("+rate+")" });
    $('.outer').css({ height: outerheight });
    

    Also you can add other browser tags; -webkit-transform, -moz-transform, -o-transform

    0 讨论(0)
  • 2020-11-27 17:30

    I solved this with by adding an 'outline: 1px solid transparent' to the element where the scale is applied on.

    #wrap:hover .quarter
    {
       -webkit-transform: scale(0.9);
       -moz-transform: scale(0.9);
       -o-transform: scale(0.9);
       transform: scale(0.9);
       -webkit-transform-origin:left top;
       -moz-transform-origin:left top;
    
       outline: 1px solid transparent;
    }
    
    0 讨论(0)
  • 2020-11-27 17:33

    I resolved my problem like yours that way.

    I have a main container and I want decrease it

    my css: .grid-container.full { transform: scale(0.6); transform-origin: top center; }

    but my container had the bigger margin bottom. then I do it:

    $mainGrid = $('.grid-container.full') $mainGrid.css('height', $mainGrid.height() * .6);

    0 讨论(0)
  • 2020-11-27 17:33

    I encountered this problem and I solved it in this way, I used SCSS in order to don't repeat the same numbers along the code. The below code just moves the element right as the zoom decreases, in order to re-align it.

    $originalWidth: 100px;
    $rate: 0.5;
    
    parent {
      width: $originalWidth;
    }
    
    parent > div {
      transform: scale(1);
    }
    
    parent:hover {
      width: $originalWidth*$rate;
    }
    
    parent:hover > div {
      transform: translateX(($originalWidth * ($rate - 1))/2) scale($rate); /* the order matters*/
    }
    

    You can get rid of SCSS just using CSS variables and calc(), if you prefer.

    0 讨论(0)
  • 2020-11-27 17:39

    solution is to wrap the element inside a container, and resize it too while the scale() is done

    Jsfiddle demo: http://jsfiddle.net/2KxSJ/

    relevant code is:

    #wrap
    {
        background:yellow;
        height:66px;
        width:55px;
        padding:10px;
        float:left;
        -webkit-transition:0.5s all;
        -moz-transition:0.5s all;
        /* more transition here */
    }
    
    #wrap:hover
    {
        height:300px;
        width:260px;
    }
    
    .quarter
    {
        padding:20px;
        -webkit-transform: scale(0.2);
        -moz-transform: scale(0.2);
        -o-transform: scale(0.2);
        transform: scale(0.2);
        background:red;
        width:250px;
        -webkit-transform-origin:left top;
        -webkit-transition:0.5s all;
        -moz-transition:0.5s all;
        /* more transition here */
    }
    
    
    #wrap:hover .quarter
    {
        -webkit-transform: scale(0.9);
        -moz-transform: scale(0.9);
        -o-transform: scale(0.9);
        transform: scale(0.9);
        -webkit-transform-origin:left top;
        -moz-transform-origin:left top;
        /* more transform-origin */
    }
    
    0 讨论(0)
提交回复
热议问题