简体   繁体   English

如何根据图像缩放图像-CSS

[英]how to scale image according to image - css

I am soo bad at css. 我对CSS很不好。 I have a div in which multiple images can be put. 我有一个可以放置多个图像的div。 this div is a slide. 这个div是一张幻灯片。

<div id="slides">                  
   <img src="imagename.jpg"/>                                                                 
</div>

some images are eg 100x300 but some are 300x100. 有些图像例如是100x300,有些则是300x100。 how can I scale those images according to their own dimensions so that they show up as in original form? 如何根据自己的尺寸缩放这些图像,使其以原始形式显示?

my live example with this bug is in here: http://wohne-wo-du-willst.de/angebot/Wohnung/ist-frei-in-Dachau/22/ 我带这个错误的实时示例在这里: http : //wohne-wo-du-willst.de/angebot/Wohnung/ist-frei-in-Dachau/22/

as you slide, the 3rd slide image doesnot show up in its full form.. 当您滑动时,第3张幻灯片图像不会完整显示。

A big problem you have here is that your images aren't 100x300px or 300x100px, they're instead 1920x2560px or 2560x1920px - much, much larger. 您这里遇到的一个大问题是图片不是 100x300px或300x100px,而是1920x2560px或2560x1920px-大得多。 They're also all around 1MB in size, which in reality is far too large for images like this. 它们的大小也都在1MB左右,实际上对于这样的图像来说太大了。 Before doing anything you should resize these images yourself to make them their desired size and ultimately reduce their file size as well. 在执行任何操作之前,您应该自己调整这些图像的大小,以使其达到所需的大小,并最终减小其文件大小。

After that, simply remove the max-width and max-height properties from your #slides img selector, then modify width and height to: 之后,只需从#slides img选择器中删除max-widthmax-height属性,然后将widthheight修改为:

#slides img {
    ...
    height: initial;
    width: initial !important;
}

The initial value sets the image size to its initial size; initial值将图像尺寸设置为其初始尺寸; that means if your image is 100x300, that is also its initial size. 也就是说,如果您的图片尺寸为100x300,则它也是其初始尺寸。 Using this with the images you currently have will set them to 1920x2560 or 2560x1920. 将其与当前使用的图像一起将其设置为1920x2560或2560x1920。

I've unfortunately had to use !important here as the slide plugin will try to force your images to 100% width. 不幸的是,我不得不在这里使用!important ,因为幻灯片插件会尝试将您的图片强制设为100%的宽度。

<style>
img
   {
      height:100%;
      width:100%;
    }
</style>

<div id="slides">                 
 <img src="imagename.jpg"/>                                                                 
</div>

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

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