飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • AngularJS教程
  • UEditor使用文档
  • ThinkPHP5.0教程

CSS 背景 background 样式

时间:2021-12-21  作者:匿名  

CSS 背景属性用于定义HTML元素的背景。

CSS 属性定义背景效果:

  • background-color 用于设置一个元素的背景颜色。

  • background-image 用于设置一个元素的背景图像。

  • background-repeat 用于控制在背景图像的重复。

  • background-attachment 用于控制在背景图像的滚动。

  • background-position 用于控制图像的背景中的位置。

背景颜色

background-color 属性定义了元素的背景颜色.

页面的背景颜色使用在body的选择器中:

实例

<html>
   <head>
   </head>

   <body style = "background-color:yellow;">
      <p >
         This text has a yellow background color.
      </p>
   </body>
</html>

上面例子中body设置背景颜色为黄色。

背景图像

background-image 属性描述了元素的背景图像.

默认情况下,背景图像进行平铺重复显示,以覆盖整个元素实体.

页面背景图片设置实例:

<html>
   <head>
      <style>
         body  {
            background-image: url("/css/images/css.jpg");
            background-color: #cccccc;
         }
      </style>
   </head>
   
   <body>
      <h1>Hello World!</h1>
   </body><html>

背景图像 - 水平或垂直平铺

默认情况下 background-image 属性会在页面的水平或者垂直方向平铺。

如果您不想重复图像,您可以为background-repeat属性使用no-repeat值,在这种情况下,图像将只显示一次。

默认情况下background-repeat属性将具有重复值。

一些图像如果在水平方向与垂直方向平铺,这样看起来很不协调,如下所示: 

<html>
   <head>
      <style>
         body {
            background-image: url("/css/images/css.jpg");
            background-repeat: repeat;
         }
      </style>
   </head>

   <body>
      <p>Tutorials point</p>
   </body></html>

以下示例演示了如何垂直重复背景图像。

<html>
   <head>
      <style>
         body {
            background-image: url("/css/images/css.jpg");
            background-repeat: repeat-y;
         }
      </style>
   </head>

   <body>
      <p>Tutorials point</p>
   </body></html>

设置背景图像位置

以下示例演示了如何设置距左侧 100 像素的背景图像位置。

<html>
   <head>
      <style>
         body {
            background-image: url("/css/images/css.jpg");
            background-position:100px;
         }
      </style>
   </head>

   <body>
      <p>Tutorials point</p>
   </body></html>

背景图像- 设置定位与不平铺

如果你不想让图像平铺,你可以使用 background-repeat 属性:

实例

body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
}

以上实例中,背景图像与文本显示在同一个位置,为了让页面排版更加合理,不影响文本的阅读,我们可以改变图像的位置。

可以利用 background-position 属性改变图像在背景中的位置:

实例

body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
}

下面的示例演示了如何将背景图像位置设置为距左侧 100 像素、距顶部 200 像素。

<html>
   <head>
      <style>
         body {
            background-image: url("/css/images/css.jpg");
            background-position:100px 200px;
         }
      </style>
   </head>

   <body>
      <p>Tutorials point</p>
   </body></html>

设置背景附件

背景附件确定背景图像是固定的还是随页面的其余部分滚动。

以下示例演示如何设置固定背景图像。

<!DOCTYPE html><html>
   <head>
      <style>
         body {
            background-image: url('/css/images/css.jpg');
            background-repeat: no-repeat;
            background-attachment: fixed;
         }
      </style>
   </head>

   <body>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
   </body></html>

以下示例演示如何设置滚动背景图像。

<!DOCTYPE html><html>
   <head>
      <style>
         body {
            background-image: url('/css/images/css.jpg');
            background-repeat: no-repeat;
            background-attachment: fixed;
            background-attachment:scroll;
         }
      </style>
   </head>

   <body>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
      <p>The background-image is fixed. Try to scroll down the page.</p>
   </body></html>

背景- 简写属性

在以上实例中我们可以看到页面的背景颜色通过了很多的属性来控制。

为了简化这些属性的代码,我们可以将这些属性合并在同一个属性中.

背景颜色的简写属性为 "background":

实例

body {background:#ffffff url('img_tree.png') no-repeat right top;}

当使用简写属性时,属性值的顺序为:

  • background-color

  • background-image

  • background-repeat

  • background-attachment

  • background-position

以上属性无需全部使用,你可以按照页面的实际需要使用。

湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。