Thursday, March 29, 2007

Why HTML renders differently in different browsers

The fundamental reason why your site may look slightly different in various browsers.

Margins and Padding

One of the main causes for the many positional differences between layouts in various browsers is due to the default stylesheet each browser applies to give styling to certain elements. This usually involves setting default margins and padding to some elements to make them behave in a certain way.

For instance, paragraph (p) tags will have a margin applied to them so that each paragraph is separated by vertical white space and do not run into each other. The same applies to many other tags including heading tags (h1 etc). The problem occurs because the amount of margin (or padding) applied to these elements is not consistent across browsers. On many occasions Mozilla/Firefox will add a top margin to the element as well as a bottom margin. IE will however only add a bottom margin. If you were then to view these two browsers side by side you would see that the alignment would be different due to the top margin applied by Mozilla which could make your design not line up as expected.

In some designs this may not be a problem but in cases where position is important, such as aligning with other elements on the page, then the design may look bad or at least not as expected.

Here are some styles taken from the default Firefox 2.0 stylesheet (html.css) and immediately shows what is going on here:

CSS:
  1. body {
  2. display: block;
  3. margin: 8px;
  4. }

  5. p, dl {
  6. display: block;
  7. margin: 1em 0;
  8. }
  9. h1 {
  10. display: block;
  11. font-size: 2em;
  12. font-weight: bold;
  13. margin: .67em 0;
  14. }

  15. h2 {
  16. display: block;
  17. font-size: 1.5em;
  18. font-weight: bold;
  19. margin: .83em 0;
  20. }

  21. h3 {
  22. display: block;
  23. font-size: 1.17em;
  24. font-weight: bold;
  25. margin: 1em 0;
  26. }

  27. h4 {
  28. display: block;
  29. font-weight: bold;
  30. margin: 1.33em 0;
  31. }

  32. h5 {
  33. display: block;
  34. font-size: 0.83em;
  35. font-weight: bold;
  36. margin: 1.67em 0;
  37. }

  38. h6 {
  39. display: block;
  40. font-size: 0.67em;
  41. font-weight: bold;
  42. margin: 2.33em 0;
  43. }

As you can clearly see there are various properties that have been set but the most important are the margins and padding as they vary considerably. If you were to look at the default IE stylesheet you would find that there would indeed be few styles that were the same as the above.

What Can Be Done

Since we can never be sure whether the browser's stylesheet has applied margin or padding to an element the only real option is to explicitly set the margins and padding ourselves. This way we can over-ride the default stylesheet so that we know exactly how each element will behave in each browser.

As we don't really know what elements have default styling applied to them (across all browsers) we must set the margin and padding for every element we use. In most cases we are just talking about block level elements -- you do not need to do this for inline elements such as em, strong, a, etc which seldom have any margin or padding applied to them. Although em and strong will have some styling already applied to them to give them their strong and emphasized look.

Here is how you can reset the padding and margin of block elements when you use them:

CSS:
  1. html,body{margin:0;padding:0}
  2. p {margin:0 0 1em 0;padding:0}
  3. h1{margin:0 0 .7em 0;padding:0}
  4. form {margin:0;padding:0}

Take the body element for example, and notice that we have included the html element also, and then we have re-set padding and margins to zero. As explained above, various browsers will apply different amounts of margin to the body to give the default gap around the page. It is important to note that Opera does not use margins for the default body spacing but uses padding instead. Therefore we must always reset padding and margins to be 100% sure we are starting on an even footing.

If you did not reset the margins or padding and you simply defined something like this:

CSS:
  1. body{margin:1em}

Then in Opera you would now have the default padding on the body plus the extra margin you just defined there by doubling the initial space around the body in error.

Also be wary of doing things like this:

CSS:
  1. html,body {margin:0;padding:1em}

You have now defined 1em padding on the html element and 1em padding on the body element giving you 2em padding overall which probably was not what you intended.

Global White Space Reset

These days it is common to use the global reset technique which uses the universal selector (*) to reset all the padding and margins to zero in one fell swoop and save a lot of messing around with individual styles.

e.g.

CSS:
  1. * {margin:0;padding:0}

The universal selector (the asterisk *) matches any element at all and to turn all elements blue we could do something like this:

CSS:
  1. * {color:blue}

(Of course they would only be blue as long as they have not been over-ridden by more specific styles later on in the stylesheet.)

The global reset is a neat little trick that saves you having to remember to reset every element you use and you can be sure that all browsers are now starting on even footing.

