CSS图像调整自身的百分比? [英] CSS image resize percentage of itself?

查看:271
本文介绍了CSS图像调整自身的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个百分比自己调整img的大小。例如,我只想将图像缩小一半,将其调整为50%。但是应用 width:50%; 会将图像大小调整为容器元素的50%(父元素也许< body& / code>)。



问题是,我可以使用自身的百分比调整图片大小,而无需使用javascript或服务器端? (我没有图像大小的直接信息)



我确定你不能这样做,但我只想看看是否有智能的CSS解决方案。谢谢!

解决方案

我有2种方法。



1. jsFiddle演示



此方法只调整图片大小



html


$ b

 < img class =fakesrc =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');
}

浏览器支持说明:浏览器统计信息在 css 中显示为内联。 jsfiddle.net/matmuchrapna/dhcDx/23/\"> dos on jsFiddle

html:

 < div id =wrap> 
< img class =fakesrc =example.png/>
< div id =img_wrap>
< img class =normalsrc =example.png/>
< / div>
< / div>

css: p>

  #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%;
}

注意: > img.normal img.fake 是同一张图片。

em>
浏览器支持注意:此方法可以在所有浏览器中使用,因为所有浏览器都支持 css b
$ b

该方法以这种方式工作:




  1. '#wrap'和'#wrap img.fake 'have flow

  2. #wrap :hidden 设置 #wrap 尺寸与内部图像相同( img.fake

  3. img.fake 只是没有 absolute 修复在 #wrap 中的位置,以不破坏第二步的结果。

  4. #img_wrap #wrap 中有绝对整个元素( #wrap

  5. 第四步的结果是 #wrap_img c>宽度:50%; code>的 img.normal ,现在你得到了你的结果。


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%; will resize the image to be 50% of the container element (the parent element which maybe the <body> for example).

Question is, can I resize the image with a percentage of itself without using javascript or server side? (I have no direct information of the image size)

I am pretty sure you cannot do this, but I just want to see whether there are intelligent CSS only solution. Thanks!

解决方案

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 to set #wrap dimensions identically to inner image (img.fake)
  3. img.fake is only the one element without absolute or fix position in #wrap to not break the result of second step.
  4. #img_wrap has absolute position inside #wrap and extended in size to the entire element (#wrap)
  5. Result of the fourth step is #wrap_img the same dimensions as image.
  6. Using responsive images method set width: 50%; of img.normal and now you got your result.

这篇关于CSS图像调整自身的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