简体   繁体   English

用身体的宽度和高度百分比控制图像的纵横比

[英]Controlling aspect ratio of images with % width and height of body

I have these two images appearing in my site that I am pulling from another company's server. 我从我的另一家公司的服务器中提取了这两个图像。 I need these photos to scale with the browser window so I've given them a % width and height to scale with the body. 我需要这些照片在浏览器窗口中缩放,所以我给它们提供了%的宽度和高度以与主体缩放。

The problem is some of these images are different sizes and I need all the images to look identical to each other. 问题是这些图像中的一些图像大小不同,我需要所有图像看起来彼此相同。 This is easy to do when I know the values of the image's width and height because I can just crop or resize. 当我知道图像的宽度和高度的值时,这很容易做到,因为我可以裁剪或调整大小。 But how can you maintain a ratio with a scalable width and height? 但是,如何保持宽度和高度可缩放的比例呢?

I think some JavaScript is required to do this. 我认为要执行此操作需要一些JavaScript。 Here's the CSS that I have: 这是我拥有的CSS:

html {
  width: 100%;
  height: 100%;
}
body {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}
.image {
  width: 40%;
  height: 40%;
  margin: 1%;
}

If you're not concerned about IE8, you can just specify the width . 如果您不关心IE8,则只需指定width Most browsers will maintain the aspect ratio for you. 大多数浏览器会为您保持宽高比。

Doing it with CSS only will take you a lot of time to optimize for all brosers. 仅使用CSS进行此操作将花费大量时间来优化所有Brosers。

With a few modifications and js you may achieve this using Perfect background image 通过一些修改和js,您可以使用Perfect背景图片实现此目的

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

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