background用法

一、语法:

1、background 
  background : background-color || background-image || background-repeat || background-attachment || background-position 
  取值: 
    该属性是复合属性。请参阅各参数对应的属性。 
    默认值为:transparent none repeat scroll 0% 0%。

  说明: 
    设置对象的背景样式。如使用该复合属性定义其单个参数,则其他参数的默认值将无条件覆盖各自对应的单个属性设置。 
    例如:设置 background : white 等于设置 background : white none repeat scroll 0% 0% 。如果在此之前设置了 background-image 属性,则其设置将被 background-image 的默认值 none 覆盖。 
    背景样式属性的作用区域为对象的内容区域与内补丁( padding )区域。不包括边框( border )与外补丁( margin )区域。尽管该属性不可继承,但如果未指定,其父对象的背景颜色和背景图将在对象下面显示。对应的脚     本特性为 background 。

2、background-image 
  background-image : none || url ( url ) 
  取值: 
    none:  默认值。无背景图 
    url ( url ) : 使用绝对或相对 url 地址指定背景图像

  说明:

    设置或检索对象的背景图像。 当背景图像与背景颜色( background-color )都被设定了时,背景图片将覆盖于背景颜色之上。此属性对于 currentStyle 对象而言是只读的。对于其他对象而言是可读写的。对应的脚本特   性为 backgroundImage 。

二、应用 

        <td width="75" background="/homeimg/hg_2.gif"><a href=http://www.qujingwang.com> 网站名称:取经网</a></td>
  这是在表table中的一列,运行显示背景图片没问题,但验证 (XHTML 1.0 Transitional) 警告:background 不是td的有效属性。
  为了通过验证改为:
        <td style="width:75px; background:/homeimg/hg_2.gif"><a href=http://www.qujingwang.com> 网站名称:取经网</a></td>

      背景图片显示不出来,在“;”后打空格让系统自动提示,出现了background-image属性, 把background改成background-image后,背景图片还是含而不露。

       后来,发现还需在图片地址前加个函数,格式为“url(图片地址)”;background与background-image均没问题。

三、其他

  .css1{ 
    background-image:url(b_1.jpg); 
    background-repeat:no-repeat; 
  } 
  .css2{ 
    background:url(b_1.jpg) no-repeat; 
  }

      发现在IE中还是CSS1不显示,CSS2正常显示,但在火狐中都可以的。

      解决方法:

    我通常的原则是尽量只用background,所以这样写, 
    .css1{ 
      background:url(b_1.jpg); 
      background-repeat:no-repeat; 
    }

posted @ 2011-03-04 11:41  Devil_Zhang  阅读(606)  评论(0编辑  收藏  举报