Detailed explanation of examples of background image settings in css

黄舟
Release: 2017-07-22 09:22:32
Original
2618 people have browsed it

Background (background) is an important part of CSS, and it is also one of the basic knowledge of CSS that you need to know. This article will cover the basic usage of css background (background), including attributes such as background-attachment, etc., and also introduce some common techniques about background (background), as well as the background (background) in css3 (including 4 new the background property).

Background in css2

Overview

There are 5 main background properties in CSS2, they are:

* background-color: Specifies the color to fill the background.

* background-image: Reference the image as the background.

* background-position: Specify the position of the element's background image.

* background-repeat: Determines whether to repeat the background image.

* background-attachment: Determines whether the background image scrolls with the page.

These properties can all be combined into one abbreviated property: background. One important point to note is that the background occupies all of the element's content area, including padding and border, but does not include the element's margin. It works fine in Firefox, Safari, Opera and IE8, but in IE6 and IE7, the background does not include the border.


Basic properties

Background-color

The background-color property fills the background with a solid color. There are many ways to specify this color, and the following methods all give the same result.

background-color: blue;
background-color: rgb(0, 0, 255);
background-color: #0000ff;
Copy after login

background-color can also be set to transparent, which makes elements underneath it visible.

背景图(background-image)

background-image 属性允许指定一个图片展示在背景中。可以和 background-color 连用,因此如果图片不重复地话,图片覆盖不到地地方都会被背景色填充。代码很简单,只需要记住,路径是相对于样式表的,因此以下的代码中,图片和样式表是在同一个目录中的。

background-image: url(image.jpg);
Copy after login

但是如果图片在一个名为 images 的子目录中,就应该是:

background-image: url(images/image.jpg);
Copy after login

糖伴西红柿:使用 ../ 表示上一级目录,比如 background-image: url(../images/image.jpg); 表示图片位于样式表的上级目录中的 images 子目录中。有点绕,不过这个大家应该都知道了,我就不详说了。前端观察 版权所有,转载请保留链接。

背景平铺(background-repeat)

设置背景图片时,默认把图片在水平和垂直方向平铺以铺满整个元素。这也许是你需要的,但是有时会希望图片只出现一次,或者只在一个方向平铺。以下为可能的设置值和结果:

background-repeat: repeat; 
background-repeat: no-repeat; 
background-repeat: repeat-x; 
background-repeat: repeat-y; 
background-repeat: inherit;
Copy after login

背景定位(background-position)

background-position 属性用来控制背景图片在元素中的位置。技巧是,实际上指定的是图片左上角相对于元素左上角的位置。
下面的例子中,设置了一个背景图片并且用 background-position 属性来控制它的位置,同时也设置了 background-repeat 为 no-repeat。计量单位是像素。第一个数字表示 x 轴(水平)位置,第二个是 y 轴(垂直) 位置。

background-position: 0 0; 
 background-position: 75px 0;
 background-position: -75px 0;
 background-position: 0 100px;
Copy after login

background-position 属性可以用其它数值,关键词和百分比来指定,这比较有用,尤其是在元素尺寸不是用像素设置时。

关键词是不用解释的。x 轴上:

  • * left
    * center
    * right
    Copy after login

y 轴上:

  • * top
    * center
    * bottom
    Copy after login

顺序方面和使用像素值时的顺序几乎一样,首先是 x 轴,其次是 y 轴,像这样:

background-position: top right;
Copy after login

使用百分数时也类似。需要主要的是,使用百分数时,浏览器是以元素的百分比数值来设置图片的位置的。看例子就好理解了。假设设定如下:

background-position: 100% 50%;
Copy after login

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.

糖伴西红柿:这一段没想到合适的翻译,保留原文,意译。前端观察 版权所有,转载请保留链接。

update: 感谢天涯的指教,这段搞明白了。使用百分数定位时,其实是将背景图片的百分比指定的位置和元素的百分比位置对齐。也就是说,百分数定位是改变了背景图和元素的对齐基点。不再像使用像素和关键词定位时,使用背景图和元素的左上角为对齐基点。例如上例的 background-position: 100% 50%; 就是将背景图片的 100%(right) 50%(center) 这个点,和元素的 100%(right) 50%(center) 这个点对齐。

这再一次说明了,我们一直认为已经掌握的简单的东西,其实还有我们有限的认知之外的知识。

