简体   繁体   English

CSS:自适应背景图片高度

[英]CSS: Responsive background image height

I'm using this CSS: 我正在使用此CSS:

.wrap {
    max-width:1400px;
    min-width:768px;
    width:100%;
    background-repeat: no-repeat scroll;
    background-position: center top;
    position: relative;
    margin-top: 0;
    margin-right: auto;
    margin-bottom: 0;
    margin-left: auto;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}
.image {
    width: 100%;
    height: 600px;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

To center an image and scale it based on window size, stopping the scaling at 768px. 要使图像居中并根据窗口大小缩放图像,请在768px处停止缩放。 I can't display the bottom part of the image when the window is at a larger size (it works fine when the window is minimized). 当窗口较大时,我无法显示图像的底部(将窗口最小化时,效果很好)。 When I change the height of the .image class to more than 600px, it breaks the image scaling... 当我将.image类的高度更改为600px以上时,它会破坏图像缩放比例...

Here's a demo of the problem: http://jrbaldwin.com/css_issue/ 这是问题的演示: http : //jrbaldwin.com/css_issue/

I think it's not scaling because the height is staying fixed. 我认为它没有缩放,因为高度保持固定。 For example, set the .image height to 1000px. 例如,将.image高度设置为1000px。 It has to enlarge the image to cover 1000px so it fills the entire 1000px height at all time, no matter what width the image is. 它必须放大图像以覆盖1000像素,因此无论图像的宽度如何,它始终会占据整个1000像素的高度。

Possibly try: 可能尝试:

background-size: 100%; 背景大小:100%; background-repeat: no-repeat; 背景重复:不重复;

Building on @jtlowe's answer, couldn't you achieve the right effect by using background-size: contain instead? 以@jtlowe的答案为基础,您不能通过使用background-size: contain来达到正确的效果吗?

Since you're already firmly in the CSS3 domain, you can optionally create a media query that uses cover in smaller size windows. 由于您已经牢固地进入CSS3域,因此可以选择创建在较小尺寸窗口中使用cover的媒体查询。

使用background-size:包含而不是cover。

Try this styles: 尝试以下样式:

.image{
  width: 100%;
  height: 600px;
  max-height: 1000px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center center;
  max-height: 1000px;
}

The main idea is to use "contain" property 主要思想是使用“包含”属性
More information: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Scaling_background_images 更多信息: https : //developer.mozilla.org/en-US/docs/Web/Guide/CSS/Scaling_background_images

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

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