/* Global values */ background-clip: inherit; background-clip: initial; background-clip: unset;
值的说明: border-box: 背景扩展到元素的border外边沿,Z轴上是显示在下方。
padding-box: 背景扩展到元素的padding外边沿,border底下无背景绘制。
content-box: 背景绘制在内容区域(content)。
text: 试验性属性值,背景仅绘制在文本背景下。
Demo:
1 2 3 4
<pclass="border-box">The background extends behind the border.</p> <pclass="padding-box">The background extends to the inside edge of the border.</p> <pclass="content-box">The background extends only to the edge of the content box.</p> <pclass="text">The background is clipped to the foreground text.</p>
<div> <pclass="catsandstars"> This paragraph is full of cats<br />and stars. </p> <p>This paragraph is not.</p> <pclass="catsandstars"> Here are more cats for you.<br />Look at them! </p> <p>And no more.</p> </div>
div { background-image: url('logo.jpg'), url('mainback.png'); /* Applies two images to the background */ background-position: top right, 0px0px; background-origin: content-box, padding-box; }
/* Shared among all <div>s */ div { background-color: #FFEE99; background-repeat: no-repeat; width: 300px; height: 80px; margin-bottom: 12px; }
/* These examples use the `background` shorthand property */ .exampleone { background: url("https://mdn.mozillademos.org/files/11987/startransparent.gif") #FFEE992.5cm bottom no-repeat; } .exampletwo { background: url("https://mdn.mozillademos.org/files/11987/startransparent.gif") #FFEE993em50% no-repeat; }
/* Multiple background images: Each image is matched with the corresponding position, from first specified to last. */ .examplethree { background-image: url("https://mdn.mozillademos.org/files/11987/startransparent.gif"), url("https://mdn.mozillademos.org/files/7693/catfront.png"); background-position: 0px0px, center; }
As the allowed space increases in size, the repeated images will stretch (leaving no gaps) until there is room (space left >= half of the image width) for another one to be added. When the next image is added, all of the current ones compress to allow room. Example: An image with an original width of 260px, repeated three times, might stretch until each repetition is 300px wide, and then another image will be added. They will then compress to 225px.(暂时不能够完全理解)
/* Shared for all DIVS in example */ ol, li { margin: 0; padding: 0; } li { margin-bottom: 12px; } div { background-image: url(https://mdn.mozillademos.org/files/12005/starsolid.gif); width: 160px; height: 70px; }
/* One-value syntax */ /* the width of the image (height becomes 'auto') */ background-size: 50%; background-size: 3.2em; background-size: 12px; background-size: auto;
/* Two-value syntax */ /* first value: width of the image, second value: height */ background-size: 50% auto; background-size: 3em25%; background-size: auto 6px; background-size: auto auto;
/* Multiple backgrounds */ background-size: auto, auto; /* Not to be confused with `auto auto` */ background-size: 50%, 25%, 25%; background-size: 6px, auto, contain;
/* Global values */ background-size: inherit; background-size: initial; background-size: unset;
local 背景相对元素的内容固定。如果元素滚动,则背景会随着元素的内容滚动,背景可绘制区域和背景定位区域是相对于元素的可滚动区域,而不是边框区域。
scroll 背景相对于元素本身固定,并且不会随着内容滚动。
Demo
HTML
1 2 3 4 5 6
<p> There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. </p>
CSS
1 2 3 4
p { background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"); background-attachment: fixed; }