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:
- Use a reset CSS;
- Declare a DOCTYPE on every page; and
- 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).
- check the 'user_agent' request header to determine the user's browser type
- 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.
精彩评论