本文内容:

  1.背景图片定位示例

  2.background常用的属性值

  3.background-repeat新增的round、space属性

  4.background-size的属性值(着重介绍contain、cover)

  5.background-origin属性值(borde-box、padding-box、content-box)

  6.多背景图片示例


 背景图片定位(background-position)原理

A. 语法:Background-position:水平位置 垂直位置

B. 水平位置/垂直位置设置方法:

1) 数值(px)正负值都可以,正数值表示向右移动或向下移动,负数值表示向左或向上移动;

2) 百分比(%)范围:0%—100%

3) 关键词:水平位置:left(左) center(中) right(右)

垂直位置:top(上) center(中) bottom(下)

默认情况下,background-position的原点位于左上角,换句话说,元素的左上角和图片的左上角是对齐的,随后图片向各个方向重复,都是以左上角为起点。

注意:当只设置一个值时:代表水平位置,垂直位置默认居中

  1. <style>
  2. input[type="button"]{
  3. width:100px;
  4. height:56px;
  5. border-radius:10px;
  6. }
  7. .btn1{
  8. background-image: url("allbgs2.png");
  9. background-position:21px355px;
  10. /*第一,第二属性值表示以图片左上角(left top)位置为原点的坐标(21px,355px),即向右移动21px,向下移动355px*/
  11. }
  12. .btn2{
  13. background-repeat:no-repeat;
  14. background-image: url("allbgs2.png");
  15. background-position:21px-175px;
  16. /*(21px,-225px),即向右移动21px,向上移动-175px*/
  17. }
  18. img{
  19. width:100px;. height:200 px;
  20. }
  21. img:hover{
  22. height:auto;
  23. width:auto;
  24. }
  25. </style>
  26. <form>
  27. <inputtype="button"class="btn1">
  28. <inputtype="button"class="btn2">
  29. <br/>
  30. <imgsrc="allbgs2.png">
  31. <small>原背景图</small>
  32. </form>
效果图

 
CSS常用背景属性
background-color  背景颜色;
background-image  背景图片
background-repeat  背景图片重复方式
background-position  背景图片位置
background-size  背景大小
background-attachment  背景图片滚动控制
background-clip:conten-box ||  padding-box || border-box 规定背景的绘制区域,注意提前设置好padding数值,这样能够效果更加明显。
background-origin  背景的定位“原点”
 
背景属性的缩写  backgroud:green url(“image/background.jpg”) top left
 
CSS3中为background-repeat提供了两个额外值,round、space。
测试代码
  1. <style>
  2. figcaption{
  3. font-size:25px;
  4. }
  5. div.div1{
  6. background-color: aliceblue;
  7. width:50px;
  8. height:100px;
  9. background: url(".idea/pdf_icon.gif");
  10. border: solid 1px royalblue;
  11. background-repeat: space;
  12. /*space 尽可能地重复图片以占满背景,不行时则加大图片的边距*/
  13. }
  14. div.div2{
  15. background-color: aliceblue;
  16. width:50px;
  17. height:100px;
  18. background: url(".idea/pdf_icon.gif");
  19. border: solid 1px royalblue;
  20. background-repeat: round;
  21. /*round 尽可能地重复图片以占满背景,不行时则拉伸图片*/
  22. }
  23. </style>
  24. <figure>
  25. <figcaption>
  26. origin
  27. </figcaption>
  28. <imgsrc=".idea/pdf_icon.gif">
  29. </figure>
  30. <h3>background-repeat属性值space</h3>
  31. <divclass="div1"></div>
  32. <hrcolor="darkgray"/>
  33. <h3>background-repeat属性值round</h3>
  34. <divclass="div2"></div>
背景尺寸设置1
background-size:50%;
background-size:100px 50px;
测试代码
  1. <style>
  2. div.div1{
  3. width:120px;
  4. height:50px;
  5. background:url("Desert.jpg")no-repeat;
  6. background-size:50%;
  7. /*将背景图像缩放成父级元素的50%,主要是背景图片的width是父级元素width的一半*/
  8. border:1px solid #00408f;
  9. }
  10. div.div2{
  11. width:100px;
  12. height:50px;
  13. background: url("Desert.jpg");
  14. background-size:100px50px;
  15. /*将背景图像设置成宽度为100px,高度为50px*/
  16. border:2px solid #00408f;
  17. box-shadow:12px10px5px gray;
  18. }
  19. </style>
  20. <divclass="div1"></div>
  21. <divclass="div2"></div>
  22. <br/>
  23. <imgsrc="Desert.jpg">
