Preventing blurry rendering with transform: scale

前端 未结 1 816
遇见更好的自我
遇见更好的自我 2021-02-14 19:24

I\'m scaling a div up with the transform property, but I want to keep its children (which have 1px width or height) the same size. I counter-scaled the

1条回答
  •  有刺的猬
    2021-02-14 19:51

    It is to do with the default transform-origin on the scaled elements. It defaults to 50% 50% for any element being transformed, but this has issues when scaling down 1px values as it has to centre the scale on a half pixel and the rendering of the elements has issues from here on out. You can see it working here with the transform-origin moved to the relevant extremes for each item.

    A bit of playing about shows that this same blurring happens on scaled elements for any dimension where the scaling ends up halving a pixel.

    body {
        padding: 1em;
    }
    
    .container {
        width: 200px;
        height: 200px;
        margin: 100px;
        background-color: #EEE;
        position: absolute;
        transform: scale(2);
    }
    
    .outline {
        position: absolute;
        background: #1899ef;
        z-index: 999999;
        opacity: 1 !important;
    }
    
    .outlineBottom, .outlineTop {
        width: 100%;
        height: 1px;
        transform: scale(1, 0.5);
    }
    
    .outlineBottom {
        bottom: 0;
        transform-origin: 0 100%;
    }
    
    .outlineTop {
        transform-origin: 0 0;
    }
    
    .outlineLeft, .outlineRight {
        height: 100%;
        width: 1px;
        transform: scale(.5,1);
    }
    
    .outlineRight {
        right: 0px;
        transform-origin: 100% 0;
    }
    
    .outlineLeft {
        left: 0px;
        transform-origin: 0 0;
    }

    0 讨论(0)
提交回复
热议问题