Great! This allows us to keep browser specific classes in the main stylesheet and
keep the CSS pure and hack-free. Super Great!
12
div.foo{color:#000;}.ie7div.foo{color:#FC0;}
Doing it in Rails
Although it is entirely possible to mimic the
above in Rails, there is room for much improvement as a friend pointed out. The
suggestion was to determine the IE version from request.user_agent and generate
the <html> tag from that. So Instead of having many conditional <html> tags,
there will be a single <html> tag with the appropriate CSS classes.
The Ruby
Add the useragent gem to your Gemfile and run bundle install
Gemfile
1
gem"useragent"
Create a new initializer in the initializers folder and add the following code
to it:
12
# Browser object used for UserAgent comparisonBrowser=Struct.new(:browser,:version)
Next is the html_tag helper in application_helper.rb. Using the useragent
gem we can detect the current version of IE and add the appropriate class to the
<html> tag. Additionally the lang attribute is set using the default Rails locale.