简体   繁体   English

使用css调整图像大小

[英]resize image using css

I am trying to resize image using css only. 我正在尝试仅使用css调整图像大小。

It is resizing but for some reason it is not stretching to 100% of the browser.What I want is it will resize the image with given height but width should be 100% throughout the browser. 它正在调整大小,但由于某种原因,它没有延伸到100%的浏览器。我想要的是它将调整给定高度的图像,但整个浏览器的宽度应为100%。

I have created a fiddle as demo so that you can see what's going on. 我创建了一个小提琴作为演示,以便您可以看到正在发生的事情。

<div class="resize_image">
    <img src="http://www.mrwallpaper.com/wallpapers/sunset-scenery.jpg">
</div>

Full Screen http://jsfiddle.net/squidraj/sbnvwped/embedded/result/ 全屏http://jsfiddle.net/squidraj/sbnvwped/embedded/result/

Demo : http://jsfiddle.net/squidraj/sbnvwped/ 演示: http//jsfiddle.net/squidraj/sbnvwped/

The following code resizes the image proportionally to the width of the page (or more correctly, the container element), but if the height of the image then becomes more than 485px then the width with will be proportional to that. 下面的代码按比例调整图像的大小(或更准确地说,容器元素),但如果图像的高度大于485px,那么宽度将与之成比例。 To chop the image, put another div around it with the right width and height, and set overflow to hidden , and remove the max-height from the image itself. 要切割图像,请在右侧放置另一个具有正确宽度和高度的div,并将overflow设置为hidden ,并从图像本身中移除max-height

.resize_image img {
    display: block;
    height: auto;
    max-height: 485px;
    max-width: 1440px;
    width: 100%;
}

Hope this helps. 希望这可以帮助。

You can resize it by setting the img tag to 100% width and height and puting it in a container div and resizing that. 您可以通过将img标记设置为100%宽度和高度并将其放入容器div并调整其大小来调整大小。 Demo 演示

<div id="resize">
<img src="http://coolvectors.com/images/vect/2009/07/500x500.jpg" width="100%" height="100%"></div>

#resize{
  width:250px;
  height:250px;
}
#resize:hover {
width:500px;
height:500px;}

Try this: 尝试这个:

img.resize{
    width:540px; /* you can use % */
    height: auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM