load different css file based on browser
H开发者_如何学Pythonow to load different css based on browser type. I want to load different css for IE and Firefox in asp.net I'm usng IE8 and above and forefox 3 and above. please help me.
Request.Browser will give you complete browser information, where you can check version, browser name, browser type etc.
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);
}
You can use the following css conditional statement to load a css file for IE after the main css file for Firefox and other browsers. This allows you to re-use a lot of the same css code and only overwrite those properties that IE doesn't get right:
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="styles/browser.css" />
<![endif]-->
The conditional statement above applies to versions of IE less than or equal to IE6, but you can set this to whatever you like.
You can learn more about CSS conditional statements here: http://www.quirksmode.org/css/condcom.html
Your main CSS should be the one that is supported by most browsers (including Firefox). Then you can use HTML conditional statements to load IE specific stylesheets
<!--[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]-->
or if you want to be specific
<!--[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]-->
You can use like this.
<!--[if IE 7]>
<link href="style-ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
Thanks.
If you google your question, you will find your answer:
Client-side(javascript):
http://tycoontalk.freelancer.com/javascript-forum/101813-loading-different-css-based-on-browser.html
http://api.jquery.com/jQuery.browser/
Server-side(asp.net): http://msdn.microsoft.com/en-us/library/3yekbd5b.aspx
Also search on stackoverflow: Browser detection
精彩评论