CSS 背景background

CSS 中有5个主要的背景(background)属性,它们是:

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

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

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

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

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

这些属性可以全部合并为一个缩写属性: background。需要注意的一个要点是背景占据元素的所有内容区域,包括 padding 和 border,但是不包括元素的 margin。它在 Firefox, Safari ,Opera 以及 IE8 中工作正常,但是 IE6 和 IE7 中,background 没把 border 计算在内。

背景色(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);

<html>
<head>
  <style type="text/css">         
  body {background-image: url(这个地方要写的就是你的图片的url地址了);}     
  </style>
</head>
<body>
</body> 
</html>

背景平铺(background-repeat)

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

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

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

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

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

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

<html>
<head>
    <style type="text/css">
       body 
       {
         background-image:url(图片123.jpg);
         background-repeat:no-repeat;
        }
    </style>
</head>
<body>
</body>
</html>

背景的简写属性

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

background: 《color》 《image》 《position》 《attachment》 《repeat》

例如,下面的声明

background-color: transparent;

background-image: url(image.jpg);

background-position: 50% 0 ;

background-attachment: scroll;

background-repeat: repeat-y;

可以合为单独一行:

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

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

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

<html>
<head>
    <style type="text/css">
        body 
        {
          background:#ff0000 url(图片888.jpg) no-repeat fixed center;
        }
    </syle>
</head>
<body>
<p>各个属性间并无顺序</p>
<p>各个属性间并无顺序</p>
<p>各个属性间并无顺序</p>
<p>各个属性间并无顺序</p>
</body>
</html>


继续学习
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>背景</title> <style type="text/css"> body { background:#ff0000 url(http://pics.sc.chinaz.com/files/pic/pic9/201609/fpic7436.jpg) no-repeat fixed center; } </syle> </head> <body> <p>各个属性间并无顺序</p> <p>各个属性间并无顺序</p> <p>各个属性间并无顺序</p> <p>各个属性间并无顺序</p> </body> </html>
提交重置代码
高并发千万级数据库系统解决方案
  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载