首頁  >  文章  >  web前端  >  HTML與CSS中背景相關屬性

HTML與CSS中背景相關屬性

php中世界最好的语言
php中世界最好的语言原創
2018-03-13 11:46:221807瀏覽

這次帶給大家HTML與CSS中背景相關屬性,使用HTML與CSS中背景相關屬性的注意事項有哪些,以下就是實戰案例,一起來看一下。

一. 背景尺寸屬性

1.什麼是背景尺寸屬性
背景尺寸屬性是CSS3中新增的一個屬性, 專門用於設定背景圖片大小

background-size:xxxx;

取值:

1.具体像素 >> background-size:200px 100px;
2.百分比 >> background-size:100% 80%;
3.宽度等比拉伸 >> background-size:auto 100px;
4.高度等比拉伸 >> background-size:100px auto;
5.cover >> background-size:cover;

5.1告訴系統圖片需要等比拉伸

5.2告訴系統圖片需要拉伸到寬度3499910bf9dac5ae3c52d5ede7383485和5db79b134e9f6b82c0b36e0489ee08ed高度都填滿元素

6. contain >> background-size:contain;

6.1告訴系統圖片需要等比拉伸

6.2告訴系統圖片需要拉伸到寬度3499910bf9dac5ae3c52d5ede7383485或5db79b134e9f6b82c0b36e0489ee08ed高度都填滿元素(3499910bf9dac5ae3c52d5ede7383485只保證一邊填滿5db79b134e9f6b82c0b36e0489ee08ed)

 background-size

二. 背景圖片定位區域屬性

#3499910bf9dac5ae3c52d5ede7383485background-origin5db79b134e9f6b82c0b36e0489ee08ed : 告訴系統背景圖片從什麼區域開始顯示,預設就是從padding區域開始顯示;

取值:

1.<a>padding-box</a>:默认值 >>background-origin: padding-box; 告诉系统背景图片从什么区域开始显示,默认情况下就是从padding区域开始显示;
 2.<a>border-box</a> >>  background-origin:border-box; 从border位置开始
 3.<a>content-box</a> >>  background-origin:content-box;从content位置开始
<html lang="en"> <head>     <meta charset="UTF-8">     <title>113-背景图片定位区域属性</title>     <style>         *{             margin: 0;             padding: 0;         }         ul li{             list-style: none;             float: left;             width: 100px;             height: 100px;             text-align: center;             line-height: 100px;             border: 20px dashed #000;             padding: 50px;             margin-left: 20px;             background: url("images/dog.jpg") no-repeat;         }         ul li:nth-child(2){             /*             告诉系统背景图片从什么区域开始显示,             默认情况下就是从padding区域开始显示             */             background-origin: padding-box;         }         ul li:nth-child(3){             background-origin:border-box;         }         ul li:nth-child(4){             background-origin:content-box;         }     </style> </head> <body> <ul>     <li>默认</li>     <li>padding</li>     <li>border</li>     <li>content</li> </ul> </body> </html>

     背景圖片定位區域屬性

#三.背景繪製區域屬性

<a>background-clip:xxx;</a>背景绘制区域属性是专门用于指定从哪个区域开始绘制背景的, 默认情况下会从border区域开始绘制背景
<html lang="en"> <head>     <meta charset="UTF-8">     <title>114-背景绘制区域属性</title>     <style>         *{             margin: 0;             padding: 0;         }         ul li{             list-style: none;             float: left;             width: 100px;             height: 100px;             text-align: center;             line-height: 100px;             border: 20px dashed #000;             padding: 50px;             margin-left: 20px;             background: red url("images/dog.jpg") no-repeat;         }         ul li:nth-child(2){             /*             背景绘制区域属性是专门用于指定从哪个区域开始绘制背景的, 默认情况下会从border区域开始绘制背景             */             background-clip: padding-box;         }         ul li:nth-child(3){             background-clip: border-box;         }         ul li:nth-child(4){             background-clip: content-box;         }     </style> </head> <body> <ul>     <li>默认</li>     <li>padding</li>     <li>border</li>     <li>content</li> </ul> </body> </html>

     背景繪製區域屬性(紅色為繪製區域)

四.多重背景圖片

3499910bf9dac5ae3c52d5ede7383485先新增的背景圖片會蓋住後新增的背景圖片5db79b134e9f6b82c0b36e0489ee08ed

元素c3之後可以設定多張背景圖片
多張背景圖片之間用逗號隔開即可

background: url("images/animal1.png") no-repeat left top,url("images/animal2.png") no-repeat right top,url("images/animal3.png") no-repeat left bottom;

注意點:

先新增的背景圖片會蓋住後新增的背景圖片

r​​rreee

建議在編寫多重背景時拆開編寫

background: url("images/animal1.png") no-repeat left top,url("images/animal2.png") no-repeat right top,url("images/animal3.png") no-repeat left bottom,url("images/animal4.png") no-repeat right bottom,url("images/animal5.png") no-repeat center center;

完整程式碼如下:

background-image: url("images/animal1.png"),url("images/animal2.png"),url("images/animal3.png"); background-repeat: no-repeat, no-repeat, no-repeat; background-position: left top, right top, left bottom;


     多重背景圖片

四.多重背景圖片聯絡

9fd01892b579bba0c343404bcccd70fb 93f0f5c25f18dab9d176bd4f6de5d30e     a80eb7cbb6fff8b0ff70bae37074b813     b2386ffb911b14667cb8f0f91ea547a7115-多重背景图片6e916e0f7d1e588d4f442bf645aedb2f     c9ccee2e6ea535a969eb3f532ad9fe89         *{             margin: 0;             padding: 0;         }         p{             width: 500px;             height: 500px;             border: 1px solid #000;             margin: 0 auto;             /*             多张背景图片之间用逗号隔开即可             注意点:             先添加的背景图片会盖住后添加的背景图片             建议在编写多重背景时拆开编写             */             /*background: url("images/animal1.png") no-repeat left top,url("images/animal2.png") no-repeat right top,url("images/animal3.png") no-repeat left bottom,url("images/animal4.png") no-repeat right bottom,url("images/animal5.png") no-repeat center center;*/             background-image: url("images/animal1.png"),url("images/animal2.png"),url("images/animal3.png");             background-repeat: no-repeat, no-repeat, no-repeat;             background-position: left top, right top, left bottom;         }     531ac245ce3e4fe3d50054a55f265927 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

HTML與CSS中2D轉換模組

#HTML與CSS中的過渡模組

H5中​​的定位

#

以上是HTML與CSS中背景相關屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn