Rails CSS Selector Helper

If you’re looking for an easy way to customize css depending on the browser, without relying on JavaScript, this should be some help:

ApplicationHelper

def browser_name
    @browser_name ||= begin

      ua = request.env['HTTP_USER_AGENT'].downcase

      if ua.index('msie') && !ua.index('opera') && !ua.index('webtv')
        'ie'+ua[ua.index('msie')+5].chr
      elsif ua.index('gecko/')
        'gecko'
      elsif ua.index('opera')
        'opera'
      elsif ua.index('konqueror')
        'konqueror'
      elsif ua.index('applewebkit/')
        'safari'
      elsif ua.index('mozilla/')
        'gecko'
      end

    end
  end

Then, in your layout, you should add this to your html tag: class=”<%= browser_name %>”
So:

<html xmlns="http://www.w3.org/1999/xhtml" class="<%= browser_name %>">

Now, in your css, you can customise it to the browser:

.gecko button {
  margin-left: -4px;