﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-有时天晴有时雨-随笔分类-CSS</title><link>http://www.blogjava.net/rorely/category/41973.html</link><description>学习足迹</description><language>zh-cn</language><lastBuildDate>Tue, 06 Oct 2009 03:48:51 GMT</lastBuildDate><pubDate>Tue, 06 Oct 2009 03:48:51 GMT</pubDate><ttl>60</ttl><item><title>CSS背景全攻略</title><link>http://www.blogjava.net/rorely/archive/2009/10/06/297291.html</link><dc:creator>期待明天</dc:creator><author>期待明天</author><pubDate>Tue, 06 Oct 2009 01:49:00 GMT</pubDate><guid>http://www.blogjava.net/rorely/archive/2009/10/06/297291.html</guid><wfw:comment>http://www.blogjava.net/rorely/comments/297291.html</wfw:comment><comments>http://www.blogjava.net/rorely/archive/2009/10/06/297291.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rorely/comments/commentRss/297291.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rorely/services/trackbacks/297291.html</trackback:ping><description><![CDATA[<p>原文链接：<a title="Backgrounds In CSS: Everything You Need To Know" rel="bookmark" href="http://www.smashingmagazine.com/2009/09/02/backgrounds-in-css-everything-you-need-to-know/">Backgrounds In CSS: Everything You Need To Know</a></p>
<p>译文链接：<a href="http://www.qianduan.net/everthing-about-css-background.html">css 背景全攻略</a></p>
<p>转载请保留版权以及链接</p>
<p>——————————————————————————————</p>
<p>背景(background)是css中一个重要的的部分，也是需要知道的css的基础知识之一。这篇文章将会涉及css背景
(background)的基本用法，包括诸如 background-attachment
等的属性，也会介绍一些有关背景(background)的常用技巧，以及 css3 中的
背景(background)(包含4个新的背景(background)属性)。</p>
<h3>css2 中的背景(background)</h3>
<h4>概述</h4>
<p>CSS2 中有5个主要的背景(background)属性，它们是：</p>
<p style="padding-left: 30px;">* background-color: 指定填充背景的颜色。</p>
<p style="padding-left: 30px;">* background-image: 引用图片作为背景。</p>
<p style="padding-left: 30px;">* background-position: 指定元素背景图片的位置。</p>
<p style="padding-left: 30px;">* background-repeat: 决定是否重复背景图片。</p>
<p style="padding-left: 30px;">* background-attachment: 决定背景图是否随页面滚动。</p>
<p>这些属性可以全部合并为一个缩写属性: background。需要注意的一个要点是背景占据元素的所有内容区域，包括 padding 和
border，但是不包括元素的 margin。它在 Firefox, Safari ,Opera 以及 IE8 中工作正常，但是 IE6 和
IE7 中，background 没把 border 计算在内。</p>
<p><img alt="Background does not extend to the borders in IE7 and IE6." src="http://www.kuqin.com/upimg/allimg/090910/0032040.jpg" /></p>
<h4>基本属性</h4>
<h4>背景色(background-color)</h4>
<p>background-color 属性用纯色来填充背景。有许多方式指定这个颜色，以下方式都得到相同的结果。</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-color: blue;<br />
<br />
<br />
<br />
background-color: #0000ff;<br />
<br />
<br />
<br />
background-color: #0000ff;</pre>
</div>
</div>
<p>background-color 也可被设置为透明（transparent），这会使得其下的元素可见。</p>
<h4>背景图(background-image)</h4>
<p>background-image 属性允许指定一个图片展示在背景中。可以和 background-color
连用，因此如果图片不重复地话，图片覆盖不到地地方都会被背景色填充。代码很简单，只需要记住，路径是相对于样式表的，因此以下的代码中，图片和样式表是
在同一个目录中的。</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-image: url(image.jpg);</pre>
</div>
</div>
<p>但是如果图片在一个名为 images 的子目录中，就应该是：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-image: url(images/image.jpg);</pre>
</div>
</div>
<p>糖伴西红柿：使用 ../ 表示上一级目录，比如 background-image: url(../images/image.jpg); 表示图片位于样式表的上级目录中的 images 子目录中。有点绕，不过这个大家应该都知道了，我就不详说了。</p>
<h4>背景平铺(background-repeat)</h4>
<p>设置背景图片时，默认把图片在水平和垂直方向平铺以铺满整个元素。这也许是你需要的，但是有时会希望图片只出现一次，或者只在一个方向平铺。以下为可能的设置值和结果：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-repeat: repeat; /* 默认值，在水平和垂直方向平铺 */<br />
<br />
<br />
<br />
background-repeat: no-repeat; /* 不平铺。图片只展示一次。 */<br />
<br />
<br />
<br />
background-repeat: repeat-x; /* 水平方向平铺(沿 x 轴) */<br />
<br />
<br />
<br />
background-repeat: repeat-y; /* 垂直方向平铺(沿 y 轴) */<br />
<br />
<br />
<br />
background-repeat: inherit; /* 继承父元素的 background-repeat 属性*/</pre>
</div>
</div>
<h4>背景定位(background-position)</h4>
<p>background-position 属性用来控制背景图片在元素中的位置。技巧是，实际上指定的是图片左上角相对于元素左上角的位置。<br />
下
面的例子中，设置了一个背景图片并且用 background-position 属性来控制它的位置，同时也设置了
background-repeat 为 no-repeat。计量单位是像素。第一个数字表示 x 轴(水平)位置，第二个是 y 轴(垂直) 位置。</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">/* 例 1: 默认值 */<br />
<br />
<br />
<br />
background-position: 0 0; /* 元素的左上角 */<br />
<br />
<br />
<br />
&nbsp;<br />
<br />
<br />
<br />
/* 例 2: 把图片向右移动 */<br />
<br />
<br />
<br />
background-position: 75px 0;<br />
<br />
<br />
<br />
&nbsp;<br />
<br />
<br />
<br />
/* 例 3: 把图片向左移动 */<br />
<br />
<br />
<br />
background-position: -75px 0;<br />
<br />
<br />
<br />
&nbsp;<br />
<br />
<br />
<br />
/* 例 4: 把图片向下移动 */<br />
<br />
<br />
<br />
background-position: 0 100px;</pre>
</div>
</div>
<p><img alt="The image can be set to any position you like." src="http://www.kuqin.com/upimg/allimg/090910/0032041.jpg" /></p>
<p>background-position 属性可以用其它数值，关键词和百分比来指定，这比较有用，尤其是在元素尺寸不是用像素设置时。</p>
<p>关键词是不用解释的。x 轴上：</p>
<ul>
    <li>* left </li>
    <li>* center </li>
    <li>* right </li>
