• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >CSS > CSS背景background、background-position使用详解

CSS背景background、background-position使用详解

作者:佚名 字体:[增加 减小] 来源:互联网 时间:2017-08-06

本文主要包含background,background-position等相关知识,佚名 希望在学习及工作中可以帮助到您

背景(background)是css中一个重要的的部分,也是需要知道的css的基础知识之一。这篇文章将会涉及css背景(background)的基本用法,包括诸如 background-attachment 等的属性,也会介绍一些有关背景(background)的常用技巧,以及 css3 中的 背景(background)(包含4个新的背景(background)属性)。

css2 中的背景(background)

概述

CSS2 中有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 does not extend to the borders in IE7 and IE6.

 

基本属性

 

背景色(background-color)

background-color 属性用纯色来填充背景。有许多方式指定这个颜色,以下方式都得到相同的结果。

  1. background-color: blue;   
  2. background-color: rgb(0, 0, 255);   
  3. background-color: #0000ff;  


background-color 也可被设置为透明(transparent),这会使得其下的元素可见。

 

背景图(background-image)

background-image 属性允许指定一个图片展示在背景中。可以和 background-color 连用,因此如果图片不重复地话,图片覆盖不到地地方都会被背景色填充。代码很简单,只需要记住,路径是相对于样式表的,因此以下的代码中,图片和样式表是在同一个目录中的。

background-image: url(image.jpg);

但是如果图片在一个名为 images 的子目录中,就应该是:

background-image: url(images/image.jpg);

糖伴西红柿:使用 ../ 表示上一级目录,比如 background-image: url(../images/image.jpg); 表示图片位于样式表的上级目录中的 images 子目录中。有点绕,不过这个大家应该都知道了,我就不详说了。

 

背景平铺(background-repeat)

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

  1. background-repeat: repeat; /* 默认值,在水平和垂直方向平铺 */  
  2. background-repeat: no-repeat; /* 不平铺。图片只展示一次。 */  
  3. background-repeat: repeat-x; /* 水平方向平铺(沿 x 轴) */  
  4. background-repeat: repeat-y; /* 垂直方向平铺(沿 y 轴) */  
  5. background-repeat: inherit; /* 继承父元素的 background-repeat 属性*/  

【重点】背景定位(background-position)

background-position 属性用来控制背景图片在元素中的位置。技巧是,实际上指定的是图片左上角相对于元素左上角的位置。
下面的例子中,设置了一个背景图片并且用 background-position 属性来控制它的位置,同时也设置了 background-repeat 为 no-repeat。计量单位是像素。第一个数字表示 x 轴(水平)位置,第二个是 y 轴(垂直) 位置。

  1. /* 例 1: 默认值 */  
  2. background-position: 0 0; /* 元素的左上角 */  
  3.     
  4. /* 例 2: 把图片向右移动 */  
  5. background-position: 75px 0;   
  6.     
  7. /* 例 3: 把图片向左移动 */  
  8. background-position: -75px 0;   
  9.     
  10. /* 例 4: 把图片向下移动 */  
  11. background-position: 0 100px;  

The image can be set to any position you like.

background-position 属性可以用其它数值,关键词和百分比来指定,这比较有用,尤其是在元素尺寸不是用像素设置时。

关键词是不用解释的。x 轴上:

* left* center* right

y 轴上:

* top* center* bottom

顺序方面和使用像素值时的顺序几乎一样,首先是 x 轴,其次是 y 轴,像这样:

background-position: right top;

使用百分数时也类似。需要主要的是,使用百分数时,浏览器是以元素的百分比数值来设置图片的位置的。看例子就好理解了。假设设定如下:

background-position: 100% 50%;

This goes 100% of the way across the image (i.e. the very right-hand edge) and 100% of the way across the element (remember, the starting point is always the top-left corner), and the two line up there. It then goes 50% of the way down the image and 50% of the way down the element to line up there. The result is that the image is aligned to the right of the element and exactly half-way down it.

糖伴西红柿:这一段没想到合适的翻译,保留原文,意译。 

update: 感谢天涯的指教,这段搞明白了。使用百分数定位时,其实是将背景图片的百分比指定的位置和元素的百分比位

您可能想查找下面的文章:

  • css background 背景图的设置方法
  • CSS background全部汇总
  • CSS的background属性及CSS3的背景图片设置总结
  • 使用CSS3来实现滚动视差效果的教程
  • background-size使用详解
  • CSS3属性background-size使用指南
  • CSS背景background、background-position使用详解
  • CSS background 控制显示图片的一部分
  • css中background-size属性使用介绍
  • css3中背景尺寸background-size详解

相关文章

  • 2017-08-06CSS Hack 浏览器兼容整理
  • 2017-08-06CSS圆形旋转效果 纯CSS制作圆形旋转菜单效果(七步完成)
  • 2017-08-06让DIV块在页面的某个位置固定的css代码
  • 2017-08-06CSS图片垂直居中实现方法详解
  • 2017-08-06使用不带单位的line-height
  • 2017-08-06玩转CSS3色彩
  • 2017-08-06分享几个CSS小众但炫酷的技巧
  • 2017-08-06css z-index层重叠顺序使用介绍
  • 2017-08-06css布局九决 学css不再难
  • 2017-08-06html5+css3气泡组件的实现

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • 关于几个常见的css字体设定问题探讨
    • 网页添加CSS样式表的四种方法
    • html td nowrap不换行属性使用方法
    • 使用CSS3实现圆角,阴影,透明
    • 纯css3制作的火影忍者写轮眼开眼至轮回眼及进化过程实例
    • CSS3 制作绽放的莲花采用效果叠加实现
    • 纯CSS实现的小三角
    • CSS3的文字阴影—text-shadow的使用方法
    • css中替换元素和不可替换元素及显示元素详细探讨
    • 让div透明而里面的文字不透明的写法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有