CSS image resize percentage of itself?

前端 未结 11 1886
执念已碎
执念已碎 2020-11-28 20:46

I am trying to resize an img with a percentage of itself. For example, I just want to shrink the image by half by resizing it to 50%. But applying width: 50%; w

相关标签:
11条回答
  • 2020-11-28 21:37

    This is a very old thread but I found it while searching for a simple solution to display retina (high res) screen capture on standard resolution display.

    So there is an HTML only solution for modern browsers :

    <img srcset="image.jpg 100w" sizes="50px" src="image.jpg"/>
    

    This is telling the browser that the image is twice the dimension of it intended display size. The value are proportional and do not need to reflect the actual size of the image. One can use 2w 1px as well to achieve the same effect. The src attribute is only used by legacy browsers.

    The nice effect of it is that it display the same size on retina or standard display, shrinking on the latter.

    0 讨论(0)
  • 2020-11-28 21:38

    I have 2 methods for you.

    Method 1. demo on jsFiddle

    This method resize image only visual not it actual dimensions in DOM, and visual state after resize centered in middle of original size.

    html:

    <img class="fake" src="example.png" />
    

    css:

    img {
      -webkit-transform: scale(0.5); /* Saf3.1+, Chrome */
         -moz-transform: scale(0.5); /* FF3.5+ */
          -ms-transform: scale(0.5); /* IE9 */
           -o-transform: scale(0.5); /* Opera 10.5+ */
              transform: scale(0.5);
                 /* IE6–IE9 */
                 filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.9999619230641713, M12=-0.008726535498373935, M21=0.008726535498373935, M22=0.9999619230641713,SizingMethod='auto expand');
    }​
    

    Browser support note: browsers statistics showed inline in css.

    Method 2. demo on jsFiddle

    html:

    <div id="wrap">
        <img class="fake" src="example.png" />
        <div id="img_wrap">
            <img class="normal" src="example.png" />
        </div>
    </div>​
    

    css:

    #wrap {
        overflow: hidden;
        position: relative;
        float: left;
    }
    
    #wrap img.fake {
        float: left;
        visibility: hidden;
        width: auto;
    }
    
    #img_wrap {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
    
    #img_wrap img.normal {
        width: 50%;
    }​
    

    Note: img.normal and img.fake is the same image.
    Browser support note: This method will work in all browsers, because all browsers support css properties used in method.

    The method works in this way:

    1. #wrap and #wrap img.fake have flow
    2. #wrap has overflow: hidden so that its dimensions are identical to inner image (img.fake)
    3. img.fake is the only element inside #wrap without absolute positioning so that it doesn't break the second step
    4. #img_wrap has absolute positioning inside #wrap and extends in size to the entire element (#wrap)
    5. The result of the fourth step is that #img_wrap has the same dimensions as the image.
    6. By setting width: 50% on img.normal, its size is 50% of #img_wrap, and therefore 50% of the original image size.
    0 讨论(0)
  • 2020-11-28 21:38

    Although it does not answer the question directly, one way to scale images is relative to the size (especially width) of the viewport, which is mostly the use case for responsive design. No wrapper elements needed.

    img {
        width: 50vw;
    }
    <img src="" />

    0 讨论(0)
  • 2020-11-28 21:41

    Actually most of the answers here doesn't really scale the image to the width of itself.

    We need to have a width and height of auto on the img element itself so we can start with it's original size.

    After that a container element can scale the image for us.

    Simple HTML example:

    <div style="position: relative;">
        <figure>
           <img src="your-image@2x.png" />
        </figure>
    </div>
    

    And here are the CSS rules. I use an absolute container in this case:

    figure {
        position: absolute;
        left: 0;
        top: 0;
        -webkit-transform: scale(0.5); 
        -moz-transform: scale(0.5);
        -ms-transform: scale(0.5); 
        -o-transform: scale(0.5);
        transform: scale(0.5);
        transform-origin: left;
    } 
    
    figure img {
        width: auto;
        height: auto;
    }
    

    You could tweak the image positioning with rules like transform: translate(0%, -50%);.

    0 讨论(0)
  • 2020-11-28 21:42

    Try zoom property

    <img src="..." style="zoom: 0.5" />
    

    Edit: Apparently, FireFox doesn't support zoom property. You should use;

    -moz-transform: scale(0.5);
    

    for FireFox.

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