注意原点总是左上角,最终的效果是笑脸图片被定位在元素的最右边,离元素顶部是元素的一半,效果和 background-position: right center; 一样。


背景附着

background-attachment 属性决定用户滚动页面时图片的状态。三个可用属性为 scroll(滚动),fixed(固定) 和 inherit(继承)。inherit 单纯地指定元素继承他的父元素的 background-attachment 属性。

为了正确地理解 background-attachment,首先需要明白页面(page)和视口(view port)是如何协作地。视口(view port)是浏览器显示网页的部分(就是去掉工具栏的浏览器)。视口(view port)的位置固定,不变动。

当向下滚动网页时,视口(view port)是不动的,而页面的内容向上滚动。看起来貌似视口(view port)向页面下方滚动了。如果设置 background-attachment: scroll,就设置了当元素滚动时,元素背景也必需随着滚动。简而言之,背景是紧贴元素的。这是 background-attachment 默认值。

用一个例子来更清楚地描述下:

background-image: url(test-image.jpg);
 background-position: 0 0;
background-repeat: no-repeat;
background-attachment: scroll;
Copy after login

当向下滚动页面时,背景向上滚动直至消失。

但是当设置 background-attachment 为 fixed 时,当页面向下滚动时,背景要待在它原来的位置(相对于浏览器来说)。也就是不随元素滚动。

用另一个例子描述下:

background-image: url(test-image.jpg);
background-position: 0 100%;
background-repeat: no-repeat;
background-attachment: fixed;
Copy after login
Copy after login

页面已经向下滚动了,但是图像仍然保持可见。

需要重视的一点是背景图只能出现在它父元素能达到的区域。即使图片是相对于视口(view port)定位地,如果它的父元素不可见,图片就会消失。参见下面的例子。此例中,图片位于视口(view port)的左下方,但是只有元素内的图片部分是可见的。

background-image: url(test-image.jpg);
background-position: 0 100%;
background-repeat: no-repeat;
background-attachment: fixed;
Copy after login
Copy after login

因为图片开始在元素之外,一部分图片被切除了。

背景的简写属性

可以把背景的各个属性合为一行,而不用每次都单独把他们写出来。格式如下:

background: <color> <image> <position> <attachment> <repeat>
Copy after login

例如,下面的声明

background-color: transparent;
background-image: url(image.jpg);
background-position: 50% 0 ;
background-attachment: scroll;
background-repeat: repeat-y;
Copy after login

可以合为单独一行:

background: transparent url(image.jpg) 50% 0 scroll repeat-y;
Copy after login

而且不需要指定每一个值。如果省略值地话,就使用属性地默认值。例如,上面那行和下面这个效果一样:

background: url(image.jpg) 50% 0 repeat-y;
Copy after login

背景的一般用法

除了可以用来使元素更加优雅这类显然的用法之外,背景也可以用于其它的目的。

仿栏

当使用 css 的 float 属性来定位布局元素时,要确保两栏或多栏有相同的长度是比较困难的。如果长度不同,其中一栏的背景会比另外的短,这会破坏整个设计。

仿栏是个非常简单的背景技巧,这个技巧最早发表在A List Apart 。思路很简单:不再给每列单独设置背景,而是给各列的父元素设置一个背景图。所有栏的设计都包含在这张图片之中。

文本替换

在网页上,对于字体的选择是相当有限的。可以使用 sIFR 之类的工具来定制字体,但是这需要用户启用 JavaScript 。一个适用于任意浏览器的简单方法是,用想用的字体来做一张文本图片,并用这张图片作为背景。这样,文本依然出现在文档标记中以供搜索引擎检索和屏幕浏览器识别,但是在浏览器中就会显示首选的字体。

例如,HTML 标记可能是这样的:

Blogroll

Copy after login

假如有一个 200 乘 75 的图片,上面有更好看的字体,就可以用如下方式来替换文本:

h3.blogroll {
width: 200px;
height: 75px; 
background:url(blogroll-text.jpg) 0 0 no-repeat; 
text-indent: -9999px; 
}
Copy after login

简单的圆点

无需列表中的圆点看起来很难看。不用再处理所有不同的 list-style 属性,只需要简单地把他们隐藏并用背景图代替就可以了。因为图片可以随意选择,这些圆点就可以看起来更漂亮。