</ul>
<p>y 轴上:</p>
<ul>
    <li>* top </li>
    <li>* center </li>
    <li>* bottom </li>
</ul>
<p>顺序方面和使用像素值时的顺序几乎一样，首先是 x 轴，其次是 y 轴，像这样：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-position: top right;</pre>
</div>
</div>
<p>使用百分数时也类似。需要主要的是，使用百分数时，浏览器是以元素的百分比数值来设置图片的位置的。看例子就好理解了。假设设定如下：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-position: 100% 50%;</pre>
</div>
</div>
<p>This goes 100% of the way across the image (i.e. the very right-hand
edge) and 100% of the way across the element (remember, the starting
point is always the top-left corner), and the two line up there. It
then goes 50% of the way down the image and 50% of the way down the
element to line up there. The result is that the image is aligned to
the right of the element and exactly half-way down it.</p>
<p>使用百分数定位时，其实是将背景图片的百分比指定的位置和元素的百分比位置对齐。也就是说，百分数定位是改变了背景图和元素的对齐基点。不再像使用
像素和关键词定位时，使用背景图和元素的左上角为对齐基点。例如上例的 background-position: 100% 50%;
就是将背景图片的 100%(right) 50%(center) 这个点，和元素的 100%(right) 50%(center) 这个点对齐。</p>
<p>这再一次说明了，我们一直认为已经掌握的简单的东西，其实还有我们有限的认知之外的知识。</p>
<p><span style="text-decoration: line-through;">注意原点总是左上角，</span>最终的效果是笑脸图片被定位在元素的最右边，离元素顶部是元素的一半，效果和 background-position: right center; 一样。</p>
<p><img alt="The smiley face is aligned as it would be if it was set to right center." src="http://www.kuqin.com/upimg/allimg/090910/0032042.jpg" /></p>
<h4>背景附着</h4>
<p>background-attachment 属性决定用户滚动页面时图片的状态。三个可用属性为 scroll(滚动)，fixed(固定)
和 inherit(继承)。inherit 单纯地指定元素继承他的父元素的 background-attachment 属性。</p>
<p>为了正确地理解 background-attachment，首先需要明白页面(page)和视口(view port)是如何协作地。视口(view port)是浏览器显示网页的部分(就是去掉工具栏的浏览器)。视口(view port)的位置固定，不变动。</p>
<p>当向下滚动网页时，视口(view port)是不动的，而页面的内容向上滚动。看起来貌似视口(view
port)向页面下方滚动了。如果设置 background-attachment:
scroll，就设置了当元素滚动时，元素背景也必需随着滚动。简而言之，背景是紧贴元素的。这是 background-attachment
默认值。</p>
<p>用一个例子来更清楚地描述下：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-image: url(test-image.jpg);<br />
<br />
<br />
<br />
&nbsp;<br />
<br />
<br />
<br />
background-position: 0 0;<br />
<br />
<br />
<br />
background-repeat: no-repeat;<br />
<br />
<br />
<br />
background-attachment: scroll;</pre>
</div>
</div>
<p><img alt="" src="http://www.kuqin.com/upimg/allimg/090910/0032043.jpg" /></p>
<p>当向下滚动页面时，背景向上滚动直至消失。</p>
<p>但是当设置 background-attachment 为 fixed 时，当页面向下滚动时，背景要待在它原来的位置(相对于浏览器来说)。也就是不随元素滚动。</p>
<p>用另一个例子描述下：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-image: url(test-image.jpg);<br />
<br />
<br />
<br />
background-position: 0 100%;<br />
<br />
<br />
<br />
background-repeat: no-repeat;<br />
<br />
<br />
<br />
background-attachment: fixed;</pre>
</div>
</div>
<p><img alt="We have scrolled down the page here, but the image remains visible." src="http://www.kuqin.com/upimg/allimg/090910/0032044.jpg" /></p>
<p>页面已经向下滚动了，但是图像仍然保持可见。</p>
<p>需要重视的一点是背景图只能出现在它父元素能达到的区域。即使图片是相对于视口(view port)定位地，如果它的父元素不可见，图片就会消失。参见下面的例子。此例中，图片位于视口(view port)的左下方，但是只有元素内的图片部分是可见的。</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-image: url(test-image.jpg);<br />
<br />
<br />
<br />
background-position: 0 100%;<br />
<br />
<br />
<br />
background-repeat: no-repeat;<br />
<br />
<br />
<br />
background-attachment: fixed;</pre>
</div>
</div>
<p><img alt="Part of the image has been cut off because it begins outside of the element." src="http://www.kuqin.com/upimg/allimg/090910/0032045.jpg" /></p>
<p>因为图片开始在元素之外，一部分图片被切除了。</p>
<h4>背景的简写属性</h4>
<p>可以把背景的各个属性合为一行，而不用每次都单独把他们写出来。格式如下：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background: <span style="color: #ddbb00;">&amp;lt;</span>color<span style="color: #ddbb00;">&amp;gt;</span> <span style="color: #ddbb00;">&amp;lt;</span>image<span style="color: #ddbb00;">&amp;gt;</span> <span style="color: #ddbb00;">&amp;lt;</span>position<span style="color: #ddbb00;">&amp;gt;</span> <span style="color: #ddbb00;">&amp;lt;</span>attachment<span style="color: #ddbb00;">&amp;gt;</span> <span style="color: #ddbb00;">&amp;lt;</span>repeat<span style="color: #ddbb00;">&amp;gt;</span></pre>
</div>
</div>
<p>例如，下面的声明</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-color: transparent;<br />
<br />
<br />
<br />
background-image: url(image.jpg);<br />
<br />
<br />
<br />
background-position: 50% 0 ;<br />
<br />
<br />
<br />
background-attachment: scroll;<br />
<br />
<br />
<br />
background-repeat: repeat-y;</pre>
</div>
</div>
<p>可以合为单独一行：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background: transparent url(image.jpg) 50% 0 scroll repeat-y;</pre>
</div>
</div>
<p>而且不需要指定每一个值。如果省略值地话，就使用属性地默认值。例如，上面那行和下面这个效果一样：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background: url(image.jpg) 50% 0 repeat-y;</pre>
</div>
</div>
<h3>背景的一般用法</h3>
<p>除了可以用来使元素更加优雅这类显然的用法之外，背景也可以用于其它的目的。</p>
<h4>仿栏</h4>
<p>当使用 css 的 float 属性来定位布局元素时，要确保两栏或多栏有相同的长度是比较困难的。如果长度不同，其中一栏的背景会比另外的短，这会破坏整个设计。</p>
<p>仿栏是个非常简单的背景技巧，这个技巧最早发表在<a href="http://www.alistapart.com/articles/fauxcolumns/">A List Apart</a> 。思路很简单：不再给每列单独设置背景，而是给各列的父元素设置一个背景图。所有栏的设计都包含在这张图片之中。</p>
<h4>文本替换</h4>
<p>在网页上，对于字体的选择是相当有限的。可以使用 sIFR 之类的工具来定制字体，但是这需要用户启用 JavaScript
。一个适用于任意浏览器的简单方法是，用想用的字体来做一张文本图片，并用这张图片作为背景。这样，文本依然出现在文档标记中以供搜索引擎检索和屏幕浏览
器识别，但是在浏览器中就会显示首选的字体。</p>
<p>例如，HTML 标记可能是这样的：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;"><span style="color: #009900;">&lt;<span style="font-weight: bold; color: #000000;">h3</span>&gt;</span>Blogroll<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="font-weight: bold; color: #000000;">h3</span>&gt;</span></pre>
</div>
</div>
<p>假如有一个 200 乘 75 的图片，上面有更好看的字体，就可以用如下方式来替换文本：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">h3.blogroll {<br />
<br />
<br />
<br />
width: 200px;<br />
<br />
<br />
<br />
height: 75px; /* So that the element will show the whole image. */<br />
<br />
<br />
<br />
background:url(blogroll-text.jpg) 0 0 no-repeat; /* Sets the background image */<br />
<br />
<br />
<br />
text-indent: -9999px; /* Hides the regular text by moving it 9999 pixels to the left */<br />
<br />
<br />
<br />
}</pre>
</div>
</div>
<h4>简单的圆点</h4>
<p>无需列表中的圆点看起来很难看。不用再处理所有不同的 list-style 属性，只需要简单地把他们隐藏并用背景图代替就可以了。因为图片可以随意选择，这些圆点就可以看起来更漂亮。</p>
<p>下面，我们把一个无需列表改造成有圆滑圆点的：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">ul {<br />
<br />
<br />
<br />
list-style: none; /* Removes default bullets. */<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
&nbsp;<br />
<br />
<br />
<br />
ul li {<br />
<br />
<br />
<br />
padding-left: 40px; /* Indents list items, leaving room for background image on the left. */<br />
<br />
<br />
<br />
background: url(bulletpoint.jpg) 0 0 no-repeat;<br />
<br />
<br />
<br />
}</pre>
</div>
</div>
<h3>CSS3 中的背景</h3>
<p>CSS3 中的背景有较多改进。最显著的是多背景图片的选项，同时也增加了4个新属性。</p>
<h4>多背景</h4>
<p>CSS3 中，可以对一个元素应用一个或多个图片作为背景。代码和 css2 中的一样，只需要用逗号来区别各个图片。第一个声明的图片定位在元素顶部，其它的图片按序在其下排列，例如：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-image: url(top-image.jpg), url(middle-image.jpg), url(bottom-image.jpg);</pre>
</div>
</div>
<h4>新属性：背景修剪(background-clip)</h4>
<p>这又把我们带回了文章开始讨论的那个关于边框内图片显示的话题。它被描述为&#8220;背景描绘区&#8221;。</p>
<p>background-clip 属性用来增强背景显示位置的控制能力。可能的值为：</p>
<p>* background-clip: border-box;<br />
背景显示在边框内。<br />
* background-clip: padding-box;<br />
背景显示在内补白(padding)内，而不是边框内。<br />
* background-clip: content-box;<br />
只在内容内显示背景，而不是内补白(padding)和边框内。<br />
* background-clip: no-clip;<br />
默认值，和 border-box 一样。</p>
<h4>新属性：背景原点(background-origin)</h4>
<p>这个属性和 background-position 结合起来使用。可以从边框，内补白或者内容盒子开始计算 background-position (类似于 background-clip)。</p>
<p>* background-origin: border-box;<br />
以边框为原点开始计算 background-position.<br />
* background-origin: padding-box;<br />
以内补白为原点开始计算 background-position<br />
* background-origin: content-box;<br />
以内容盒子为原点开始计算 background-position<br />
对于 background-clip 和 background-origin 不同的一个解释参看 <a href="http://www.css3.info/preview/background-origin-and-background-clip/">CSS3.info</a></p>
<h4>新属性： 背景尺寸(background-size)</h4>
<p>background-size 用来调整背景图的大小。有好几个可能值：</p>
<p>* background-size: contain;<br />
缩小图片来适应元素的尺寸(保持像素的长宽比)<br />
* background-size: cover;<br />
扩展图片来填满元素(保持像素的长宽比)<br />
* background-size: 100px 100px;<br />
调整图片到指定大小<br />
* background-size: 50% 100%;<br />
调整图片到指定大小。百分比是相对于包含元素的尺寸的。</p>
<p>可以看一下 <a href="http://www.w3.org/TR/css3-background/#background-size">CSS3规则</a> 网站上的几个例子。</p>
<h4>新属性：(background-break)</h4>
<p>CSS3 中，元素可以被分成几个独立的盒子(例如 使内联元素 span 跨越多行)。background-break 属性用来控制背景怎样在这些不同的盒子中显示。</p>
<p>可能值为：</p>
<p>* Background-break: continuous;<br />
默认值。忽略盒之间的距离(也就是像元素没有分成多个盒子，依然是一个整体一样)<br />
* Background-break: bounding-box;<br />
把盒之间的距离计算在内<br />
* Background-break: each-box;<br />
为每个盒子单独重绘背景</p>
<h4>背景色(background-color)的改进</h4>
<p>background-color 在 css3 中有了稍许改进。除了设置背景颜色之外，如果元素底层的背景图不可用，还可以设置一个&#8220;回退色&#8221;。</p>
<p>通过在回退色之前增加一个斜杠(/)来实现，例如：</p>
<div style="padding-bottom: 0px;">
<div>
<pre style="font-family: monospace;">background-color: green / blue;</pre>
</div>
</div>
<p>此例中，背景色应该是绿色(green)。然而，如果底层背景图不能使用的话，背景色就是蓝色而不是绿色。如果在斜杠前不指定颜色，默认为透明(transparent)。</p>
<h4>背景平铺(background-repeat)的改进</h4>
<p>CSS2中当图片平铺时，会被元素在末端截断。CSS3 引入了两个属性来修正这个问题:</p>
<p>* space: 应用同等数量的空白到图片之间，直到填满整个元素<br />
* round: 缩小图片直到正好平铺满元素<br />
关于 background-repeat: space; 的一个例子，可以在<a href="http://www.w3.org/TR/css3-background/#background-repeat"> CSS3 规则</a>网站看到。</p>
<h4>背景附着(background-attachment)的改进</h4>
<p>background-attachment 属性增加了一个新值：local。这是用来配合可以滚动的元素的(设置为 overflow:
scroll; 的元素)。当 background-attachment 设置为滚动(scroll)时，背景图不会随元素内容的滚动而滚动。</p>
<p>设置为 background-attachment :local; 时，背景图会随内容的滚动而滚动。</p>
<h3>总结</h3>
<p>总结一下，css 中关于背景有许多需要知道的知识。但是一旦把这些知识融会贯通了，这些技术和命名约定就变得非常有意义而且很快就会成为潜意识行为了。</p>
<p>如果刚接触 css，主要不断联系就可以较快地掌握背景的要点了。如果是老手，我希望你可以和我一样期待 css3 。</p>
<h4>关于作者</h4>
<p>Michael Martin 的文章大多涉及网页设计，WordPress 并为 <a href="http://www.problogdesign.com/">Pro Blog Design </a>工作。可以查看更多关于博客设计的文章或者在 <a href="http://twitter.com/problogdesign">twitter</a> 上关注他。</p>
<p>糖伴西红柿：一看到这么长篇大论的也头晕，花了好几天的时间折腾这篇文章。全是基础的只是，没有什么花哨的，但是我觉得最基础的也是最重要的。</p>
<p>鉴于好多网站都会转载别人的文章，当然了，好的文章我们也会转载，不过有些人直接把别人辛辛苦苦的劳动付出拿来当自己的东西。</p>
<p>这些人真的很无耻，这个大环境也真的很悲哀，没人愿意踏踏实实地做事。这就是为什么我们总是翻译国外的文章，而不是把咱们的文章翻译成外语让外国人看的根本所在。</p>
<p>可悲啊。</p>
<img src ="http://www.blogjava.net/rorely/aggbug/297291.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rorely/" target="_blank">期待明天</a> 2009-10-06 09:49 <a href="http://www.blogjava.net/rorely/archive/2009/10/06/297291.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>