The Principles Of Cross-Browser CSS Coding

Advertisement

It is arguable that there is no goal in web design more satisfying than getting a beautiful and intuitive design to look exactly the same in every currently-used browser. Unfortunately, that goal is generally agreed to be almost impossible to attain. Some have even gone on record1 as stating that perfect, cross-browser compatibility is not necessary.

While I agree that creating a consistent experience for every user in every browser (putting aside mobile platforms for the moment) is never going to happen for every project, I believe a near-exact cross-browser experience is attainable in many cases. As developers, our goal should not just be to get it working in every browser; our goal should be to get it working in every browser with a minimal amount of code, allowing future website maintenance to run smoothly.

In this article, I’ll be describing what I believe are some of the most important CSS principles and tips that can help both new and experienced front-end developers achieve as close to a consistent cross-browser experience as possible, with as little CSS code as possible.

Cross-Browser CSS

Understand the CSS Box Model

This is one of the first things you should be thoroughly familiar with if you want to be able to achieve cross-browser layouts with very few hacks and workarounds. Fortunately, the box model is not a difficult thing to grasp, and generally works the same in all browsers, except in circumstances related to certain versions of Internet Explorer (more on this later).

The CSS box model is responsible for calculating:

  • How much space a block-level element takes up
  • Whether or not borders and/or margins overlap, or collapse
  • A box’s dimensions
  • To some extent, a box’s position relative to other content on the page

The CSS box model has the following basic rules:

  • Block-level elements are essentially rectangular (as are all elements, really)
  • The dimensions of a block element are calculated by width, height, padding, borders, and margins
  • If no height is specified, a block element will be as high as the content it contains, plus padding (unless there are floats, for which see below)
  • If no width is specified, a non-floated box will expand to fit the width of its parent minus padding (more on this later)

Some important things to keep in mind for dealing with block-level elements:

  • If a box has its width set to “100%”, it shouldn’t have any margins, padding, or borders, otherwise it will overflow its parent
  • Vertically-adjacent margins can cause some complex collapsing issues2 that may cause layout problems
  • Elements positioned relatively or absolutely will behave differently, the details of which are extensive and beyond the scope of this article
  • The rules and principles above are stated under the assumption that the page holding the block-level elements is rendered in standards mode (this point was added later after review of the comments posted3)

The Box Model as shown in Firebug
The box model as its displayed using Firebug in Firefox

Understand the Difference Between Block and Inline

For experienced developers, this is another no-brainer. It is, however, another crucial area that, when fully understood, will cause the light bulb to appear4, many headaches will be avoided, and you’ll feel much more confident in creating cross-browser layouts.

The image below illustrates the difference between block and inline elements:

Block and Inline Elements

Here are some basic rules that differentiate block elements from inline:

  • Block elements will, by default, naturally expand horizontally to fill their parent container, so there’s no need to set a width of “100%”
  • Block elements will, by default, begin at the leftmost edge of the parent box, below any previous block elements (unless floats or positioned elements are utilized; see below)
  • Inline elements will ignore width and height settings
  • Inline elements flow with text, and are subject to typographical properties such as white-space, font-size, and letter-spacing
  • Inline elements can be aligned using the vertical-align property, but block elements cannot
  • Inline elements will have some natural space below them in order to accommodate text elements that drop below the line (like the letter “g”)
  • An inline element will become a block element if it is floated

Understand Floating and Clearing

For multi-column layouts that are relatively easy to maintain, the best method is to use floats5. Having a solid understanding of how floats work is, therefore, another important factor in achieving a cross-browser experience.

A floated element can be floated either left or right, with the result that the floated element will shift in the specified direction until it reaches the edge of its parent container, or the edge of another floated element. All non-floated, inline content that appears below the floated element will flow along the side of the element that is opposite to the float direction.

A Floated Element

Here are some important things to keep in mind when floating and clearing elements:

  • Floated elements are removed from the flow of other block-level, non-floated elements; so in other words, if you float a box left, a trailing paragraph (block level) that’s not floated will appear behind the float in the stack, and any text inside the paragraph (inline level) will flow around the float
  • To get content to flow around a floated element, it must be either inline or else floated in the same direction
  • A floated element without a declared width will shrink to the width of its content, so it’s generally best to have a set width on a float
  • If a block element contains floated children, it will “collapse”, requiring a fix6
  • An element that’s “cleared” will avoid flowing around floated elements above them in the document
  • An element that’s both cleared and floated will only clear itself of elements that come before, not after

Use Internet Explorer First (Or Don’t)

Please note that Smashing Magazine’s team strongly advises against developing websites in Internet Explorer first. In our opinion, sites should be developed in “modern” web-browsers, with standards first and then be tweaked for buggy versions of Internet Explorer. The advice below does not reflect the opinion of the Smashing Editorial team. Agree or disagree? Comment on this article!

Most of what I’ve discussed so far has to do with CSS coding and layout principles. This principle is more related to habits and preferences of most designers. Although we might hate to use IE6 and IE7 in our everyday internet activities, when it comes time to build a client project from scratch, one of the best things you can do is test your layout in those browsers early in development. You might even want to open up a standalone version of IE6 or IE77 and just start your development in that browser.

Of course, you won’t have access to tools like Firebug, but generally for CSS (especially early in development) you won’t need Firebug. It’s much easier to get a layout working first in IE6 and IE7, then fix for other browsers, than to do it the other way around. Waiting until late in the development process to open up IE6 and/or IE7 will likely cause some, if not all, of the following problems:

  • Multiple hacks will be required, needing separate stylesheets for different versions of IE, making the code bloated and less maintainable and making the website slower
  • The layout in some spots may have to be reworked, multiplying development time
  • Testing time will increase, sometimes exponentially, leaving less time for more important tasks
  • The layout in other browsers will not be the same as in IE6 and IE7

I wouldn’t expect developers to use Internet Explorer this aggressively for personal projects, web apps, or other non-client work. But for corporate clients whose user base is primarily on Internet Explorer, this tip could prevent a lot of headaches while getting things to work properly, and will definitely make a cross-browser experience much more likely.

Sometimes viewing IE’s problems as “annoying bugs” can create unnecessary negativity, and can hinder development time and future maintenance. Dealing with IE is a fact of life for designers and developers, so just view its problems as you would any CSS issue — and build from there.

Understand Internet Explorer’s Most Common Problems

If you’re going to start with IE in your development, or at the very least check your layout in IE early on, then you should understand what things Internet Explorer (usually versions 6 & 7) has problems with, or what its limitations are.

A detailed discussion of every possible problem that can occur in Internet Explorer, and a list of all of its CSS compatibility issues8 is certainly beyond this article. But there are some fairly significant inconsistencies and issues that come up in relation to IE that all CSS developers should be aware of. Here is a rundown of the most common problems you’ll need to deal with:

  • IE6 will become problematic if floats are overused, causing (paradoxically) disappearing content9 or duplicate text10
  • IE6 will double the margin on floated elements on the side that is the same direction as the float; setting display: inline will often fix this
  • In IE6 and IE7, if an element doesn’t have layout11, it can cause a number of problems, including backgrounds not showing up, margins collapsing improperly, and more12
  • IE6 does not support min- and max-based CSS properties like min-height, or max-width
  • IE6 does not support fixed positioning of background images
  • IE6 and IE7 do not support many alternate values for the display property (e.g. inline-table, table-cell, table-row, etc.)
  • You cannot use the :hover pseudo-class on any element in IE6 except an anchor (<a>)
  • Certain versions of IE have little support for certain CSS selectors13 (e.g. attribute selectors, child selectors, etc.)
  • IE versions 6-8 have little support for CSS3, but there are some workarounds14

There are many more bugs, issues, and inconsistencies that can arise in Internet Explorer, but these are probably the most common and most important ones to keep in mind when trying to create a cross-browser experience. I encourage all developers to do further research on many of the issues I’ve mentioned above in order to have a more accurate understanding of what problems these issues can cause, and how to handle them.

Some Things Will Never Look the Same

As already mentioned, creating the exact same experience visually and functionally in every browser is possible, but unlikely. You can get the layout and positioning of elements close to pixel-perfect, but there are some things that are out of the developer’s control.

Forms Will Often Look Different

Discussing all the differences and quirks that occur with form elements across the different browsers and platforms could be an article in itself, so I won’t go into extensive details here. A simple visual demonstration, however, should suffice to get the point across.

Take a look at the image below, which displays the <select> elements on the Facebook15 home page, as shown in 5 different browser versions (screenshots taken from Adobe’s Browserlab16):

The Facebook sign-up form in different browsers
<select> elements appear differently in different browsers

Some form elements can be controlled visually. For example, if the client requires that the submit button looks the same in every browser, that’s no problem, you can just use an image, instead of the default <input type="submit">, which, similar to <select> elements, will look different in different browsers.

But other form elements, like radio buttons, textarea fields, and the aforementioned <select> elements, are impossible to style in a cross-browser fashion without complicating matters using JavaScript plugins17 (which some developers feel harm the user experience).

Typography Will Always Look Different

Another area in which we can’t expect pixel-perfect designs is with regards to fonts, particularly fonts on body copy. Different methods18 have sprung up to help with custom fonts in headers, and the recently launched Google Font API19 will contribute to this. But body copy will probably always look different in different browsers.

With typography, we not only face the problem of font availability on different machines, but in some cases even when the font is available on two different machines, the type will look different. Windows ClearType20, for example, is available on IE7, but not on IE6, causing the same font to look different on two different versions of IE.

The graphic below displays screenshots from A List Apart21 on IE6 and IE7. The grainy text in IE6 is more evident on the heading than in the body copy, but all text displays a marked difference between the two browsers (unless of course the text is an image):

A List Apart's typography compared in IE6 and IE7
A List Apart’s typography compared in IE6 and IE7

Use a CSS Reset

Ever since I started using a CSS reset for my projects, my ability to create a cross-browser experience has greatly increased. It’s true that most resets will add unnecessary code to your CSS, but you can always go through and remove any selectors that you know will not be a factor (for example, if you don’t plan to use the <blockquote> tag, then you can remove reference to it, and repeat this for any other unused tags).

Many of the margin- and padding-related differences that occur across different browsers become more normalized (even in troublesome HTML forms) when a CSS reset is implemented. Because the reset causes all elements to start from a zero base, you gain more control over the spacing and alignment of elements because all browsers will begin from the same basic settings.

CSS Reset
A CSS reset as shown in Firefox’s developer toolbar

Besides the benefits of producing a cross-browser experience, a reset will also be beneficial because you won’t use as many hacks, your code will be less bloated, and you’ll be much more likely to create maintainable code. I recommend Eric Meyer’s CSS reset22, which I’ve been using for quite some time now.

Use SitePoint’s CSS Reference

If you’re having trouble getting a particular CSS property to display correctly across all browsers, look up the property in the SitePoint CSS Reference23 to see if it has any compatibility limitations. SitePoint’s reference (which is also available as a hard copy24, though not as up to date), includes a useful compatibility chart that displays browser support for every standard CSS property.

SitePoint's Compatibility Charts for CSS Properties
SitePoint’s Compatibility Charts for CSS Properties

Each compatibility chart is accompanied by a fairly detailed description of the bugs that occur in different browsers, and users are permitted to add comments to document new bugs that come up and to provide further explanations on complex CSS issues.

Using this as a guide, you can narrow down the possibilities, and can usually determine whether or not a CSS issue is due to a browser bug, or due to your own misapplication or misunderstanding of the CSS property in question.

Conclusion

While there is so much more that could be discussed on the topic of cross-browser CSS, the principles and guidelines I’ve introduced here should provide a foundation to assist CSS developers in creating as close to a consistent cross-browser experience as is currently possible. Cross-browser CSS is an attainable goal, within reasonable limits.

But, as an epilogue to this article, I also would like to concur with those promoting the use of CSS3 with progressive enhancement25, and encourage developers to push new CSS techniques to the limits, even doing so, where possible, on client projects.

The principles I’ve introduced here should help developers create a beautiful and intuitive experience in IE, while providing an extra-beautiful and super-intuitive experience in the more up-to-date browsers. That’s a cross-browser goal that is definitely worth striving for.

Footnotes

  1. 1 http://dowebsitesneedtolookexactlythesameineverybrowser.com/
  2. 2 http://reference.sitepoint.com/css/collapsingmargins
  3. 3 #comments
  4. 4 http://css-tricks.com/the-css-ah-ha-moment/
  5. 5 http://www.smashingmagazine.com/2009/10/19/the-mystery-of-css-float-property/
  6. 6 http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/
  7. 7 http://www.my-debugbar.com/wiki/IETester/HomePage
  8. 8 http://www.smashingmagazine.com/2009/10/14/css-differences-in-internet-explorer-6-7-and-8/
  9. 9 http://haslayout.net/css/Disappearing-Content-Bug
  10. 10 http://www.impressivewebs.com/ie6-ghost-text-bug-with-multiple-solutions/
  11. 11 http://reference.sitepoint.com/css/haslayout
  12. 12 http://www.satzansatz.de/cssd/onhavinglayout.html
  13. 13 http://www.impressivewebs.com/buggy-css-selectors-cross-browser-jquery/
  14. 14 http://www.smashingmagazine.com/2010/04/28/css3-solutions-for-internet-explorer/
  15. 15 http://www.facebook.com
  16. 16 https://browserlab.adobe.com
  17. 17 http://widowmaker.kiev.ua/checkbox/
  18. 18 http://www.smashingmagazine.com/2009/10/22/rich-typography-on-the-web-techniques-and-tools/
  19. 19 http://code.google.com/apis/webfonts/
  20. 20 http://www.microsoft.com/typography/cleartype/tuner/step1.aspx
  21. 21 http://www.alistapart.com/
  22. 22 http://meyerweb.com/eric/tools/css/reset/
  23. 23 http://reference.sitepoint.com/css
  24. 24 http://www.sitepoint.com/books/cssref1/
  25. 25 http://perishablepress.com/press/2010/01/11/css3-progressive-enhancement-smart-design/

↑ Back to top Tweet itShare on Facebook

Louis Lazaris is a freelance web developer and author based in Toronto, Canada. He blogs about front-end code on Impressive Webs and curates Web Tools Weekly, a weekly newsletter for front-end developers.

