CSS中如何使用背景樣式屬性,看這篇文章就夠用了


css背景樣式屬性介紹

  • 背景樣式就是自定義HTML標簽的背景顏色或背景圖像。
  • 背景屬性說明表
屬性名 屬性值 描述
background-color #f00、red、rgb(255,0,0) 設置背景顏色。
background-image url(背景圖片路徑) 設置背景圖像。
background-repeat repeat、repeat-x、repeat-y、no-repeat 設置背景圖片是否平鋪和平鋪方向。
background-position left、center、right、top、bottom、固定值、百分比 設置背景圖片位置。
background-attachment scroll、fixed 設置背景圖片位置是否是固定或滾動。
background 屬性值就是以上的所有值 設置背景的縮寫形式。

屬性為background-color使用方式

  • 讓我們進入屬性為background-color實踐,實踐內容如:將HTML頁面中的div背景設置為紅色。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-color屬性使用</title>
    <style>
      div{
          background-color: red;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 為什么我們給div標簽設置了background-color屬性,還有屬性值為reddiv標簽背景沒有發生任何變化呢?
  • 原因有2點如: div標簽里面沒有任何內容、 div標簽沒有設置寬高度。
  • 接下來我們在實踐,將div標簽放置一些內容。
  • 代碼塊
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-color屬性使用</title>
    <style>
      div{
          background-color: red;
      }
       
    </style>
</head>
  
<body>
   <div>成功不是打敗別人,而是改變自己。</div>
</body>
</html>
  • 結果圖

  • 現在屬性為background-color和屬性值為red才真正的被渲染出來。
  • 現在讓我們將div內容消除掉,然后我們給div設置寬高度為200px像素,看看屬性為background-color和屬性值為red,能否被渲染出來呢?
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-color屬性使用</title>
    <style>
      div{
           width: 200px;
           height: 200px;
          background-color: red;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 注意:現在大家應該明白了屬性為background-color,只有設置了寬高度的元素或者元素里面有內容,才能被渲染出來。

屬性為background-image使用方式

  • 屬性為background-image用於給元素設置背景圖片,將圖片路徑放在url()括號當中才會被渲染。

  • 屬性為background-image和屬性為background-color是一致的,都必須要有寬高度和內容才會被渲染。

  • 讓我們進入屬性為background-image實踐,實踐內容如:給div標簽設置背景圖片,div標簽寬高度設置為400px像素。

  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-image屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           background-image: url(./img/001.png);
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 注意:屬性為background-image默認圖片是平鋪的,所以這個結果圖並不奇怪哈。


屬性為background-repeat使用方式

  • 屬性為background-repeat有2種作用如:
  • 1、元素的背景圖片是否平鋪。
  • 2、設置背景圖片的水平方向平鋪或垂直方向平鋪。
  • 屬性為background-repeat的屬性值有4種如: repeatrepeat-xrepeat-yno-repeat
  • background-repeat屬性值說明表:
屬性值 描述
repeat background-repeat屬性的默認值,作用表示背景圖片平鋪。
repeat-x 作用:將背景圖片設置為水平方向平鋪。
repeat-y 作用:將背景圖片設置為垂直方向平鋪。
no-repeat 作用:將背景圖片設置為不平鋪。

屬性值為repeat實踐

  • 讓我們進入屬性為background-repeat並且屬性值為repeat實踐,實踐內容如:將div標簽背景圖片設置為平鋪。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-repeat屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           background-image: url(./img/001.png);
           background-repeat: repeat;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 注意:假設我們不設置屬性為background-repeat並且屬性值為repeat,也沒有關系的默認就是平鋪。


屬性值為repeat-x實踐

  • 讓我們進入屬性為background-repeat並且屬性值為repeat-x實踐,實踐內容如:將div標簽背景圖片設置為水平方向平鋪,為了給初學者一個直觀的印象,筆者將div標簽添加了一個邊框樣式。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-repeat屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:repeat-x;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

屬性值為repeat-y實踐

  • 讓我們進入屬性為background-repeat並且屬性值為repeat-y實踐,實踐內容如:將div標簽背景圖片設置為垂直方向平鋪,為了給初學者一個直觀的印象,筆者將div標簽添加了一個邊框樣式。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-repeat屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:repeat-y;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

屬性值為no-repeat實踐

  • 讓我們進入屬性為background-repeat並且屬性值no-repeat實踐,實踐內容如:將div標簽背景圖片設置為不平鋪,為了給初學者一個直觀的印象,筆者將div標簽添加了一個邊框樣式。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-repeat屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖


屬性為background-position使用方式

  • 屬性為background-position作用:設置背景圖片的位置在哪。
  • 屬性為background-position的屬性值分為3種使用方式如:英文單詞、固定值、百分比。
  • 英文單詞的表示說明如:left(居左)、right(居右)、top(居上)、bottom(居下)、center(居中)
  • 讓我們進入屬性為background-position使用英文單詞設置背景的位置實踐。
  • 默認就是居上和居左我們就不實踐了,如果是初學者可以嘗試下。
  • 設置背景圖片位置為居上和居右實踐。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-position:center;
           background-position: top right;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 設置背景圖片位置為居下和居左實踐。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-position:center;
           background-position: bottom left;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 設置背景圖片位置為居下和居右實踐。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-position:center;
           background-position: bottom right;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 設置背景圖片位置為居中實踐。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-position:center;
            background-position: center center;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 以上就是英文單詞設置背景圖片的位置內容。
  • 現在我們進入固定值和百分比設置div標簽背景圖片的位置實踐。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-position:center;
           background-position: 100px;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖

  • 由於簡單百分比就不進行代碼實踐了,px單位換成%百分號就是按照元素的寬高度進行百分比計算背景圖片的位置。
  • 其實英文單詞和固定值或百分比可以混合使用呢,筆者將背景圖片位置設置為居下並且是水平向右20px像素。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-position:center;
           background-position: 20px bottom;
      }
       
    </style>
</head>
  
<body>
   <div></div>
</body>
</html>
  • 結果圖


屬性為background-attachment使用方式

  • 屬性為background-attachment作用:就是設置背景圖片位置是否是固定或者是滾動的。
  • 屬性為background-attachment屬性值說明表:
屬性值 描述
scroll 設置背景圖片滾動。
fixed 設置背景圖片固定。
  • 讓我進入屬性為background-attachment實踐,實踐內容將div標簽背景圖片位置滾動和固定位置,方便大家理解滾動和固定筆者將在div標簽中放置一些內容。
  • 屬性為background-attachment默認屬性值就是scroll滾動的。
  • 背景圖片位置滾動的實踐
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-attachment:scroll;
           
      }
       
    </style>
</head>
  
<body>
   <div>
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
    </div>
</body>
</html>
  • 結果圖

  • 背景圖片位置固定實踐
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background-position屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background-image: url(./img/001.png);
           background-repeat:no-repeat;
           background-attachment:fixed;
           
      }
       
    </style>
</head>
  
<body>
   <div>
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
    </div>
</body>
</html>
  • 結果圖


屬性為background使用方式

  • 屬性為background就是設置背景的一個縮寫。本章內容大家都掌握了這個就如小菜一點不值一提哈,廢話就不多說了直接上代碼塊咯。
  • 代碼塊

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>background屬性使用</title>
    <style>
      div{
           width: 400px;
           height: 400px;
           border: 1px solid red;
           background: url(./img/001.png) no-repeat top right scroll;   
      }
       
    </style>
</head>
  
<body>
   <div>
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰,
     微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。
    </div>
</body>
</html>
  • 結果圖


免責聲明!

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



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