css中url的路徑含義

來源:互聯網
上載者:User
在CSS中有用url文法來指定background-image或是其他引用檔案中,如:
.mainheader{
    height:52px;
    background-image:url(../images/mainheader_top_bj.gif);
    background-repeat: repeat-x;
    background-position: top;
}
1.background-image一類:
實際應用時,經常把css寫入樣式檔案中,並放入專門的目錄,把圖片放入專門圖片目錄中,而實際使用的aspx檔案在另一個目錄,假設有這樣一個實際應用目錄:
----root
   ----images
       --mainheader_top_bj.gif
   ----css
       --a1.css
   ----ff.aspx

其中,ff.aspx引用到a1.css,而a1.css中有.mainheader,如文章開頭那樣的url定義,想使用images中的mainheader_top_bj.gif的圖片.
這個url的路徑應該以ff.aspx為準,還是以a1.css為準?
經測試,發現該路徑以a1.css(也即CSS檔案)為準,不論是HTML檔案使用還是在asp.net網站中,都是如此.

2.behavior一類
IE使用的 behavior: url(xxx/xxxxx.htc)來做HTML組件,這時,url裡指定的htc檔案路徑,則是相對HTML所在的目錄來的了,如下目錄:
----root
   ----images
       --mainheader_top_bj.gif
       --iepngfix.htc
   ----css
       --a1.css
   ----pub1
       --frm1.aspx
   ----ff.aspx
則ff.aspx使用的a1.css檔案中要指定behavior,則要如下:
 behavior: url(images/iepngfix.htc)
當然,這個用法就比較死板了,這樣一來,這個css檔案就只能root目錄下的aspx和html檔案使用了,如果pub1目錄下的frm1.aspx想用這個css,那上面的那個htc目錄就不對了,而應該變為:
behavior: url(../images/iepngfix.htc)
當然,這樣用,還不如直接在每個aspx檔案裡指定這個htc,而不是把這個htc式子放入css中;如果能定下網站的相對根(/)的相對路徑,也可以寫成(假設iepngfix.htc固定放在網站根下的/images目錄中):
behavior: url(/images/iepngfix.htc)

相關文章

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.