Lists need a special mention here as it is not often understood that the default space or the bullet in lists is simply provided via the default stylesheet in the provision of some left margin. Usually about 16px left margin is added by default to the UL to allow the bullet image to show; otherwise there is nowhere for it to go. As with the problems already mentioned we also need to cater for some browsers that don't use left margin but use left padding instead.

This can be quite a big issue if, for instance, you have not reset the default padding and margin to zero and try something like this.

CSS:
  1. ul {padding:1em}

In browsers that have a default margin applied you will now get the default left margin of 16px (approx) and a default padding of 1em, giving you approximately twice the amount of space on the left side of the list. This would, of course, make the design look quite different in the various browsers and not something you would wish to do.

In essence the margin should have been reset to zero, either initially with the global reset, or by simply doing the following:

CSS:
  1. ul {margin:0;padding:1em}

Now all browsers will display the same, but you will need to ensure that the 1em is still enough room for the bullet to show. I usually allow 16px left margin (or padding) as a rough guide and that seems to work well. (You can use either padding or margin for the default bullet space.)

Drawbacks

However, as with all things that make life easier there is a price to be paid.

First of all, certain form elements are affected by this global reset and do not behave as per their normal defaults. The input button in Mozilla will lose its "depressed when clicked effect" and will not show any action when clicked other than submitting the form, of course. IE and Opera do not suffer from this problem and it is not really a major issue but any loss of visual clues can be a detriment to accessibility.

You may think that you can simply re-instate the margin and padding to regain the depressed effect in Mozilla but alas this is not so. Once you have removed the padding then that changes the elements behavior and it cannot be restored even by adding more padding.

There is also an issue with select/option drop down lists in Mozilla and Opera. You will find that using the global reset will take away the right padding/margin on the drop down list items and that they will be flush against the drop down arrow and look a little squashed. Again, we have problems in re-instating this padding/margin in a cross browser way.

You can't add padding to the select element because Mozilla will add the padding all around which includes the little drop down arrow that now suddenly becomes detached from its position and has a big white gap around it. You can, however, add padding right to the option element instead to give you some space and this looks fine in Mozilla but unfortunately doesn't work in Opera. Opera in fact needs the padding on the select element which as we already found out messes up Mozilla.

Here is an image showing the problems in Firefox and Opera:

Select element in Firefox and Opera

There is no easy fix -- it's something you have to live with if you use the global reset method.

If you do not have any forms in your site (unlikely) then you don't have to worry about these issues or you can simply choose to ignore them if you think your forms are still accessible and don't look too bad. This will vary depending on the complexity of your form design and is something you will need to design for yourself. If you are careful with the amount of padding you add then you can get away with a passable design that doesn't look too bad cross-browser.

Another perceived drawback, of which there has been a lot of discussion, is whether the global reset method could have speed implications on the browsers rendering of the page. As the universal selector applies to every single element on the page, including elements that don't really need it, it has been put forward that this could slow the browser down in cases where the html is very long and there are many nodes for the parser to travel.

While I agree with this logic and accept that this may be true I have yet to encounter an occasion where this has been an issue. Even if it were an issue I doubt very much that in the normal scheme of things it would even be noticeable but of course is still something to be aware of and to look out for.

The final drawback of using the global reset method is that it is like taking a hammer to your layout when a screwdriver would have been better. As I have noted above there is no need to reset things like em, b , i, a, strong etc anyway and perhaps it's just as easy to set the margins and padding as you go.

As an example of what I mean take this code.

CSS:
  1. * {margin:0;padding:0}
  2. p,ol,ul,h1,h2,h3,h4,h5,h5,h6 {margin:0 0 1em 0}

I have negated the padding on all elements and then given a few defaults for the most popular elements that I am going to use. However, when coding the page, I get to the content section and decide I need some different margins so I define the following:

CSS:
  1. #content p {margin-top:.5em}

So now I have a situation where I have addressed that element three times already. If I hadn't used the global reset or the default common styling as shown above then I could simply have said:

#content p {margin:.5em 0 1em 0;padding:0}

This way I have addressed the element only once and avoided all issues related to the global reset method. It is likely that you will apply alternate styling to all the elements that you use on the page anyway and therefore you simply need to remember to define the padding and margin as you go.

CSS:
  1. form{width:300px;margin:0;padding;0}
  2. h1{color:red;background:white;margin:1em; padding:2px;}

Conclusion

