开发者

Implementing different CSS on page

I want to implement different开发者_开发技巧 CSS style sheet using javascript or code behind on aspx page so that for different web browser my page look better. Can anyone have some solution about this problem? I try a lot to implement that but failed.


Generally you don't want to go down the route of dynamically generating CSS with Javascript. The best approach to CSS is to:

  1. Use a reset CSS;
  2. Declare a DOCTYPE on every page; and
  3. If necessary, include IE-specific additions (because, let's face it, it's always IE that causes the problems).


To add to Ravia: You can use Request.Browser to get browser versions:

HttpBrowserCapabilities bc = Request.Browser;
if (bc.Browser == "IE" && bc.Version == "6.0")
{
    HtmlLink link = new HtmlLink();
    link.Href = ResolveClientUrl("~/CSSFile.css");
    link.Attributes.Add("rel", "stylesheet");
    link.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(link);
}


I'd go with the server side option (aspx in your case).

  1. check the 'user_agent' request header to determine the user's browser type
  2. logically include a different css file based on this variable


HtmlLink styleSheet = new HtmlLink(); styleSheet.Attributes.Add("rel","stylesheet"); styleSheet.Attributes.Add("type","text/css"); styleSheet.Attributes.Add("href",ResolveClientUrl("MyStyleSheet.css"));

this.Page.Header.Controls.Add(styleSheet);

Check this out.

You can even set the style by adding a literal to your head tag and add the css style as text to this literal.

Happy coding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