CSS背景教程

CSS网页制作第6讲:背景的使用与操作演示
课程内容讲解:
CSS 中有5个主要的背景(background)属性,它们是:
* background-color: 指定填充背景的颜色。
* background-image: 引用图片作为背景。
* background-position: 指定元素背景图片的位置。
* background-repeat: 决定是否重复背景图片。
* background-attachment: 决定背景图是否随页面滚动。
这些属性可以全部合并为一个缩写属性: background


 

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

 /* 例 1: 默认值 */ background-position: 0 0; /* 元素的左上角 */
 /* 例 2: 把图片向右移动 */ background-position: 75px 0;
 /* 例 3: 把图片向左移动 */ background-position: -75px 0;
 /* 例 4: 把图片向下移动 */ background-position: 0 100px;

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

  • * left
  • * center
  • * right

y 轴上:

  • * top
  • * center
  • * bottom

背景附着

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

高清《CSS背景》视频下载地址:http://bbs.zzarea.com/thread-429-1-1.html
 

本文链接:https://www.dnwfb.com/983.html,转载请注明出处。
0

评论0

没有账号? 注册  忘记密码?