The safest method is simply to define the margins and padding as you go because nine times out of ten you will be changing something on these elements and more than likely, it will involve the padding and margins. This saves duplication and also solves all the issue that the global reset may have.

The global reset is useful for beginners who don't understand that they need to control everything or who simply forget that elements like forms have a great big margin in IE but none in other browsers.

In the end it's a matter of choice and of consistency. Whatever method you use make sure you are consistent and logical and you won't go wrong. It is up to the designer to take control of the page and explicitly control every element that is used. Do not let the browser's defaults get in your way and be aware that elements can have different amounts of padding and margin as determined by the browser's own default stylesheet. It is your job to control this explicitly.

Top 20 Websites in US

Web metrics firm Compete has an interesting post, outlining the top 20 websites (for US traffic). According to Compete, all 20 of them got over 20 million unique visitors in October 2006. Here is the chart:



A couple of people noted in the comments that if you add Microsoft's 4 top 20 properties together (msn.com, live.com,microsoft.com and passport.net), then they would probably be number 1. However a counter to that is that a lot of passport.net domains currently re-direct to live.com. I think there may be some crossover between live.com and MSN too. So it may well be that Yahoo remains number 1, even accounting for Microsoft's multiple brands. Plus of course Yahoo and Google both have separately branded properties too - e.g. Flickr, YouTube. If I was to estimate, I'd put Microsoft at number 2 overall - but interested to hear what others think.


Compete notes that Adobe.com, Live.com, Wikipedia.org and YouTube.com are new to the top 20 over the past year,while Expedia.com, Monster.com, Paypal.com and Weather.com have all dropped out.


Looking at the Alexa data for US traffic, the top 20 is quite different:


1. yahoo.com
2. google.com
3. myspace.com
4. msn.com
5. ebay.com
6. amazon.com
7. youtube.com
8. craigslist.org
9. wikipedia.org
10. cnn.com
11. facebook.com
12. go.com
13. live.com
14. blogger.com
15. aol.com
16. microsoft.com
17. comcast.net
18. imdb.com
19. digg.com
20. flickr.com

The Alexa list is (I think) only counting US traffic, but it is quite different from Compete's stats. The presence of non-mainstream web 2.0 sites in Alexa's top 20 (blogger.com, digg, flickr) suggests that the traffic is heavily skewed towards technical users - which makes sense, given Alexa relies on toolbar downloads to get their stats.


Also interesting to note there is just one Microsoft property in the top 10 in Alexa, compared to 3 for Compete.

The Top 100 Alternative Search Engine List

