简体   繁体   English

如何使背景图像大小保持其比例?

[英]How to make background-image size maintain its proportions?

Example: http://jsfiddle.net/SJUeK/ 示例: http//jsfiddle.net/SJUeK/

I want for a background image in example to be always its full height (dependent on width). 我希望示例中的背景图像始终是其全高(取决于宽度)。 I can't do it with pixels, because it needs to be responsive. 我不能用像素来做,因为它需要响应。

Is there a way to do that? 有没有办法做到这一点? Or need I to use img in html? 或者我需要在html中使用img?

Thanks in advance. 提前致谢。

Sure: 当然:

background-size: auto 100%;

Here's a demo. 这是一个演示。

Or if you actually meant "maintain its aspect ratio and scale to fit inside the container", then background-size: contain should do the trick. 或者,如果您实际上意味着“保持其纵横比和比例以适应容器内”,那么background-size: contain应该可以解决问题。

html { 
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

https://css-tricks.com/perfect-full-page-background-image/ https://css-tricks.com/perfect-full-page-background-image/

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

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