基于浏览器加载不同的css文件

时间:2011-07-16 06:33:29

标签: c# .net asp.net css

如何根据浏览器类型加载不同的css。 我想在asp.net中为IE和Firefox加载不同的CSS 我用IE8及以上版本和forefox 3及以上版本。 请帮帮我。

5 个答案:

答案 0 :(得分:6)

Request.Browser会为您提供完整的浏览器信息,您可以在其中查看版本,浏览器名称,浏览器类型等。

if(Request.Browser.Browser == "IE")
    {
        HtmlLink css = new HtmlLink();
        css.Href = ResolveClientUrl("~/style/StyleSheet.css");
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";
        Page.Header.Controls.Add(css);
    }

答案 1 :(得分:4)

您可以使用以下css条件语句在Firefox和其他浏览器的主c​​ss文件之后加载IE的css文件。这允许您重复使用大量相同的css代码,并且只覆盖IE无法正确的属性:

<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="styles/browser.css" />
<![endif]-->

上述条件语句适用于小于或等于IE6的IE版本,但您可以将其设置为您喜欢的任何内容。

您可以在此处了解有关CSS条件语句的更多信息:http://www.quirksmode.org/css/condcom.html

答案 2 :(得分:2)

您的主要CSS应该是大多数浏览器(包括Firefox)支持的CSS。然后,您可以使用HTML条件语句来加载IE特定的样式表

    <!--[if gt IE 7]>
    According to the conditional comment this is Internet Explorer greater than IE8<br />
<link rel="stylesheet" type="text/css" href="IEgreatethan7.css">

    <![endif]-->

或者如果您想要具体

    <!--[if IE 8]>
    According to the conditional comment this is Internet Explorer equal to IE8<br />
<link rel="stylesheet" type="text/css" href="IE8.css">

    <![endif]-->

答案 3 :(得分:2)

你可以这样使用。

<!--[if IE 7]>
     <link href="style-ie.css" rel="stylesheet" type="text/css" />
<![endif]-->

感谢。

答案 4 :(得分:0)

如果你谷歌你的问题,你会找到你的答案:

客户端(JavaScript):

服务器端(asp.net):http://msdn.microsoft.com/en-us/library/3yekbd5b.aspx

还搜索stackoverflow: Browser detection

相关问题