积极答复者
div 无法显示背景图

问题
-
新建网站,首页写入<div ><a href="Default2.aspx" >
<div ><<a href ="aaa/aaa.aspx">
然后分别在Default2和aaa/aaa的页面上写下:
<div style ="background-image:url('image/heading.png'); height :100px;">
可是运行,在 Default2 的页面上可以显示出 heading.png, 而在 aaa/aaa 的页面上无法显示出来,为什么?
多了个文件夹就不行了。如果换 asp:image 的控件,则无此现象。- 已移动 Sheng Jiang 蒋晟 2009年3月25日 18:02 HTML (从 ASP.NET 与 AJAX 移动到 Web 相关讨论(IIS,IE8等))
答案
-
你那是相对路径错误造成的。
你最好写在一个CSS文件里,就不会有不同文件夹相对位置不同的烦恼了
.bg { background-image:url('image/heading.png');}
<div style =" height :100px;" class="bg">
注意:写在css文件里的背景图位置是相对于css文件的。
另外一个方法是从根路径开始:
<div style ="background-image:url('/image/heading.png'); height :100px;">
这样的写法,开发和部署的时候都必须是网站才可以,不能是虚拟目录部署。
也可以这样
<div style ="background-image:url('<%=Request.ApplicationPath %>/image/heading.png'); height :100px;">
孟宪会- 已标记为答案 lfj0912 2009年3月25日 9:13
全部回复
-
你那是相对路径错误造成的。
你最好写在一个CSS文件里,就不会有不同文件夹相对位置不同的烦恼了
.bg { background-image:url('image/heading.png');}
<div style =" height :100px;" class="bg">
注意:写在css文件里的背景图位置是相对于css文件的。
另外一个方法是从根路径开始:
<div style ="background-image:url('/image/heading.png'); height :100px;">
这样的写法,开发和部署的时候都必须是网站才可以,不能是虚拟目录部署。
也可以这样
<div style ="background-image:url('<%=Request.ApplicationPath %>/image/heading.png'); height :100px;">
孟宪会- 已标记为答案 lfj0912 2009年3月25日 9:13