效果图
背景尺寸设置2
background-size:cover
拉大图片,使背景图片完全填满父级容器,并保持宽高比例
background-size:contain
缩放图片,使背景图片恰好适合背景区,保持宽高比例
指定设定background-origin属性,可以指定背景图片原点位置。
(参考盒子模型
background-origin:border-box,默认值,以边框左上角为原点;
background-origin:padding-box,去掉padding之后为原点;
background-origin:content-box,以内容区域为原点;
示例
联想:背景裁剪
backgroun-clip控制背景绘制区域,比如可以让背景色和背景图片只出现在内容区域(content-box),而不出现内边框区域(padding-content)。默认情况下,背景绘制区是扩展到边框外边距的(margain)。
background-clip的属性值border-box,padding-box,content-box的用法与backgroun-origin类似
示例
  1. <styletype="text/css">
  2. body {
  3. margin:0;
  4. padding:0;
  5. font:100%Georgia,"Times New Roman",Times, serif;
  6. background:#3c6b92;
  7. }
  8. #wrapper {
  9. margin:0auto;
  10. width:960px;
  11. height:400px;
  12. background:#fff;
  13. padding:50px00200px;
  14. }
  15. #wrapper div {
  16. float: left;
  17. margin-right:50px;
  18. background:#e1d8b9;
  19. padding:25px;
  20. }
  21. #wrapper #one {
  22. width:150px;
  23. height:150px;
  24. border:10px solid rgba(212,178,220,.8);
  25. background:#e1d8b9 url(star_icon_large.png) no-repeat;
  26. /*因为背景图片是透明的,所以设置了背景颜色*/
  27. /*在此试验各种值,比如border-box*/
  28. background-clip: content-box;
  29. /*background-clip: padding-box;
  30. background-clip: border-box;*/
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <divid="wrapper">
  36. <divid="one">
  37. </div><span>content-box</span>
  38. </div>
  39. </body>
复合背景色
多背景示例
 

background的属性和背景图片定位的实例的更多相关文章

  1. CSS常用背景图片定位方法

    CSS背景图片定位其实对于每一位学习前端的同学来说,都已经非常熟悉了.网上铺天盖地的最常见的一种方案就是在父元素中relative,然后子元素absolute.这种方案当然好,不过带来的一个缺点就是会 ...

  2. CSS背景图片定位

    原文:CSS背景图片定位 在网页开发中我们经常需要对图片进行分割(如下图)来使用,而不是分别提供单独的图片来调用,常见的如页面背景,按钮图标等,这样做的好处就是减少请求次数,节省时间和带宽. 对背景图 ...

  3. CSS 背景图像 背景图片定位

    背景图片定位 background-position属性可以给背景图片定位. background-position属性有两个值,第一个值是水平位置,第二个值是垂直位置.这两个值可以使用百分比来表示( ...

  4. css 背景(background)属性、背景图定位

    background属性: Background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图上和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: ...

  5. (转)HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法

    http://blog.csdn.net/oscar92420aaa/article/details/51304067 CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中 ...

  6. HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法

    CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中,背景图片的定位方法有3种: 1)关键字:background-position: top left; 2)像素:bac ...

  7. 移动端适配之雪碧图(sprite)背景图片定位

    为了减少网络请求个数量,提高网站的访问速度,我们一般都会把一些小的图片合并成一张sprite图,然后根据background-position来进行定位.在web端由于是固定的大小与left .top ...

  8. css背景图片定位练习(二): background-position的百分比

    background-position:x y; 百分比定位并不能直观的看出来,需要通过计算. background-position百分比计算公式: (容器宽度—背景图片的宽度)*x%=xpx(容器 ...

  9. css背景图片定位练习(一)

    首先准备一张雪碧图,Like this 背景图片的定位方法有3种,比较常用的两种为 关键字:background-position: top left; (top/bottom/cennter/lef ...

随机推荐

  1. android 6.0权限判断 音频 拍照 相册

    ------------------------------------------打开音频权限------------------------------------------------ if ...

  2. 编译fresco源码

    同步发表于http://avenwu.net/fresco/2015/05/07/build_fresco_sample fresco出来已经有一阵子了,曾经尝试过一次clone源码编译,主要是看其自 ...

  3. windows原生开发之界面疑云

        windows桌面开发,界面始终是最大的困惑.我们对前端工具的要求,其实只有窗体设计器.消息映射,过分点的话自适应屏幕.模型绑定.能够免于手工书写,其实这个问题并不复杂,但VS不实现.QT语法 ...

  4. IIS7.5配置SSL

    1,首先需要准备两个证书(CA证书,服务器证书). CA证书由公共的CA机构提供,widnow系统内部已经内置了很多这类证书,如图(日文系统). 服务器证书是导入到IIS里面用的. 2,有了上面的认识 ...

  5. CORS(跨来源资源共享协议) 与 http 302状态

    昨天遇到的问题 使用ajax请求一个支持CORS的跨域页面(A),此页面返回302状态并且重新定向到页面(B).此时ajax停止不前,并且触发 ajax onerror 事件. 正确的相应应该是:aj ...

  6. ecshop的订单状态

    ecshop的订单状态都是在ecs_order_info表中的字段里. 订单状态 未确认 取消 确认 已付款 配货中 已发货 已收货 退货 order_status 0 2 1 1 1 5 5 4 s ...

  7. Ruby on Rails 和 J2EE:两者能否共存?

    http://www.ibm.com/developerworks/cn/java/wa-rubyonrails/

  8. repo: 创建local manifest以及如何添加app到CM/Android build系统中

    The local manifest Creating a local manifest allows you to customize the list of repositories on you ...

  9. Vectoroid

    Use cases Drawing (sketch, illustrations, cartooning, etc). Diagramming (any sort of chart with obje ...

  10. svn代码版本管理总结

    svn代码版本管理 1.0开发,做dev1.0的branch此时的目录结构svn://proj/             +trunk/ (不负担开发任务)             +branches ...