下面,我们把一个无需列表改造成有圆滑圆点的:

ul {
list-style: none; 
}
 ul li {
padding-left: 40px; 
background: url(bulletpoint.jpg) 0 0 no-repeat;
}
Copy after login

CSS3 中的背景

CSS3 中的背景有较多改进。最显著的是多背景图片的选项,同时也增加了4个新属性。

多背景

CSS3 中,可以对一个元素应用一个或多个图片作为背景。代码和 css2 中的一样,只需要用逗号来区别各个图片。第一个声明的图片定位在元素顶部,其它的图片按序在其下排列,例如:

background-image: url(top-image.jpg), url(middle-image.jpg), url(bottom-image.jpg);
Copy after login

新属性:背景修剪(background-clip)

这又把我们带回了文章开始讨论的那个关于边框内图片显示的话题。它被描述为“背景描绘区”。

The background-clip attribute is used to enhance the ability to control the background display position. Possible values ​​are:

* background-clip: border-box;
The background is displayed within the border.
* background-clip: padding-box;
The background is displayed within the padding (padding), not within the border.
* background-clip: content-box;
Only displays the background within the content, not within the padding and border.
* background-clip: no-clip;
Default value, the same as border-box.

New attribute: background-origin

This attribute is used in conjunction with background-position. Background-position can be calculated starting from the border, padding or content box (similar to background-clip).

* background-origin: border-box;
Start calculating the background-position with the border as the origin.
* background-origin: padding-box;
Start calculating the background with the inner padding as the origin. -position
* background-origin: content-box;
Start calculating background-position with the content box as the origin
For an explanation of the difference between background-clip and background-origin, please refer to CSS3.info

New attribute: Background size (background-size)

background-size is used to adjust the size of the background image. There are several possible values:

* background-size: contain;
Shrink the image to fit the size of the element (maintaining the pixel aspect ratio)
* background-size: cover;
Expand the image to fill the element (maintaining the pixel aspect ratio)
* background-size: 100px 100px;
Adjust the image to the specified size
* background-size: 50% 100%;
Adjust Image to specified size. Percentages are relative to the dimensions of the containing element.

You can take a look at a few examples on the CSS3 Rules website.

New property: (background-break)

In CSS3, elements can be divided into several independent boxes (such as making inline elements span across multiple lines). The background-break attribute is used to control how the background appears in these different boxes.

Possible values ​​are:

* Background-break: continuous;
Default value. Ignore the distance between boxes (that is, it is as if the element is not divided into multiple boxes and is still a whole)
* Background-break: bounding-box;
Calculate the distance between boxes
* Background-break: each-box;
Redraw the background separately for each box

背景色(background-color)的改进

background-color 在 css3 中有了稍许改进。除了设置背景颜色之外,如果元素底层的背景图不可用,还可以设置一个“回退色”。

通过在回退色之前增加一个斜杠(/)来实现,例如:

background-color: green / blue;
Copy after login

此例中,背景色应该是绿色(green)。然而,如果底层背景图不能使用的话,背景色就是蓝色而不是绿色。如果在斜杠前不指定颜色,默认为透明(transparent)。

背景平铺(background-repeat)的改进

CSS2中当图片平铺时,会被元素在末端截断。CSS3 引入了两个属性来修正这个问题:

* space: 应用同等数量的空白到图片之间,直到填满整个元素
* round: 缩小图片直到正好平铺满元素
关于 background-repeat: space; 的一个例子,可以在 CSS3 规则网站看到。

背景附着(background-attachment)的改进

background-attachment 属性增加了一个新值:local。这是用来配合可以滚动的元素的(设置为 overflow: scroll; 的元素)。当 background-attachment 设置为滚动(scroll)时,背景图不会随元素内容的滚动而滚动。

设置为 background-attachment :local; 时,背景图会随内容的滚动而滚动。

总结

总结一下,css 中关于背景有许多需要知道的知识。但是一旦把这些知识融会贯通了,这些技术和命名约定就变得非常有意义而且很快就会成为潜意识行为了。

如果刚接触 css,主要不断联系就可以较快地掌握背景的要点了。如果是老手,我希望你可以和我一样期待 css3

The above is the detailed content of Detailed explanation of examples of background image settings in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!