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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11223585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:18:11  来源:igfitidea点击:

How to stretch the background image to fill a div

csshtmlbackgroundbackground-image

提问by Muhammad Usman

I want to set a background image to different divs, but my problems are:

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

  1. The size of image is fixed(60px).
  2. Varying div's size
  1. 图像的大小是固定的(60px)。
  2. 改变div的大小

How can I stretch the background-image to fill the whole background of the div?

如何拉伸背景图像以填充 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.

检查这里的代码。

回答by hendos43

Add

添加

background-size:100% 100%;

to your css underneath background-image.

到背景图像下方的 css。

You can also specify exact dimensions, i.e.:

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

background-size: 30px 40px;

Here: JSFiddle

这里:JSFiddle

回答by Ouadie

You can use:

您可以使用:

background-size: cover;

Or just use a big background image with:

或者只是使用一个大背景图像:

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

回答by jave.web

Modern CSS3 (recommended for the future & probably the best solution)

现代 CSS3(推荐用于未来,可能是最好的解决方案)

.selector{
   background-size: cover;
   /* stretches 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. */
}

Max. stretch without crop nor deformation (may not fill the background):background-size: contain;
Force absolute stretch (may cause deformation, but no crop):background-size: 100% 100%;

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

"Old" CSS "always working" way

“旧” CSS“始终有效”的方式

Absolute positioning image as a first child of the (relative positioned)parent and stretching it to the parent size.

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

HTML

HTML

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

Equivalent of CSS3 background-size: cover;:

等效于 CSS3 background-size: cover;

To achieve this dynamically, you would have to use the opposite of contain method alternative(see below) and if you need to center the cropped image, you would need a JavaScript to do that dynamically - e.g. using jQuery:

要动态实现这一点,您必须使用相反的包含方法替代(见下文),如果您需要将裁剪图像居中,您将需要一个 JavaScript 来动态地做到这一点 - 例如使用 jQuery:

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

Practical example:
css crop like example

实际例子:
css裁剪示例

Equivalent of CSS3 background-size: contain;:

等效于 CSS3 background-size: contain;

This one can be a bit tricky - the dimension of your background that would overflow the parent will have CSS set to 100% the other one to auto. Practical example:css stretching background as image

这个可能有点棘手 - 会溢出父级的背景尺寸会将 CSS 设置为 100%,另一个设置为自动。 实际例子:css拉伸背景作为图像

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

Equivalent of CSS3 background-size: 100% 100%;:

等效于 CSS3 background-size: 100% 100%;

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

PS: To do the equivalents of cover/contain in the "old" way completely dynamically (so you will not have to care about overflows/ratios) you would have to use javascript to detect the ratios for you and set the dimensions as described...

PS:要完全动态地以“旧”方式执行cover/contain的等效操作(因此您不必关心溢出/比率),您必须使用javascript为您检测比率并按照所述设置尺寸。 ..

回答by sandeep

For this you can use CSS3 background-sizeproperty. Write like this:

为此,您可以使用 CSS3background-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;
}

Check this: http://jsfiddle.net/qdzaw/1/

检查这个:http: //jsfiddle.net/qdzaw/1/

回答by Xella

You can add:

你可以加:

#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;
}

You can read more about it here: css3 background-size

您可以在此处阅读更多相关信息:css3 background-size

回答by Kursat Turkay

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

回答by MrBii

by using property css:

通过使用属性 css:

background-size: cover;

回答by jagan reddy

Use: background-size: 100% 100%; To make background image to fit the div size.

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

回答by Atul Yadav

To keep the aspect ratio, use background-size: 100% auto;

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

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

回答by Deejay

Try something like this:

尝试这样的事情:

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