CSS中背景圖片位置 background-position 的使用方法

來源:互聯網
上載者:User
在使用背景圖片時,經常會遇到背景圖片所在的位置不是我們想要的,那如何設定背景圖片的位置呢?以下就來介紹CSS中背景圖片位置 background-position 的用法。
在背景圖片的背景圖片位置屬性中,background-position是用來控制元素背景圖片的位置。它接受三種值:

關鍵詞:如top、right、bottom、left、center
長度值;如px、em、rem等
百分值:%
一般最常用的值或者說最瞭解的值是關鍵詞和長度值,百分比使用最多的是0%、50%和100%。
在背景圖片位置屬性中,屬性值一般有2個,第一個代表橫向位置,就是X軸;第二個代表縱向位置,即Y軸,使用兩個屬性值定義背景圖片的起始位置,如果兩個方向的起始位置相同,則可以使用一個值。

其中部分屬性值的含義如下所述:

top: 背景圖片的初始位置為元素頂部

center: 背景圖片的起始位置為元素中部

left: 背景圖片的起始位置為元素左側

right: 背景圖片的起始位置為元素右側

bottom: 背景圖片的起始位置為元素底部

舉例:設定背景圖片的位置為左邊、底部,同時定義了背景圖片不平鋪。

<!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title></title>  <style type="text/css">   div{width: 200px;height: 200px;border:1px solid #ccc;background: url(img/gift2x.png) no-repeat left bottom;}  </style> </head> <body>  <div></div> </body></html>

以上是 background-position 的簡單介紹,希望對你有協助!

相關文章

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.