I'd like to add a CSS file "design-ie.css" to be launched only if the user is using IE.

I know in HTML we have [if IE7]

So something like

if ($user_is_using_ie) drupal_add_css(ie.css);

What is the Drupal way of doing this?

Thanks a lot!

Comments

grobemo’s picture

You could try the Conditional Stylesheets module. Or take a look at the Garland theme's page.tpl.php and the phptemplate_get_ie_styles() function) in Garland's template.php file as an example of a fairly easy way to handle this.

Alternatively, you could just hard code the conditional stylesheet into your page.tpl.php. It's not the most elegant, but I think it's more efficient than adding the stylesheet through drupal_add_css().

esend7881’s picture

Well I actually ended up doing it this way:

if (stristr((string)$_SERVER['HTTP_USER_AGENT'],'MSIE'))  
  drupal_add_css('sites/all/modules/best_submission/design-ie.css');

Seems to work, no extra modules or anything like that.

esend7881’s picture

Actually, I am wondering:

Is the above method I used "good" programming practice?

if (stristr((string)$_SERVER['HTTP_USER_AGENT'],'MSIE'))  {...}
mm167’s picture

good but dirty ..haha

eamonogealagain’s picture

I know this thread is quite old, but I came here searching for a solution to this problem. I didn't like the look of the above hack (any browsers spoofing as IE will get fed bad stylesheets - some browsers do this by default, some have an option that certain users enable), so I kept looking and eventually found this:

drupal_set_html_head('<!--[if lte IE 6]><link rel="stylesheet" type="text/css" href="'.drupal_get_path('module','best_submission').'/best_submission_design-ie.css.css"/><![endif]-->');
jaypan’s picture

That will work also, but you still have the same situation where any browsers spoofing IE will be fed the IE stylesheet.

Contact me to contract me for D7 -> D10/11 migrations.

eamonogealagain’s picture

As spoofing as IE only spoofs the UA - it doesn't cause the parsing of conditional html comments.