欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
  CSS3 Background
 
  CSS3对于 background 做了一些修改,最明显的一个就是采用设置多背景,不但添加了4个新属性,并且还对目前的属性进行了调整增强。
 
  1、 多个背景图片
 
  在CSS3里面,你可以在一个标签元素里应用多个背景图片。代码类似与CSS2.0版本的写法,但引用图片之间需用“,”逗号隔开。第一个图片是定位在元素最上面的背景,后面的背景图片依次在它下面显示,如下:
 
  background-image: url(image1.jpg), url(image2.jpg), url(image3.jpg);
 
  2、新属性:Background Clip
 
  background-clip 顾名思义,背景剪切,用来设置元素的背景(背景图片或颜色)是否延伸到边框下面。
 
  background-clip: border-box; 背景延伸至边框外沿(但是在边框下层)
 
  background-clip: padding-box; 背景延伸至内边距(padding)外沿。不会绘制到边框处
 
  background-clip: content-box; 背景被裁剪至内容区(content box)外沿
 
  background-clip: text; 背景被裁剪成文字的前景色(实验属性,需要加浏览器前缀)
 
  3、新属性: Background Origin
 
  此属性需要与 background-position 配合使用。你可以用 background-position 计算定位是从border,padding或content boxes内容区域算起。(类似 background-clip )
 
  注意:当使用 background-attachment 为fixed时,该属性将被忽略不起作用。
 
  background-origin:border-box; 从border边框位置算起
 
  background-origin:padding-box; 从padding位置算起
 
  background-origin:content-box; 从content-box内容区域位置算起;
 
  4、新属性:Background Size
 
  Background Size属性用来设置背景图片的大小。有几个属性值:
 
  background-size: contain;缩小背景图片使其适应标签元素(主要是像素方面的比率)
 
  background-size: cover;让背景图片放大延伸到整个标签元素大小(主要是像素方面的比率)
 
  background-size: 100px 100px;标明背景图片缩放的尺寸大小
 
  background-size: 50% 100%;百分比是根据内容标签元素大小,来缩放图片的尺寸大小
 
  5、Background Repeat 调整
 
  CSS2里当设置背景的时候,它经常被标签元素截取而显示不全,CSS3介绍了2个新属性来修复此问题。
 
  background-repeat: space; 图片以相同的间距平铺且填充整个标签元素
 
  background-repeat: round; 图片自动缩放直到适应且填充整个标签元素
 
  6、Background Attachment 的调整
 
  Background Attachment有了一个新属性值: local ,当标签元素滚动时它才有效(如设置 overflow: scroll; ),当 background-attachment 设置为 scroll 时,背景图片是不随内容滚条滚动的。现在,有了 background-attachment: local ,就可以做到让背景随元素内容滚动而滚动了。
 
  7、新增 Background Blend Mode背景的混合模式是当背景重叠时计算像素最终色值的方法,每种混合模式采用前景和背景颜色值(分别为顶部颜色和底部颜色),执行其计算并返回颜色值。最终的可见层是对混合层中的每个重叠像素执行混合模式计算的结果。 background-blend-mode: normal | multiply | screen | overlay | darken | lighten | color-dodge | color-burn | hard-light | soft-light | difference | exclusion | hue | saturation | color | luminosity;
 
  CSS3 多背景模拟元素边框
 
  我们这里主要使用了 background-img 、 background-size 和 background-position 三个属性。
 
  background-image: [background-image], [background-image], [background-image];
 
  background-position: [background-position], [background-position], [background-position];
 
  background-repeat: [background-repeat], [background-repeat], [background-repeat];
 
  简写形式如下:
 
  background: [background-image] [background-position] [background-repeat],
 
  [background-image] [background-position] [background-repeat],
 
  [background-image] [background-position] [background-repeat];
 
  现在我们用多背景来模拟一个元素的边框
 
  。exammple {
 
  background: linear-gradient(0, #108b96 2px, #108b96 2px) no-repeat,
 
  linear-gradient(-90deg, #108b96 2px, #108b96 2px) no-repeat,
 
  linear-gradient(-180deg, #108b96 2px, #108b96 2px) no-repeat,
 
  linear-gradient(-270deg, #108b96 2px, #108b96 2px) no-repeat;
 
  background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
 
  background-position: left top, right top, right bottom, left bottom;
 
  }
 
  我们用四个渐变的背景来模拟四个边框(为什么我们要用渐变而不是直接的Color呢?这是由于CSS的多背景只能是 background-image , background-color 不支持多个值,所有即便是纯色的边框,我们也只能使用渐变)。
 
  初步效果
 
  接下来我们让边框动起来
 
  。exammple {
 
  transition: ease-in .3s;
 
  background: linear-gradient(0, #108b96 2px, #108b96 2px) no-repeat,
 
  linear-gradient(-90deg, #108b96 2px, #108b96 2px) no-repeat,
 
  linear-gradient(-180deg, #108b96 2px, #108b96 2px) no-repeat,
 
  linear-gradient(-270deg, #108b96 2px, #108b96 2px) no-repeat;
 
  background-size: 0 2px, 2px 0, 0 2px, 2px 0;
 
  background-position: left top, right top, right bottom, left bottom;
 
  }
 
  。exammple:hover {
 
  background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
 
  }
 
  现在我们已经按要求实现了交互效果。
 
  总结
 
  相比border属性,用background的模拟边框存在以下的优势和劣势
 
  优势
 
  劣势
 
  可以控制宽高,渐变色,运动方向等,灵活多变,能实现很多border不能实现的效果,并且不用添加额外的元素
 
  不能实现border圆角
 
  需要注意的是 background模拟的边框不等同于真正的边框,是不占用边框的宽高的,计算盒子模型时要留心
 
  最后
 
  由于CSS3对背景属性的进一步丰富,利用CSS3的多背景可以实现很多以前必须借助js或图片才能实现的效果,比如半透明背景、几何图案背景、条纹背景等,期待和大家一块儿去探索CSS的奥秘。

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h62261.shtml