css設定背景圖片

來源:互聯網
上載者:User

標籤:

background:pink;圖片在背景圖上面

background-image:url(food.jpg);一張圖片鋪滿一行
background-repeat:repeat-x;同一張圖片多張鋪滿橫向

background-repeat:repeat-y;同一張圖片多張鋪滿縱向

background-repeat:no-repeat;只在左上方顯示一張按圖片原來的大小顯示

background-attachment:fixed;把圖片固定在某處,圖片不隨著捲軸的滾動上下移動

 background-position:center;圖片位置置中

<style>
#test1{
 background:pink;
 background-image:url(food.jpg);
 height:493px;
 background-repeat:no-repeat;
 background-attachment:fixed;
 background-position:center;
}
</style>
</head>
<body>
<p id="test1">人生總有許多偶然和巧合,兩條平行線,也可能會有交匯的一天。
人生又有許多意外和錯過,握在手裡的風箏,也會突然斷了線……。 </p>
</body>

精確控製圖片顯示位置:

background-position:center  enter;

<style>
#test1{
 width:500px;
 height:400px;
 background-image:url(food.jpg);
 background-repeat:no-repeat;
 background-position:center center;
}
</style>
</head>
<body>
<div id="test1"></div>

</body>

用精確的像素來確定位置:

<style>
#test1{
 width:500px;
 height:400px;
 background-image:url(food.jpg);
 background-repeat:no-repeat;
 background-position:center center;
}
#test2{
 width:50px;
 height:30px;
 background-image:url(food1.jpg);
 background-position:-150px -500px;
}
<style>
</head>
<body>
<div id="test1"></div>
<div id="test2"></div>
</body>

把所有的代碼寫在一行裡的順序

後面是具體像素,顯示圖片上你需要的某部分,即用一張圖做多個元素的背景,調整需要部分它的位置即可

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.