標籤:
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設定背景圖片