background-size

这篇翻译不完整。请帮忙从英语翻译这篇文章

background-size 设置背景图片大小。

/* 关键字 */
background-size: cover
background-size: contain

/* 一个值: 这个值指定图片的宽度,图片的高度隐式的为auto */
background-size: 50%
background-size: 3em
background-size: 12px
background-size: auto

/* 两个值 */
/* 第一个值指定图片的宽度,第二个值指定图片的高度 */
background-size: 50% auto
background-size: 3em 25%
background-size: auto 6px
background-size: auto auto

/* 逗号分隔的多个值:设置多重背景 */
background-size: auto, auto     /* 不同于background-size: auto auto */
background-size: 50%, 25%, 25%
background-size: 6px, auto, contain

/* Global values */
background-size: inherit;
background-size: initial;
background-size: unset;

初始值auto auto
适用元素all elements. It also applies to ::first-letter and ::first-line.
是否是继承属性
Percentagesrelative to the background positioning area
适用媒体visual
计算值as specified, but with relative lengths converted into absolute lengths
Animation typerepeatable list of simple list of length, percentage, or calc
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

The background size for a single image can be specified in one of three ways:

  • using the keyword contain
  • using the keyword cover
  • using width and height values

To specify a size using width and height, you can supply one or two values:

  • If only one value is given it sets the width, with the height set to auto.
  • If two values are given, the first sets width and the second sets height.

Each value can be a <length>, a <percentage>, or auto.

For example:

background-size: contain;

background-size: 50%;
background-size: 3em;

background-size: auto 1em;
background-size: 50% 25%;


To specify background size for more than one image, supply multiple sizes, separated by commas:

background-size: 50% 25%, contain, 3em;

属性值

<length>
<length> 值,指定背景图片大小,不能为负值。
<percentage>
<percentage> 值,指定背景图片相对背景区(background positioning area)的百分比。背景区由background-origin设置,默认为盒模型的内容区与内边距,也可设置为只有内容区,或者还包括边框。如果attachmentfixed,背景区为浏览器可视区(即视口),不包括滚动条。不能为负值。
auto
以背景图片的比例缩放背景图片。
cover
缩放背景图片以完全覆盖背景区,可能背景图片部分看不见。A keyword that is the inverse of contain. Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container. When the image and container have different dimensions, the image is clipped either left/right or top/bottom.
contain
缩放背景图片以完全装入背景区,可能背景区部分空白。A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). Image is letterboxed within the container. When the image and container have different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color.

位图一定有固有尺寸与固有比例,矢量图可能两者都有,也可能只有一个。渐变视为只有固有尺寸或者只有固有比例的图片。

Gecko 8.0 note
(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)

This behavior changed in Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5). Before this, gradients were treated as images with no intrinsic dimensions, with an intrinsic proportion identical to that of the background positioning area.

-moz-element 生成的背景图片,(which actually match an element) are currently treated as images with the dimensions of the element, or of the background positioning area if the element is SVG, with the corresponding intrinsic proportion.

Note: This is not the currently-specified behavior, which is that the intrinsic dimensions and proportion should be those of the element in all cases.

背景图片大小计算:

如果指定了 background-size 的两个值并且不是auto
背景图片按指定大小渲染。
containcover:
保留固有比例,最大的包含或覆盖背景区。如果图像没有固有比例,则按背景区大小。
autoauto auto:
图像如果有两个长度,则按这个尺寸。如果没有固有尺寸与固有比例,则按背景区的大小。如果没有固有尺寸但是有固有比例, 效果同 contain。如果有一个长度与比例,则由此长度与比例计算大小。如果有一个长度但是没有比例,则使用此长度与背景区相应的长度。
一个为 auto 另一个不是auto:
如果图像有固有比例,则指定的长度使用指定值,未指定的长度由指定值与固有比例计算。如果图像没有固有比例,则指定的长度使用指定值,未指定的长度使用图像相应的固有长度,若没有固有长度,则使用背景区相应的长度。

