简体   繁体   English

ASP.net和CSS样式

[英]ASP.net and css styles

can some on help me on how i can reference images i have all my images stored in an image folder which is located in the root folder (..\\images) in my css class i have the the following 可以帮我一些有关如何参考图像的问题吗?我所有的图像都存储在css类的根文件夹(.. \\ images)中的图像文件夹中,我具有以下内容

#image
{  background-position: left;
  background-repeat: no-repeat;} 

.background
{
  background-image: url(..\images\image.gif);
}

and in my .cs file i have 在我的.cs文件中

image.Attributes.Add("class", "background");

image is a div tag which is in my aspx code !! 图片是一个div标签,在我的aspx代码中!

when i run this code i image doesnt show, any ideas what i might be doing wrong 当我运行此代码时,我的图像没有显示,任何想法我可能做错了

You should turn those slashes in your paths around from \\ to / 您应该将路径中的斜线从\\转到/

Also, I usually put an url keyword before the parenthesis in CSS, but I am not sure if this is required: 另外,我通常在CSS中的括号前放置一个url关键字,但是我不确定是否需要这样做:

background-image: url(../images/image.gif);

UPDATE: If the image field in your C# code is some kind of webcontrol, say an Image control, you should use the setts on its CssClass property rather than explicitly adding a class attribute. 更新:如果C#代码中的image字段是某种Web控件,例如Image控件,则应在其CssClass属性上使用setts,而不是显式添加class属性。 What you are doing now might yield two class attributes in the markup, which might not get handled well. 您现在正在执行的操作可能会在标记中产生两个 class属性,这些属性可能无法很好地处理。 You can do a quick "view source" on your page to test if this is the problem. 您可以在页面上进行快速的“查看源代码”以测试是否是问题所在。

If this does not help, see Nick Cravers answers. 如果这样做没有帮助,请参阅Nick Cravers的答案。 The path from your CSS to the image is wrong, or the image file is not where you believe it is. 从CSS到图像的路径错误,或者图像文件不在您认为的位置。

URL references in a CSS file must be relative to the CSS file . CSS文件中的URL引用必须相对于CSS文件

Make sure that compared to the CSS file the image is in ..\\images , if not, adjust it to be in relation to the CSS. 确保与CSS文件相比,该图像位于..\\images ,如果没有,请将其调整为与CSS相关。

Also your background declaration should be like this: 另外,您的背景声明应如下所示:

background-image: url(..\images\image.gif);

Example: 例:

CSS = \CSS\styles.css
IMG = \Images\image.gif
Style = url(../Images/image.gif);

CSS = \styles.css
IMG = \Images\image.gif
Style = url(Images/image.gif);

The style definition: 样式定义:

background-image: url(../images/image.gif);

is saying "go up one directory and into the images folder". 说“进入一个目录并进入images文件夹”。 Try: 尝试:

background-image: url(/images/image.gif);

This assumes that the images directory is in the root of your website. 假设images目录位于您网站的根目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM