首頁  >  文章  >  web前端  >  css background-attachment屬性基礎介紹

css background-attachment屬性基礎介紹

小云云
小云云原創
2017-12-19 13:17:452978瀏覽

本文主要為大家介紹了在CSS中,使用背景附件屬性background-attachment可以設定背景圖像是隨對象滾動還是固定不動,需要的朋友可以參考下,希望能幫助大家。

一、background-attachment屬性

在CSS中,使用背景附件屬性background-attachment可以設定背景影像是隨物件滾動還是固定不動。

語法:

background-attachment:scroll/fixed;

說明:

background-attachment 屬性只有2個屬性值。 scroll表示背景圖像隨物件滾動而滾動,是預設選項;fixed表示背景圖像固定在頁面不動,只有其他的內容隨捲軸滾動。

範例:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        #p1
        {
            width:160px;
            height:1200px;
            border:1px solid gray;
            background-image:url("cartoongirl.gif");
            background-repeat:no-repeat;
            background-attachment:fixed;
        }
    </style>
</head>
<body>
    <p id="p1"></p>
</body>
</html>

在瀏覽器預覽效果如下:

分析:

大家在瀏覽器中拖曳右邊的捲軸會發現,背景圖片在頁面固定不動。

在IE或360設定background-attachment之後不能設定background-position屬性,不然圖片沒辦法在瀏覽器顯示。大家可以測試一下google瀏覽器、Firefox瀏覽器。

相關建議:

css background-attachment屬性的使用與定義

background-attachment PHP原始碼陣列統計count分析

GoogleChrome瀏覽器相容background-attachment:fixed;嗎?如何做到火狐背景圖片固定的效果_html/css_WEB-ITnose

以上是css background-attachment屬性基礎介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn