CSS背景图像适合宽度,高度应按比例自动缩放

时间:2012-02-13 15:14:34

标签: html css

我有

body {
    background: url(images/background.svg);
}

期望的效果是此背景图像的宽度等于页面的宽度,高度变化以保持比例。例如如果原始图像恰好是100 * 200(任何单位)且正文宽度为600px,则背景图像最终应为1200px高。如果调整窗口大小,高度应自动更改。这可能吗?

目前,Firefox似乎正在调整高度,然后调整宽度。这可能是因为高度是最长的尺寸而且它试图避免裁剪?我希望垂直裁剪,然后滚动:没有水平重复。

此外,即使明确指定了background-repeat:repeat,Chrome也会将图片置于中心位置,不会重复,无论如何这都是默认值。

10 个答案:

答案 0 :(得分:727)

这有一个CSS3属性,即background-sizecompatibility check)。虽然可以设置长度值,但它通常与特殊值containcover一起使用。在您的具体情况下,您应该使用cover

body {
    background-image:    url(images/background.svg);
    background-size:     cover;                      /* <------ */
    background-repeat:   no-repeat;
    background-position: center center;              /* optional, center the image */
}

containcover

的Eggsplanation

对不好的双关语感到抱歉,但我将使用picture of the day by Biswarup Ganguly进行演示。让我们说这是你的屏幕,灰色区域在你的可见屏幕之外。为了演示,我将采用16x9比率。

screen

我们想用上面提到的那一天的照片作为背景。但是,出于某种原因,我们将图像裁剪为4x3。我们可以将background-size属性设置为某个固定长度,但我们会关注containcover。请注意,我还假设我们没有破坏body的宽度和/或高度。

contain

contain
     

缩放图像,同时保持其固有纵横比(如果有),使其最大尺寸,使其宽度和高度都能够适合背景定位区域。

这可确保背景图像始终完全包含在背景定位区域中,但在这种情况下,background-color可能会填充一些空白空间:

contain

cover

cover
     

缩放图像,同时保持其固有的宽高比(如果有),使其最小尺寸,使其宽度和高度都可以完全覆盖背景定位区域。

这可确保背景图像覆盖所有内容。没有可见的background-color,但是根据屏幕的比例,您的图片很大一部分可能被切断:

cover

使用实际代码进行演示

&#13;
&#13;
div > div {
  background-image: url(http://i.stack.imgur.com/r5CAq.jpg);
  background-repeat: no-repeat;
  background-position: center center;
  background-color: #ccc;
  border: 1px solid;
  width: 20em;
  height: 10em;
}
div.contain {
  background-size: contain;
}
div.cover {
  background-size: cover;
}
/********************************************
 Additional styles for the explanation boxes 
*********************************************/

div > div {
  margin: 0 1ex 1ex 0;
  float: left;
}
div + div {
  clear: both;
  border-top: 1px dashed silver;
  padding-top:1ex;
}
div > div::after {
  background-color: #000;
  color: #fefefe;
  margin: 1ex;
  padding: 1ex;
  opacity: 0.8;
  display: block;
  width: 10ex;
  font-size: 0.7em;
  content: attr(class);
}
&#13;
<div>
  <div class="contain"></div>
  <p>Note the grey background. The image does not cover the whole region, but it's fully <em>contained</em>.
  </p>
</div>
<div>
  <div class="cover"></div>
  <p>Note the ducks/geese at the bottom of the image. Most of the water is cut, as well as a part of the sky. You don't see the complete image anymore, but neither do you see any background color; the image <em>covers</em> all of the <code>&lt;div&gt;</code>.</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:32)

根据https://developer.mozilla.org/en-US/docs/CSS/background-size的提示,我最终得到了以下适合我的食谱

body {
        overflow-y: hidden ! important;
        overflow-x: hidden ! important;
        background-color: #f8f8f8;
        background-image: url('index.png');
        /*background-size: cover;*/
        background-size: contain;
        background-repeat: no-repeat;
        background-position: right;
}

答案 2 :(得分:11)

我不确定你到底在寻找什么,但是你真的应该看看这些由Chris Coyier写的CSS-Tricks的优秀博客文章:

http://css-tricks.com/how-to-resizeable-background-image/

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

阅读每篇文章的说明,看看它们是否适合您。

第一个回答了以下问题:

  

有没有办法让背景图片可以调整大小?同样,无论浏览器窗口的大小如何,都要使用图像边缘到边缘填充网页的背景。此外,随着浏览器窗口的更改,请将其调整为更大或更小。此外,确保它保持其比例(不会拉伸怪异)。此外,不会导致滚动条,只是在需要时垂直切断。此外,在页面上显示为内联标记。

第二篇文章的目标是获得以下内容,“网站上的背景图片始终覆盖整个浏览器窗口。”

希望这有帮助。

答案 3 :(得分:7)

试试这个,

element.style {
    background: rgba(0, 0, 0, 0) url("img/shopping_bgImg.jpg") no-repeat scroll center center / cover;
}

答案 4 :(得分:6)

只需添加以下一行:

    .your-class {
        height: 100vh;
    }

vh是视口高度。 这将自动缩放以适合设备&#39;浏览器窗口。

点击此处查看更多信息:Make div 100% height of browser window

答案 5 :(得分:6)

背景图片不是设置完美然后他的css是问题创建所以他的css文件更改为下面的代码

html {  
  background-image: url("example.png");  
  background-repeat: no-repeat;  
  background-position: 0% 0%;
  background-size: 100% 100%;
}

%; background-size:100%100%;“

答案 6 :(得分:2)

我遇到了同样的问题,在调整浏览器尺寸时无法调整图片大小。

错误代码:

html {  
  background-color: white;  
  background-image: url("example.png");  
  background-repeat: no-repeat;  
  background-attachment: scroll;  
  background-position: 0% 0%;
}


好的代码:

html {  
  background-color: white;  
  background-image: url("example.png");  
  background-repeat: no-repeat;  
  background-attachment: scroll;  
  background-position: 0% 0%;
  background-size: contain;
}

这里的关键是添加这个元素 - &gt; background-size:contains;

答案 7 :(得分:2)

body{
    background-image: url(../url/imageName.jpg);
    background-attachment: fixed;
    background-size: auto 100%;
    background-position: center;
}

答案 8 :(得分:1)

这对我有用:

background-size: auto 100%;

答案 9 :(得分:-2)

设置背景大小没有帮助,以下解决方案对我有用:

.class {
    background-image: url(blablabla.jpg);
    /* Add this */
    height: auto;
}

它基本上会裁剪图像并使其适应,background-size: contain/cover仍然无法使其适合。

相关问题