Make it look good on Windows or Mac

News in pictures
News in pictures
On Facebook
From the blogs

Thanks to The Sun, for enriching each of our lives

Those at the super-soaraway Sun are, yet again, making outlandish claims that they’ve changed the wo...

Ones to watch: Aiden Grimshaw to Hey Sholay

With so much new music coming out it’s difficult to keep track of what’s out there. It’s a lucky dip...

Banter Bigotry: It’s only a joke, love

Banter is a very odd thing. As an activity it provides a handy shelter for bigots to flex their ant...

Fighting out of the Fringes: taking a school show to the Edinburgh Fringe

When I first thought about taking a group of ten Year 13 students to the Edinburgh Fringe Festival i...

Cascading style sheets are, without doubt, the most important improvement made to HTML since the introduction of tables. With CSS you can separate how the content should be presented from the actual content itself. You can also make changes to the layout and fonts of a single HTML document or an entire site from a single file rather than having to recode each and every page.

Cascading style sheets are, without doubt, the most important improvement made to HTML since the introduction of tables. With CSS you can separate how the content should be presented from the actual content itself. You can also make changes to the layout and fonts of a single HTML document or an entire site from a single file rather than having to recode each and every page.

I have discussed CSS before in this column and introduced the basic concepts. However, there are several inconsistencies in the standard that make it problematic for many developers to use. This is especially true when it comes to matching font sizes between Windows and Mac. Actually, the problem is not with CSS itself, but in the way in which the Mac operating system (OS) and Windows OS define point sizes for fonts. Without getting into the history and technical details, the problem is that Windows will display the same size font larger than a Mac will. Another problem is that Windows machines display colours darker than Mac's.

The answer? Using a JavaScript and CSS files tailored to the OS, we can deliver the right file sizes and colours for the OS that our audience is viewing our site in.

The CSS

The first thing to do is set up external CSS files. These are text files that will contain the CSS definitions and be imported for use in the HTML document using the <link> tag. Since the Mac OS and Windows OS have different font and colour standards, we will set up three different CSS files: one that will contain definitions that are common for both (default), one with definitions specific to the Mac, and one with definitions specific to Windows.

Place the following code in its own text file and save it as "default.css":

.copy { font-family: times, serif; width: 600px; line-height: 1.5; color: #000000; }

This file sets up a class called "copy" which will be used to set the appearance of the text on the HTML page.

This class sets the font to Times (or the generic serif font if Times does not exist on the visitor's computer), the colour of the text to black, and the width of the column to 600 pixels. In addition, it sets the space between the lines of text (line height) to one and a half.

If we desired to have the text double-spaced, we would use a two instead.

What has not been set by this rule is the actual size in which the text will appear. In addition, since the colour on a Windows machine will be darker than on a Mac, we need to set the colour to be lighter to compensate.

Next, create the Mac version of the CSS by saving the following code in its own file. Call this file "mac.css"

.copy { font-size: 12pt; color: #999999; }

This sets the font size for the class "copy" to 12pt and the colour to a medium grey.

The last CSS file that needs to be created is the Windows version of the CSS. Save the following code in its own file. Call this file "other.css"

.copy { font-size: 10pt; color: #cccccc; }

This sets the font size for the class "copy" to a smaller 10pt and a lighter colour of gray than the Mac version.

All of these files (default.css, mac.css, and other.css) should be saved in the same directory as the file that will contain our actual content, which, in this case, will be called "index.html".

The JavaScript

In the <head> of the of index.html, place the following JavaScript:

<script language="JavaScript">

document.write('<link href="default.css" rel="styleSheet" type="text/css">');

if ((navigator.appVersion. indexOf('Mac') != -1)) {document.write('<link href="mac.css" rel="styleSheet" type="text/css">'); }

else {document.write('<link href="other.css" rel="styleSheet" type="text/css">'); }

</script>

This script first "writes" in the link tag that will attach the standard style sheet to the document and then checks to see whether the browser being used is on a Mac. If it is, then the Mac version of the CSS is linked to the page. If not, then the Windows version is used. Notice that the script does not check to make sure that the OS is Windows. That is because this code will be used if the browser is UNIX-based or on any other OS other than the Mac.

So why doesn't the definition of "copy" in the Mac or Windows CSS file replace the definition in the standard css file? The "Cascading" in Cascading Style Sheets refers to the ability to "blend" definitions together even if they are coming from different sources.

Notice, however, that although we defined the colour of the text in standard.css, it gets redefined by the later colour rules in either mac.css or other.css. The last version of a rule listed in a CSS definition is the one that gets used.

The HTML

Now for the easy part: the HTML. Anywhere that you want to use the class "copy," set up your <p> tags like this:

<p class="copy">text here</p>

All of the content between the <p class="copy>...</p> tags will obey the CSS rules that have been set up and linked to the document using the above JavaScript. The JavaScript trick shown above can actually be used for many different purposes. For example, if you wanted to deliver a different stylesheet depending on a preference expressed by the visitor, you could use a JavasScript variable to control which stylesheet is loaded.

This script gives the Web designer and the site visitor a lot more control over exactly how the page is displayed, without having to make a new page for each version.

Jason Cranford Teague is the author of DHTML for the World Wide Web. Visit his website, Webbed Environments.

Independent Comment
blog comments powered by Disqus

Day In a Page

Child of the revolution: the Burmese family that democracy brought back together

Home of the free

The Burmese family that democracy brought back together
Cannes review: Canine accolade and Hitler's return are high spots amid the gloom

Cannes review

Frocks, canine accolade and Hitler's return
Robert Fisk: The going price of getting away with murder... would $33m be enough?

The going price of getting away with murder

Robert Fisk: The long view
Principled Skinner rises above the fray

Principled Skinner rises above the fray

Andy McSmith meets Dennis Skinner
Patrick Cockburn: I fear this terrible massacre will be the beginning of a long civil war in Syria

Patrick Cockburn

I fear this terrible massacre will be the beginning of a long civil war in Syria
Hardeep Singh Kohli: For me, it is all about 'Gregory's Girl', a record of first love

Hardeep Singh Kohli

For me, it is all about 'Gregory's Girl', a record of first love
Christian Louboutin: 'I don't think comfort equals happiness'

Christian Louboutin interview

'I don't think comfort equals happiness'
Happy birthday, Hotel Babylon!

Happy birthday, Hotel Babylon!

Hollywood's home to the A-list celebrates 100 years of discreet luxury
Rupert Cornwell: Low-rise capital could finally reach for the sky

Rupert Cornwell: Out of America

Low-rise capital could finally reach for the sky
The secret life of the red carpet

The secret life of the red carpet

As Cannes reaches its climax with the Palme d'Or and the celebrities gather in London for the Baftas tonight, Kate Youde and Jack Dean investigate the real star of the show
It's not easy being Professor Green: The rapper, the heiress and a drama made in Chelsea...

It's not easy being Professor Green

The rapper, the heiress and a drama made in Chelsea...
Hardcore, hard-wired: How the prevalence of porn is changing our everyday lives

How porn is changing our lives

It's everywhere - from pop videos to fashion magazines to the theatrical stage.
River Phoenix: the final reel

River Phoenix: the final reel

Twenty years after the actor's death, his last film is to be released
Facebook: The shares shenanigans

Facebook: The shares shenanigans

Investors are crying foul over the huge losses they incurred when the social network site floated on the stock market last week
Up and away – how '7 Up' went global

Up and away – how '7 Up' went global

As the last episode of Britain's '56 Up' airs, the first episode of '28 Up', from the former USSR, starts. Then there's the US, Japan, Germany...