AllTha.at www.allth.at The search engine that keeps on looking.
Ask Mobile www.m.ask.com Mobile search engine from Ask.com
ASK VOX www.askvox.com A second talking female user interface.
AnswerBus www.answerbus.com Ask in English, French, Spanish, German or Italian.
Blabline www.blabline.com Podcast / videocast search engine
blinkx www.blinkx.com Video Search
boing www.boing.mobi Search the Mobile web
bookmach.com www.bookmach.com Searches for posts related to your keywords.
ChaCha www.chacha.com Human Guides are available to aid in your search.
ClipBlast! www.clipblast.com Video Search
Clusty www.clusty.com Clustering search engine
collarity www.collarity.com Behavioral personalized search / Collarity Compass
CONGOO www.congoo.com Searches for Premium Content
crossEngine www.crossengine.com Searches Search Engines; formerly mrSAPO
d e c i p h o www.decipho.com Behavioral personalized search / Social Meter
Ditto www.ditto.com Visual search engine
Dogpile www.dogpile.com MetaSearch Engine
dumbfind www.dumbfind.com Featuring the Two-Box search method.
exalead www.exalead.com/search Web / Image search with a European flavor
factbites www.factbites.com Search Result snippets are complete sentences.
fazzle www.fazzle.com Search engine that emphasizes Boolean Search
filangy www.filangy.com Personalized Search Engine
FIND FORWARD www.findforward.com Multi-featured search engine; check this one out!
FindSounds www.findsounds.com Search for sound effects and musical samples.
FyberSearch www.fybersearch.com Parent site for some interesting new search engines.
GIGABLAST www.gigablast.com A multi-featured search engine.
girafa www.girafa.com Visual search engine - results are thumbnails
gnod www.gnod.net Oustanding recommendation search engines
gnosh www.gnosh.org Metasearch engine
GoLexa www.golexa.com "COMPLETE page analysis for each result."
goshme Beta 3.0 www.goshme.com A search engine for search engines. Top 10 pick.
GoYams www.goyams.com Metasearch engine where you select the mix.
grokker www.grokker.com A multi-featured meta-search engine.
GRUUVE www.gruuve.com Groovy music recommendation search engine.
hakia www.hakia.com "Meaning based" search engine
ICEROCKET www.icerocket.com Blog search engine
ixquick www.ixquick.com Metasearch engine
KartOO www.kartoo.com Visually appealling clustering search engine
Lexxe www.lexxe.com Natural language processing (NLP) search engine
like www.like.com Visual shopping engine; see also riya
liveplasma www.liveplasma.com Attractive music / movies clustering / recommendation engine
Local.com www.local.com Search for local businesses, products, and services
lurpo www.lurpo.com Searches for custom Google search engines
mamma www.mamma.com metasearch engine
MetaGlossary www.metaglossary.com Searches for definitions, phrases and acronyms.
mnemomap www.mnemo.org Clustering search engine
Mojeek www.mojeek.com Customize your own personal search engine.
Mooter www.mooter.com Clustering search engine
mrquery www.mrquery.com Metasearch engine / metasearch providers
MS. DEWEY www.msdewey.com Unique user interface - enough said.
Omgili www.omgili.com Social community search engine
onkosh www.onkosh.com Arabic / English Search Engine
Pagebull www.pagebull.com Visual results search engine
pipl http://pipl.com People search engine
PlanetSearch www.planetsearch.com Metasearch engine
PolyMeta www.polymeta.com Metasearch and clustering search engine
pronto.com www.pronto.com Metasearch engine
qksearch www.qksearch.com Multi-featured "3-in-1" multi-search engine
Quintura www.quintura.com Clustering search engine with a new interface
Quintura for kids http://kids.quintura.com/ Search engine for kids by Quintura
RedZee www.redzee.com Search Engine with nice preview results
retrievr http://labs.systemone.at/retrievr/ Visual search engine
riya www.riya.com Visual search engine; see also Like
scirus http://scirus.com Scientific information only search engine
searchbots www.searchbots.net Have a little fun, create your own searchbot.
SearchTheWeb2 www.searchtheweb2.com Search The Popular Head and The Long Tail
sidekiq www.sidekiq.com Multi-category search engine. Very nice.
Slideshow http://slideshow.zmpgroup.com/ Displays search results as a moving slideshow.
Slifter www.slifter.com A mobile shopping search engine.
soople www.soople.com A simplified version of Google's search options.
Speegle www.speegle.com The speeglebot talks to you.
Sphere www.sphere.com A blog search engine.
Sproose www.sproose.com Social search engine
S R C H R www.srchr.com Metasearch engine
SurfWax www.surfwax.com Meaning-based search engine
Swamii www.swamii.com Search engine that keeps on searching for you.
Swoogle http://swoogle.umbc.edu Semantic Web search engine
thefind.com www.thefind.com Shopping search engine
Trexy www.trexy.com Follow "trails" and "trailblazers" with Trexy.
turboscout www.turboscout.com Metasearch engine
TWERQ www.twerq.com Multi-category search engine with tabbed results.
UJIKO www.ujiko.com A fun interface where you can vote on the results.
url.com www.url.com "Search with many" community metasearch engine.
VMGO.com www.vmgo.com Vote on the search results with emoticons.
WASALive www.wasalive.com A new member of the list.
Web 2.0 www.web20searchengine.com Web 2.0 search engines
WEBBRAIN www.webbrain.com Clustering "see the web" search engine.
whonu? www.whonu.com Deluxe metasearch engine.
WIKIO www.wikio.com "Live information from 33981 media and blogs"
Windows Live Mobile www.wls.live.com Windows Live Mobile search engine
WiseNut www.wisenut.com Clustering search engine
Yahoo! Mobile http://m.yahoo.com Yahoo! Mobile search engine
Yahoo! MINDSET www.mindset.research.yahoo.com Intention-driven search; commercial versus research
yoono www.yoono.com People-rated community web search
yoople www.yoople.net Yoople! = Yahoo! + Google + People
yubnub www.yubnub.org Use command lines to search the web.
ZABASEARCH www.zabasearch.com People and Public Information Search Engine.
zapmeta www.zapmeta.com Metasearch engine
Zippy www.zippy.co.uk Search engine for webmasters
ZUULA www.zuula.com Multi-category, multi-search engine, with good tabs.