如何拉伸背景图像以填充div

时间:2012-06-27 09:56:00

标签: css html background background-image

我想将背景图片设置为不同的div,但我的问题是:

  1. 图像尺寸固定(60像素)。
  2. 改变div的大小
  3. 如何拉伸背景图像以填充div的整个背景?

    #div2{
      background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
      height:180px;
      width:200px;
      border: 1px solid red;
    }
    

    Check the code here.

9 个答案:

答案 0 :(得分:125)

添加

background-size:100% 100%;

在background-image下面的css。

您还可以指定确切的尺寸,即:

background-size: 30px 40px;

此处:JSFiddle

答案 1 :(得分:67)

您可以使用:

background-size: cover;

或者只使用大背景图片:

background: url('../images/teaser.jpg') no-repeat center #eee;

答案 2 :(得分:40)

现代CSS3(推荐用于未来和最佳解决方案)

.selector{
   background-size: cover;
   /* streches background WITHOUT deformation so it would fill the background space,
      it may crop the image if the image's dimensions are in different ratio,
      than the element dimensions. */
}

<强>最大。拉伸没有裁剪也不变形(可能不会填充背景。)background-size: contain;
强制绝对拉伸(可能导致变形,但没有裁剪) background-size: 100% 100%;

&#34;旧&#34; CSS&#34;始终在工作&#34;方式

将绝对定位图像作为(相对定位)父级的第一个子级并将其拉伸到父级大小。

<强> HTML

<div class="selector">
   <img src="path.extension" alt="alt text">
   <!-- some other content -->
</div>

相当于CSS3 background-size: cover;

要动态实现这一点,您必须使用包含方法替代(见下文),如果您需要将裁剪后的图像居中,则需要使用JavaScript来动态执行此操作 - 例如使用jQuery:

$('.selector img').each(function(){ 
   $(this).css({ 
      "left": "50%", 
      "margin-left": "-"+( $(this).width()/2 )+"px", 
      "top": "50%", 
      "margin-top": "-"+( $(this).height()/2 )+"px" 
   }); 
});

实际例子:
css crop like example

相当于CSS3 background-size: contain;

这个可能有点棘手 - 你的背景尺寸会溢出父母将CSS设置为100%而另一个设置为auto。 实际例子: css streching background as image

.selector img{
   position: absolute; top:0; left: 0;
   width: 100%;
   height: auto;
   /* -- OR -- */
   /* width: auto; 
      height: 100%; */
}

相当于CSS3 background-size: 100% 100%;

.selector img{
   position: absolute; top:0; left: 0;
   width: 100%;
   height: 100%;
}

PS:在&#34; old&#34;中执行覆盖/包含的等价物完全动态的方式(所以你不必关心溢出/比率)你必须使用javascript来检测你的比率并将尺寸设置为描述......

答案 3 :(得分:24)

为此,您可以使用CSS3 background-size属性。写得像这样:

#div2{
    background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
    -moz-background-size:100% 100%;
    -webkit-background-size:100% 100%;
    background-size:100% 100%;
    height:180px;
    width:200px;
    border: 1px solid red;
}

请检查:http://jsfiddle.net/qdzaw/1/

答案 4 :(得分:20)

您可以添加:

#div2{
    background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
    background-size: 100% 100%;
    height:180px;
    width:200px;
    border: 1px solid red;
}

您可以在此处详细了解:css3 background-size

答案 5 :(得分:2)

body{
  margin:0;
  background:url('image.png') no-repeat 50% 50% fixed;
  background-size: cover;
}

答案 6 :(得分:1)

使用: 背景大小:100%100%; 使背景图像适合div大小。

答案 7 :(得分:0)

要保持宽高比,请使用background-size: 100% auto;

div {
    background-image: url('image.jpg');
    background-size: 100% auto;
    width: 150px;
    height: 300px;
}

答案 8 :(得分:0)

尝试这样的事情:

div {
    background-image: url(../img/picture1.jpg);
    height: 30em;
    background-repeat: no-repeat;
    width: 100%;
    background-position: center;
}