background的屬性和背景圖片定位的實例


本文內容:

  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>
復合背景色
多背景示例
 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM