December 06, 2005
Curious George: CSS & browsers
I'm just starting to work with Cascading Style Sheets and I find (of course) that IE displays everything differently from Firefox. (Haven't even tried Opera yet) What gives? How do webdesigners "fix" something like that?
The sad part for me is that in IE it looks "correct", but in Firefox it's all jumbled up. Every component is in the right general location, just scrunched all together. Someone told me I needed to get a javascript that determines the browser and then load 2+ different .css files depending. Anyone know?
<style type="text/css" media="screen">@import "link/to/normal.css";</style> <!--[if IE 6]> <style type="text/css" media="screen">@import "link/to/IE.css";</style> <![endif]-->
That drops in a link to a CSS file for all browsers (in this case normal.css) and a second sheet (in this case named IE.css) that contains the re-written or alternate style rules for IE. You can embed these rules into a single sheet by using a bunch of weird hacks but this is a cleaner and more backwards- and forwards-compatible way to do it. Only IE will see IE.css; all other browsers will interpret anything contained between if and endif as a comment, and will ignore it.float: XXX;
” on an element, also add “display: inline;
”; it's nonsensical, but stops IE from doubling margins. 3. Design in Firefox first, and hack for IE afterwards. These points will reduce a lot of the biggest frustrations, in my experience.