Tailor your CSS to your user’s operating system

It should go without saying that you want your web pages to display properly, no matter what system your site’s visitor is using. Problem is, PCs and Macs have a nasty habit of rendering things like spacing, line-heights, and typography differently.

I used to have a crude and hackish way of inserting custom CSS via JavaScript if the user was using a Mac. Today I took some time to reassess the problem and refine the solution a bit. The end result? One (ish) line of JavaScript that I like to call macfix:

<script type="text/javascript">var OSName;if(navigator.appVersion.indexOf("Mac")!=-1){document.getElementsByTagName('body')[0].className+='mac';};</script>

It’s a very simple function, as you can see – it just adds the class “mac” to your page’s body element if your user is using a Mac. After this, you’re on your own – you’ll have to look at your page on a Mac, see how it renders, and then work out what else needs to be done using the body.mac CSS rule.

Check it out for yourself – I’ve put up a simple demo here.

If this simple script has helped you, or you have suggestions or improvements, please get in touch by email or using the comments below!

About Jim

Web developer at Big Fish. Bassist, sandwich enthusiast. Excessively tall.
  • Alekgv

    Hey man. Completely unrelated. 
    I just wanted to see if you could continue to update the redditincident site. Maybe allow anyone to edit it so that it stays up to date.

    Thanks!

  • Anonymous

    Hi,

    Actually it’s just a single PHP file, and editing it would require public access to my web hosting, which is obviously not going to happen… I just updated it though. If I miss one then maybe people should email me or leave a comment on this webzone?