Advertisement
  1. 1

    The only thing missing from these comments is someone mentioning Hitler.

    Loved the article. I was able to understand what the author meant about using IE at the beginning of the process instead of the end and not get in a self-righteous tizzy and call the author names. It’s a nice piece of advice.

    A friend of mine has stopped reading any comments on Teh Internets and I think this proves his point. I think I’m done, too.

    0
  2. 252

    I wanna say that above one is a great effort and I’m very pleased that you did this for all of us.
    Thanx a bundle bro

    0
  3. 503

    It’s sad that this article has to exist, but I’ve been bitten by many bugs listed… I hate IE, thank god the % is dropping rapidly for 7/8…

    steve

    0
  4. 754

    Personally I think IE can go to hell; all of the hours wasted trying to fix mistakes and bugs that Microsoft was too daft to correct themselves. Even IE’s debugging tool is antiquated, clunky and does not afford you the kind of microscopic analyis of page elements the way modern browsers can, making the solving of all these massive IE problems even more tiresome of. A global boycott and ban on all Microsoft products should be put forward. We, as developers and designers, need to say enough with Microsoft’s B-Grade software, we are going to use something from this decade.

    0
  5. 1005

    I think explorer is now coming with different issues, all of my progaming i first test it in mozilla. My website came out really good when i tested in Mozilla and it showed really bad in Explorer, but now its fixed andit looks really good. It’s Called distance calling

    0
  6. 1256

    You can use a javascript plugin named Formalize (http://formalize.me/) to standardize your form. Or we could say it is a form reset CSS.

    1
  7. 1507

    Nachiket Bhandarkar

    May 24, 2013 7:32 am

    IE is is the most pathetic browser I have experienced coding in until now. The same code works differently in newer versions of IE. Fierfox and Chrome are good to start your work. Agree with Ben.

    1
  8. 1758

    Cross browser compatibility is more important when talking about client-side scripting like javascript than css. Few end users will even see the same webpage in different browsers. If they are a FireFox user they will use, you guessed it, FireFox, and so on…

    0
  9. 2009

    Let IE rest in peace already.

    0
  10. 2260

    danmoore.com.au

    June 7, 2010 4:56 am

    I would like to expand on what Alexey has said above. While I don’t necessarily agree with this method (each to their own in what works best for them) there are some major added bonuses underlying in that statement.

    The added benefit to this is, if you work in a corporate environment whereby you don’t do the design and initial research on the users, by understanding the browsers used by the patrons you are understanding part of your target audience and can then decide on certain aspects of the build which may be practical/impractical for the UX, based on that information.

    Example: should you want to add some nice tricks such as fading I a transparent 24-bit PNG, you know you that you’ll probably run into some IE issues in handling the transformation of alpha channels (messy black box during transition while the browser handles opacity change).

    You’ll not only save yourself unwanted build time, which may not exist in the budget, but you’ll learn a thing or two about the website users and how to direct give them the best UX possible.

    Make yourself a better designer/developer and research your users first.

    1
  11. 2511

    So you’re sure that, when you design your site in Firefox, it will look the same on Safari/Chrome (webkit), and Opera? You do realize those browsers all have different rendering engines? So you do realize that, even when all those browsers you’ve mentioned are modern browsers, they will never render one page the same? Even with the same engine but different operating systems.
    I agree that, developing on Firefox is made easy with Firebug and stuff. But as the author says, you should always go with the bad, then make it good. I disagree with your approach by making the good and then reverse engineer it so it works in less modern browsers (IE and stuff). When I do that while developing websites, I spend too much time fixing issues for IE, when, if you start with IE, those problems will not arise or be less time consuming.

    -5
  12. 2762

    Vitaly Friedman (Smashing Magazine)

    June 7, 2010 4:58 am

    I must agree with Ben here. In my personal experience working with IE first actually results in the code which is extremely difficult to maintain over a long period of time. In particular, I believe it would be a big mistake to design for IE6 first and then launch other browsers and tweak for them. It just makes more sense to start designing in the right environment and fix problems in the “less comfortable” environments after that.

    1
  13. 3013

    If we will use IE first then we can catch big mistakes at early stage.

    0
  14. 3264

    Another agreement with Ben. I test frequently on different browsers but only fix IE6’s issues later. I don’t do CSS hacks and, since I have an IE6-specific style sheet, I can always drop it later when the blessed day comes where I don’t have to support it any longer.

    1
  15. 3515

    Quite agree – only bring IE into the equation when you reach a milestone, such as when the basic layout has been worked out, and then ignore it again until all of the subtle little tweaks have been put in place. Then make sure that no-one else is around to hear the swearing…

    0
  16. 3766

    I believe that if we want to see standards become standards across all browsers, we, the developers, should stop coding for the browsers that require non standard hacks and modifications for a page to render similarly.

    I personally am in the desktop and mobile web app business, and I have completely stopped coding for anything other than Webkit browsers. I do not support even IE9, or FireFox as I believe they do not render a page as accurately, as predictably and as consistently as Safari of Chrome.

    The big corporations like Microsoft think that they are the ones leading the board when it comes to ‘standards’ but they are often the ones misleading the direction HTML, CSS and JavaScript should take.

    We are the ones coding the pages people visit with a chosen web browser… shouldn’t they ask our opinion on how things should work?

    1

↑ Back to top