.css文件中的html样式链接?

时间:2022-11-22 19:17:03

At the moment, all the pages on my site have a line that looks like this:

目前,我网站上的所有网页都有一行如下所示:

<link rel="stylesheet" type="text/css" media="screen, projection" href="css/screenpro.css" />

screenpro.css is a tiny file that looks like this:

screenpro.css是一个小文件,如下所示:

@import url("reset.css");
@import url("master.css");
@import url("account.css");
@import url("slideshow.css");
@import url("enriched.css");
@import url("ie.css");
@import url("popup.css");

That all works fine. However, I just wrote a little handler that generates a dynamic css file that changes the background colour on the page. I've included it on one of my pages like this:

一切正常。但是,我刚刚编写了一个小程序,它生成一个动态css文件,用于更改页面上的背景颜色。我把它包含在我的一个页面上,如下所示:

<link rel="Stylesheet" href="ColorChange.ashx" />

However, I am very lazy and my site has many pages - I don't want to have to go add this new import on every page. I tried putting this:

但是,我很懒,我的网站有很多页面 - 我不想在每个页面上添加这个新的导入。我试过这个:

@import url("../ColorChange.ashx");

and this:

@import url("ColorChange.ashx");

In screenpro.css, but it didn't work. Is there any way for me to include the .ashx page that generates the dynamic css into my existing css tree, or am I going to have to manually link it from every page?

在screenpro.css中,但它没有工作。有没有办法让我将生成动态css的.ashx页面包含到我现有的css树中,或者我是否必须从每个页面手动链接它?

Edit: The interesting bit in ColorChange.ashx

编辑:ColorChange.ashx中有趣的一点

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/css";

    String query = context.Request.UrlReferrer.Query;        
    Match match = Regex.Match(query, "bg=(?<bg>\\w{6})"); 
    if (!match.Success) return;

    context.Response.Write("\n.proaccount div.box, .proouter #pnlHaveResults div.box { background-color:#" + match.Groups[1] + ";}\n");
}

3 个解决方案

#1


4  

Are you sending the proper header? Something like: 'Content-type: text/css' Because you need it for the browser to identify your response as a CSS.

你发送正确的标题吗?类似于:'Content-type:text / css'因为浏览器需要它来将您的响应识别为CSS。

I haven't use much ASP, but in PHP you can do exactly what you want to do, by just printing first your header, and then your rules.

我没有使用太多的ASP,但在PHP中,您可以完全按照自己想做的方式完成,只需首先打印标题,然后再打印规则。

#2


1  

I would use an absolute path. If you start all your with a forward slash / then this means "from the root of the site". It gives you a solid start point to work from:

我会使用绝对路径。如果您使用正斜杠开始所有操作/那么这意味着“从站点的根目录”。它为您提供了一个坚实的起点:

@import url("/css/ColorChange.ashx");

I dont think you also have the content type issue occurring that has been mentioned elsewhere in this thread as you have a line that says:

我不认为你也有这个线程中已经提到的内容类型问题,因为你有一行说:

context.Response.ContentType = "text/css";

The only problem with using absolute urls is that sometimes you end up with different dev urls than your live site. For example you might end up with a url like http://localhost/mysuperapplication/ when you run it in visual studio. There is an easy way to get around this though:

使用绝对网址的唯一问题是,有时您最终会得到与您的实时网站不同的开发网址。例如,当您在visual studio中运行它时,最终可能会得到一个像http:// localhost / mysuperapplication /这样的URL。虽然有一种简单的方法可以解决这个问题:

#3


1  

Is the browser caching the old CSS file without the new import line? Try clearing your cache.

浏览器是否在没有新导入行的情况下缓存旧的CSS文件?尝试清除缓存。

#1


4  

Are you sending the proper header? Something like: 'Content-type: text/css' Because you need it for the browser to identify your response as a CSS.

你发送正确的标题吗?类似于:'Content-type:text / css'因为浏览器需要它来将您的响应识别为CSS。

I haven't use much ASP, but in PHP you can do exactly what you want to do, by just printing first your header, and then your rules.

我没有使用太多的ASP,但在PHP中,您可以完全按照自己想做的方式完成,只需首先打印标题,然后再打印规则。

#2


1  

I would use an absolute path. If you start all your with a forward slash / then this means "from the root of the site". It gives you a solid start point to work from:

我会使用绝对路径。如果您使用正斜杠开始所有操作/那么这意味着“从站点的根目录”。它为您提供了一个坚实的起点:

@import url("/css/ColorChange.ashx");

I dont think you also have the content type issue occurring that has been mentioned elsewhere in this thread as you have a line that says:

我不认为你也有这个线程中已经提到的内容类型问题,因为你有一行说:

context.Response.ContentType = "text/css";

The only problem with using absolute urls is that sometimes you end up with different dev urls than your live site. For example you might end up with a url like http://localhost/mysuperapplication/ when you run it in visual studio. There is an easy way to get around this though:

使用绝对网址的唯一问题是,有时您最终会得到与您的实时网站不同的开发网址。例如,当您在visual studio中运行它时,最终可能会得到一个像http:// localhost / mysuperapplication /这样的URL。虽然有一种简单的方法可以解决这个问题:

#3


1  

Is the browser caching the old CSS file without the new import line? Try clearing your cache.

浏览器是否在没有新导入行的情况下缓存旧的CSS文件?尝试清除缓存。