css代码添加背景图片常用代码

1 背景颜色 {background-color:数值}

  2背景图片{background-image: url(url)|none}

  3 背景重复 {background-repeat:inherit|no-repeat|repeat|repeat-x|repeat-y}

  4 背景固定 {background-attachment:fixed|scroll}

  5 背景定位 {background-position:数值|top|bottom|left|right|center}

  6 背影样式 {background:背景颜色|背景图象|背景重复|背景附件|背景位置}

  1.背景颜色:background-color

  语法:{background-color:数值}

  说明:参数取值和颜色属性一样

  注意:在html当中,要为某个对象加上背景色只有一种办法,那就是先做一个表格,在表格中设置完背景色,再把对象放进单元格。这样做比较麻烦,不但代码较多,还要为表格的大小和定位伤些脑筋。现在用css就可以轻松地直接搞定了,而且对象的范围很广,可以是一段文字,也可以只是一个单词或一个字母。

  例子:给部分文字加背景颜色给部分文字加背景颜色

  表格背影颜色:style="background-color:red"

  2.背景图片:background-image

  语法:{background-image: url(url)|none}

  说明: url就是背景图片的存放路径。如果用“none”来代替背景图片的存放路径,将什么也不显示。

  例子:给部分文字加背景图片 .imgbgstyle { background-image: url(logo.gif)}

  3.背景重复:background-repeat

  语法:{background-repeat:inherit|no-repeat|repeat|repeat-x|repeat-y}

  作用:背景图片重复控制的是背景图片平铺与否,也就是说,结合背景定位的控制可以在网页上的某处单独显示一幅背景图片

  说明:参数取值范围:

  ·inherit 继承

  ·no-repeat 不重复平铺背景图片

  ·repeat

  ·repeat-x 使图片只在水平方向上平铺

  ·repeat-y 使图片只在垂直方向上平铺

  注意:如果不指定背景图片重复属性,浏览器默认的是背景图片向水平、垂直两个方向上平铺。

  4.背景固定:background-attachment

  语法:{background-attachment:fixed|scroll}

  说明:参数取值范围

  ·fixed:网页滚动时,背景图片相对于浏览器的窗口而言,固定不动

  ·scroll:网页滚动时,背景图片相对于浏览器的窗口而言,一起滚动

  注意:背景图片固定控制背景图片是否随网页的滚动而滚动。如果不设置背景图片固定属性,浏览器默认背景图片随网页的滚动而滚动。为了避免过于花哨的背景图片在滚动时伤害浏览者的视力,所以可以解除背景图片和文字内容的捆绑,该为和浏览器窗口捆绑。

  例子:使背景图案不随文字“滚动”的css

  body { background: purple url(bg.jpg); background-repeat:repeat-y; background-attachment:fixed }

  5.背景定位:background-position

  语法:{background-position:数值|top|bottom|left|right|center}

  作用:背景定位用于控制背景图片在网页中显示的位置。

  说明:参数取值范围

  ·带长度单位的数字参数

  ·top:相对前景对象顶对齐

  ·bottom:相对前景对象底对齐

  ·left:相对前景对象左对齐

  ·right:相对前景对象右对齐

  ·center:相对前景对象中心对齐

  ·比例关系

  关键字解释如下:

  top left = left top = 0% 0%

  top = top center = center top = 50% 0%

  right top = top right = 100% 0%

  left = left center = center left = 0% 50%

  center = center center = 50% 50%

  right = right center = center right = 100% 50%

  bottom left = left bottom = 0% 100%

  bottom = bottom center = center bottom = 50% 100%

  bottom right = right bottom = 100% 100%

  注意:参数中的center如果用于另外一个参数的前面,表示水平居中;如果用于另外一个参数的后面,表示垂直居中。

  6. 背景样式:background

  语法:{background:背景颜色|背景图象|背景重复|背景附件|背景位置}

  作用:背景属性是一个更明确的背景—关系属性的略写。以下是一些背景的声明:

  例子:

  body { background: white url(htmlhelp.com/foo.gif) }

  blockquote { background: #7fffd4 }

  p { background: url(../backgrounds/pawn.png) #f0f8ff fixed }

  table { background: #0c0 url(leaves.jpg) no-repeat bottom right }

  注意:当一个值未被指定时,将接受其初始值。例如,在上述的前三条规则,背景位置属性将被设置为0% 0%。为了避免与用户的样式表之间的冲突,背景和颜色应该一起被指定。

发布于 2020-12-03 13:50