CSS-Background详解

background主要包含5个属性:

background-color: 指定填充背景的颜色。

background-image: 引用图片作为背景。

background-position: 指定元素背景图片的位置。

background-repeat: 决定是否重复背景图片。

background-attachment: 决定背景图是否随页面滚动。

基本属性

背景色(background-color)

background-color 属性用纯色来填充背景。有许多方式指定这个颜色,以下方式都得到相同的结果。

background-color: blue;

background-color: rgb(0, 0, 255);

background-color: #0000ff;

background-color 也可被设置为透明(transparent),这会使得其下的元素可见。

背景图(background-image)

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

background-image: url(image.jpg);

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

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

背景平铺(background-repeat)

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

background-repeat: repeat; /* 默认值,在水平和垂直方向平铺 */

background-repeat: no-repeat; /* 不平铺。图片只展示一次。 */

background-repeat: repeat-x; /* 水平方向平铺(沿 x 轴) */

background-repeat: repeat-y; /* 垂直方向平铺(沿 y 轴) */

background-repeat: inherit; /* 继承父元素的 background-repeat 属性*/

背景定位(background-position)

background-position 属性用来控制背景图片在元素中的位置。技巧是,实际上指定的是图片左上角相对于元素左上角的位置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章