HTML學習筆記8——CSS設定背景圖片

來源:互聯網
上載者:User

標籤:

注意點:   background:blue;

                       與  

     background-color:blue;

     不一樣!

一、關於background設定:

     1)background:blue;

     2)background-image:url(圖片名稱);  

  當背景既有顏色,又有圖片時,哪個在後面哪個就生效,如下例所示;

  此處寫了“新宋體”的這張圖片作為本文涉及到的背景圖片。

  以圖片為背景時,若圖片小於頁面大小,則圖片會一直重複直到鋪滿為止;

字在後面時,字生效
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>CSS設定背景圖片</title> 6 </head> 7 <style type="text/css"> 8     #picture 9     {10         width:600px;11         height :400px;12         background: blue;13         background-image: url(ziti.png);14     }15 </style>16 <body>17     <div id="picture">18         19     </div>20 </body>21 </html>
View Code

 

顏色在後面時,顏色生效
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>CSS設定背景圖片</title> 6 </head> 7 <style type="text/css"> 8     #picture 9     {10         width:600px;11         height :400px;12         background-image: url(ziti.png);13         background: blue;14     }15 </style>16 <body>17     <div id="picture">18         19     </div>20 </body>21 </html>
View Code

二、當使用以下兩種時:

         1)background-color:red;

     2)background-image:url(圖片名稱);  

  當背景顏色既設定顏色,又設定圖片時,圖片優先顯示。(有些網站不僅設定圖片背景,還同時設定背景色~好處,額,我在知乎看到好多~大概是說讓使用者體驗更優,減少錯誤率)

如下例所示:

雖然background-color寫在後面,但圖片優先顯示
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>CSS設定背景圖片</title> 6 </head> 7 <style type="text/css"> 8     #picture 9     {10         width:600px;11         height :400px;12         background-image: url(ziti.png);13         background-repeat: no-repeat;14         background-position: center;15         background-color: red;16     }17 </style>18 <body>19     <div id="picture">20         21     </div>22 </body>23 </html>
View Code

三、想要解除重複屬性,或者控制其在x或者y方向上重複時,可在<style></style>中用   background-repeat:no-repeat;不重複

                                                       repeat-x;     在水平方向鋪滿

                                                       repeat-y;     在豎直方向鋪滿

效果如下:

四、一般情況下,背景圖片預設從左上方開始鋪,使用background-position 屬性可定義其位置,如:右邊,中間等。

另外,還可以使用background-position 屬性對圖片位置進行精確的控制,詳見下一篇學習筆記 :)

五、background-attachment:fixed;  在頁面很大時,可使該圖片不隨著捲軸的移動而消失在視野中,它會一直顯示在頁面已經定義好的地方。

 

HTML學習筆記8——CSS設定背景圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.