注意,对于没有固有尺寸或固有比例的矢量图不是所有的浏览器都支持。特别注意测试Firefox 7- 与Firefox 8+,以确定不同之处能否接受。

Formal syntax

如何阅读 CSS 语法。

<bg-size>#

where
<bg-size> = [ <length-percentage> | auto ]{1,2} | cover | contain

where
<length-percentage> = <length> | <percentage>

案例

background-size: cover 演示 与  background-size: contain 演示 在新窗口打开,这样你可以看到当背景区大小变化时 containcover 是怎样的。 系列演示:background-size 及其与background-*属性的关联  很好的说明了单独使用 background-size 及与其它属性共同使用。

备注

如果用渐变作为背景并且对它使用了background-size ,最好不要只用一个auto, 或者只指定一个宽度值 (例如 background-size: 50%)。对这两种情况 Firefox 8有所改变, 并且目前各浏览器表现不一致,不是全部浏览器都完全支持 CSS3 background-size 规范CSS3 Image Values gradient 规范

.bar {
       width: 50px; height: 100px;
       background-image: gradient(...);

       /* 不推荐 */
       background-size: 25px;
       background-size: 50%;
       background-size: auto 50px;
       background-size: auto 50%;

       /* 可行 */
       background-size: 25px 50px;
       background-size: 50% 50%;
}

特别不推荐对渐变px与auto一起用, 因为Firefox 8之前不能重复渲染,并且对于没有实现Firefox 8渲染特性的浏览器,不知道指定了背景的元素的确切大小。

规范

规范 状态 说明
CSS Backgrounds and Borders Module Level 3
background-size
Candidate Recommendation Initial definition

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support

1.0 -webkit- 1

3.01

12

3.6 -moz-

49 -webkit-

4.0

9.0

9.5 -o- 2

10

3.0 -webkit- 1

4.1

<contain> and <cover></cover></contain>3.0123.6910.04.1
SVG image as background44.0128.09.031.0?
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support2.3(Yes)(Yes)

1.0 -moz-

49 -webkit-

4.0

(Yes)(Yes)(Yes)
<contain> and <cover></cover></contain>??(Yes)(Yes)10.0?(Yes)
SVG image as background??(Yes)8.0?(Yes)?

1. WebKit-based browsers originally implemented an older draft of CSS3 background-size in which an omitted second value is treated as duplicating the first value; this draft does not include the contain or cover keywords.

2. Opera 9.5's computation of the background positioning area is incorrect for fixed backgrounds. Opera 9.5 also interprets the two-value form as a horizontal scaling factor and, from appearances, a vertical clipping dimension. This has been fixed in Opera 10.

Polyfills and Workarounds

For Internet Explorer prior IE8

Though Internet Explorer 8 doesn't support the background-size property, it is possible to emulate some of its functionality using the non-standard -ms-filter property:

-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='path_relative_to_the_HTML_file', sizingMethod='scale')";

This simulates the value cover.

In Firefox 3.5

While this property was added in Firefox 3.6, it is possible to stretch a image fully over the background in Firefox 3.5 by using -moz-border-image.

.foo {
  background-image: url(bg-image.png);

  -webkit-background-size: 100% 100%;           /* Safari 3.0 */
     -moz-background-size: 100% 100%;           /* Gecko 1.9.2 (Firefox 3.6) */
       -o-background-size: 100% 100%;           /* Opera 9.5 */
          background-size: 100% 100%;           /* Gecko 2.0 (Firefox 4.0) and other CSS3-compliant browsers */
 
  -moz-border-image: url(bg-image.png) 0;    /* Gecko 1.9.1 (Firefox 3.5) */
}

相关链接

文档标签和贡献者

标签: 
 此页面的贡献者: Ende93, fedwatch, Sebastianz, teoli, yan
 最后编辑者: Ende93,