Cross browser CSS for your site
This article will go through some useful cross-browser CSS techniques I use to get my sites to look the same in several modern browsers. It's fairly easy to send out different versions of your site to different browsers. This should be avoided though since it will end up with you having to maintain the site as if it was in fact several. That defeats the whole purpose with standards, why are they even needed if you are adapting to the browsers instead? My opinion is that good cross-browser coding is to find the set of standards that are supported and then use them.
Validate your site
Validation is a much debated area and many Level 2 bosses doubt that this procedure really helps. It does help though. It ensures that you didn't do any simple spelling errors, things that could be incredibly hard to find manually. A validator also checks for nesting errors (did you put a <div>
inside of an anchor?) and other strange things like your character encoding. Information about each of the errors is available as links when they appear, just click on one and you're on your way to learn something new.
Validation is the simplest of my tricks to check. There are validators available for both (X)HTML and CSS. Use them! Any errors that show up on those lists could be a potential cross-browser breaker so if you decide to ignore any of them you should be really sure about what you are doing. There are reasons why each one of all of the errors on the validation page show up, so validate, fix, validate, fix, validate.
Stay in standards mode
The next trick is not as obvious. Modern browsers have two rendering modes they use to display websites with: Standards mode and Quirks mode. Standards mode is a rendering mode that is made to work according to the W3C specifications as closely as possible and Quirks mode is a bug ridden mode made for older sites. Why have a mode with bugs you ask? It's a way for browser makers to keep their users happy. When you do big changes to your rendering engine a lot of old sites relying on browser bugs will break. Some might think that this is a good thing, why should sites still work when they are poorly coded? If you think like that you have forgot about who the web is for. It's not a place for experts only, it's made for regular users, that is, anyone with a browser. Those people need to see a working site if that's possible.
So a new browser is released with a more standards compliant rendering mode and pages start to break. This is a bad thing for users so browser makers decided to first identify pages that tried to follow the standards, and if they did, switch to the new and improved rendering mode. You will probably see why I recommend standards mode now. All browsers are trying to render things as similar to the specs as possible when in standards mode, while in quirks mode they keep all their old bugs just to help regular users.
So how do the browsers identify who's trying to follow standards and who's not? They use the doctype. If you're not familiar with doctypes, don't worry, they are easy to learn. A doctype is a tag on first line of your site file telling the browser what markup language you will be using. There are basically two doctypes you should select among:
HTML 4.01 Strict (what I recommend)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
XHTML 1.0 Strict (without <?xml ...>
on the line before)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Using any of these make sure the browser switches to standards mode and your design not fail because of that. Using a strict doctype means that you will do your best to separate structure from design and the validator will give you errors in those areas. It's very useful. (Worth a small note is that the XHTML Transitional doctype also triggers standards mode, but while using transitional you don't get as many good validation checks so don't use that one anyways.)
There is one last catch one needs to talk about when dealing with doctypes and standards mode - the doctype needs to be the first tag in the document. If you put any HTML comments or strange characters before it IE will go crazy and switch to quirks mode. This has cause many developers countless hours of trying to fix things. Just don't do it!
Remove default styling of elements
Another cause of many web developers screaming in the night is the default CSS that is applied to elements. If you don't use any CSS at all on your page elements will still have a certain look. Headers will be larger than text paragraphs and blockquotes will have padding. Sizes of text is something that is pretty similar across browsers but something that's not is padding and margins. Let me give you an example: With no styling an <ul>
gets a padding in Firefox but a margin in IE. Solution? Set either the margin or padding to zero and set the other one to the indentation you want. You need to somehow remove the default browset styles.
These kinds of problems take up a lot of development time if not handled nicely. "Do definition lists in Opera have padding or margin?". "What about second level headers in IE 6?". Two schools of thought have evolved to handling this. The first one tells you to start by resetting all margins to their defaults at the top of you CSS file. This can easily be done by typing in *
, * being a universal selector that applies the same rules to all elements.
Problem solved right? That's where the second school of thought comes in. They argue that too many default margins are reset. Why should we mess with users form fields, rendering them hard to use unless they are set to good values again? Instead you could just reset those elements that have differences, and leave the rest untouched. This is quite a lot of work to get right so Faruk Ate? built a "starting css" template that you can easily include in your head. Personally I prefer the *-method, but try both and decide for yourself.
Browser bugs
This is the area where CSS gets hard. Even though browser makers work their asses off to follow standards they sometimes don't reach their goals. This leaves us webmasters with bugs that when fixed triggers new bugs, either in the same or another browser. It can easily get real dirty.
One of the worst browsers (that is widely in use) is Microsoft's Internet Explorer, version 6. Some claim they have about 80% of the browser market so it's not a browser you can just ignore. IE was a good browser when it was first released but by today's standards it's certainly not. No other browser caused me more pain while building the design of this page. Its shortcomings get painfully clear when it comes to rendering complex CSS layouts.
How do you handle these bugs then? The easiest (and fastest) way is not solving it yourself but reading up on someone else's solution. "Holly 'n John" have gathered the most frequent bugs on their page Explorer Exposed!. They give you examples of how to detect the bug, how it works and why (sometimes) and most importantly how to solve it. Sometimes the solution is just setting position: relative;
or display: inline;
on some element and sometimes you have to resort to strange code. The point here is that if your bug is on that page; don't waste time trying to figure it out yourself. Learn that list by heart.
So what do you do if your bug isn't on the list? You start by googling for a solution of course. Googling takes a few minutes compared to the hour you probably need to hunt it down. Don't underestimate this step.
If you don't find it somewhere you need to hunt it down yourself. Do this by making a copy of your page and then removing as much code as you can while keeping the bug. Then find out exactly what line (or lines) of code that causes it and finally try to find another way of doing what triggers it. This is much better than just throwing in hacks, you keep your code maintainable and you learn a lot more useful stuff than if you were throwing in nonsense code from the beginning.
If you for some reason do not manage to solve the bug with the above technique you either rethink what you are doing (not likely) or you go get your arsenal of hacks. Make sure the hacks are valid code. The one I use for IE when nothing else works is the "* html" hack. You use it but writing like this: * html #element
. That selector selects all tags that have the child html that have the child #element. But "html" is the topmost element in the hierarchy so nothing is selected, unless IE can choose of course. The code gets applied in IE only. Note that it is perfectly valid CSS, it just doesn't select anything. Remember: hacks are your last resort when nothing else works.
Update 18 Jan: Richard in the comments point out that you can also use conditional comments to serve a certain <style> or <link>
to different versions of IE. The "* html"-hack will not work in the comming IE7. Thanks Richard.
I hope you found something useful in this article that you can use when you get cross-browser CSS problems. I have now told you what steps I use, did I miss something? Do you do something differently?
Comments
By: zapada (#1)
By: jose (#2)
I’m a graphic designer from Venezuela, a tropical country in South America.
First of all, let me thank you and congratulate you for your excellent blog.
I’ve found it recently, and I must say that it has been very useful to me.
You’ve helped me to clarify some key concepts about standards development.
In my new weblog (in Spanish) I’m trying to write about these topics and I
wonder if it would be ok to translate some parts of your articles into Spanish.
Of course, I’d let people know that you are the original writer.
I hope this doesn’t bother you in any way, but, if so, please, let me know.
I’ll look forward for your answer.
By: Emil Stenström (#3)
By: david (#4)
is this hack?
*{
margin:0;
padding:0;
}
I can't get a definitive answer googling so I decided to start asking people.
BTW nice articles you have here
By: david (#5)
By: jose (#6)
Of course, a link to your site will be shown as well as your credits.
There is less information about web standards in Spanish than in English. For that reason I think that a translation of your articles would be very useful.
I mistyped my URL in my first comment, now it's fine so that you can check the future posts.
Thanks a lot!
By: Emil Stenström (#7)
@david: no worries :)
By: Henrik (#8)
Standards-mode: Also here am I the second school of thought - let's use the XHTML definition. Furthermore, you don't need the XML declaration on XHTML 1.0 Strict since it's served as text/html when you do nothing more.
By: Emil Stenström (#9)
By: Richard (#10)
By: David Duret (#11)
By: Busy are we? » Cross-browser strategies for CSS (#12)
By: Uira (#13)
By: Emil Stenström (#14)
By: Sven (#15)
Can I translate this in german and use it on my site?
I would like to show this my classmates.
By: Emil Stenström (#16)
By: Free Hogg » Blog Archive » links for 2006-01-19 (#17)
By: draco (#18)
* html
is still my friend till IE7 comes out. I have no idea why any other modern browsers can safely ignore this rule and yet IE7 won't, so currently I have no qualms with using the tan hack.@Henrik: Use HTML strict DTD when it's served as HTML. No point serving it as XHTML when what the users get is
text/html
. Just my 2 cents.By: Emil Stenström (#19)
By: Zach (#20)
good article, was having a terrible time getting my layout to work in IE because I had placed above the doctype.
Thanks Again.
By: Odar W. (#21)
Your site isn't displaying well in IE7-beta (background is missing).
I had the same problem with some of my sites.
By: Adam (#22)
By: About Web Designing » Blog Archive » Cross-browser CSS (#23)
By: David Owens (#24)
Hi, A lot of people have posted about conditional comments in IE standalones. There is a good article here http://www.positioniseverything.net/articles/multiIE.html
which explains how to get them working. I have IE5, 5.5, 6 and 7 beta 2, all running with conditional comments. Very useful!
By: Andyman3000 (#25)
By: Emil Stenström (#26)
@Odar W, Adam: I've had a look at IE7 and in the shape it's in now it isn't showing anything like it should. I'll wait to fix my site until they have fixed their rendering engine.
By: matt (#27)
By: Velaluka (#28)
Noteworthy:
doctype switching also affects scripting!
quirksmode /standards mode affects browsers
http://hsivonen.iki.fi/doctype/
By: Maksim Rossomachin (#29)
«The other thing is the use of the "star-selector," or * { } in CSS. The star selector selects every single element, which produces an overkill of style-nullifying. Sander pointed out that Mozilla (for one) has a great deal of default styling on form controls, which the star selector nullifies when applying margin:0 and padding:0 to it. As a result, buttons don't behave like buttons anymore, and so forth. I never really noticed that, which only goes to show that I'm no longer used to buttons behaving like buttons. I did some research in this, and as it turned out, most of all the weblogs I frequently comment on have this same problem: buttons not behaving like buttons. In most cases, it was indeed the star selector being the culprit.»
Citation from http://kurafire.net/log/archive/2005/07/26/starting-css-revisited
Maksim Rossomachin, Russian Federation.
By: Emil Stenström (#30)
By: Rob Worley (#31)
By: Eric Chua (#32)
By: Cross-Browser CSS | LevelTen Blog (#33)
By: ViB (#34)
Once again, thx!
Cheers!
By: Writing cross-browser CSS at Style Grind (#35)
By: Cert IV in IT (Web Design) | Further Links - CSS and more (#36)
By: Cert IV in IT (Multimedia) | Further Links - CSS and more (#37)
By: otro blog ms » Unos cuantos de desarrollo web (LXXXII) (#38)
By: Kerry Pratt (#39)
I found your articles both interesting and useful. I think I'm a 4.8 striving to be a solid 5 (however, with my anal personality, I fear I might be headed for a low 6).
My current site is my first attempt at using (almost) exclusively. I am now in the midst of updating to strict XHTML and valid CSS.
Thanks for such an excellent site.
--kerry
By: Tom (#40)
By: Emil Stenström (#41)
By: Ace your user assistance - by Becky Lash, Epic Trends » Blog Archive » Writing cross browser CSS (#42)
By: Michael (#43)
Good explanation of CSS.
How do you replace a framed site and still have a static group of elements like navigation and a masthead, footer etc. to make it more search engine friendly?
I've tried using layers but have difficulty undrsatnding how to target a layer with a new page and still make it cross browser compatible.
Mike
By: Emil Stenström (#44)
<?php if ($_GET["page"] == "info") include("info.php"); ?>
.Good luck!
By: Dave Everitt (#45)
By: Emil Stenström (#46)
@Dave Everitt: Agreed, that was just a quick fix to get him started. There are many steps between beginner and expert :) I'm afraid I don't know a quickfix for what you ask about.
By: jenn.suz.hoy (#47)
Anyway, my links are proving to be a bit of a problem, with all the links in IE to appear to have my "nav" class applied to them. I'm glad to find your ideas on where to start when it comes to solving problems like this!
By: Layout-palooza : Top CSS Layout Download Sites, Hacks, Galleries and Tricks : eConsultant (#48)
By: Mahesh babu.R » Cross-browser strategies for CSS (#49)
By: Jehangir Larry (#50)
Why does this page not use 'strict'? Just curious.
By: Emil Stenström (#51)
By: Clay Jackson (#52)
By: Cross-Browser « Techie Note (#53)
By: Dudewtf? (#54)
By: Emil Stenström (#55)
By: Hello world! « Webkori’s Weblog (#56)
By: Farid (#57)
Thank you.
By: David Slade (#58)
I have a problem with my site. It appears that the site requires a firefox browser but when i use it with explorer some of the pages don't align properly. Could you give me some suggestions. Hell i'll even allow you to adjust the code for me, if thats what it takes. I know your the best from what ive read. Please email me as well. Its seems that the song page and the idiom pages have the biggest problem. Funny thing is, that i went to my friends house and had them download the firefox browser and guess what the same browser the firefox browser had an alignment problem. Is the firefox browser shit or what?
By: Emil Stenström (#59)
By: Css and Themes in net 3.5 question | keyongtech (#60)
By: Sem medo de bugs: aplicando CSS para IE6, IE7 e outros browsers … « Deus, o mestre do design! (#61)
By: Jayesh (#62)
By: Aprende CSS como nunca | Emilio José Rodríguez García (#63)
By: Dan (#64)
Now I need to read the rest of the article!
By: Emil Stenström (#65)
By: rodrigo r. baysic jr (#66)
By: cinr11 (#67)
By: Emil Stenström (#68)
By: Jonice (#69)
By: Minnetonka fence (#70)
By: aditya (#71)
i am a budding web designer. i have hit a few roadblocks now. i was hoping you could help me. my site looks similar in chrome & firefox but completely different in IE8, and makes it even worse in earlier versions of IE & firefox. i think i have followed most of the standards in place. there are specifically two problems i am looking at. would you be willing to help me out if i provide a link to you here? a reply asap would be much appreciated.
thanks.
By: Mark Firestone (#72)