[OSCON 2008] CSS for High Performance JavaScript UI by Gavin Doughtie
Gavin is a front end developer on Google’s Picasa Web Albums and a contributor to the dojo toolkit. The original designer tools for Web 1.0 were the FONT tag, TABLE hacks and spacer gifs. Now we live in the future (shows a picture of the iPhone). Maybe it's time to accept that we're stuck with CSS. "I'm a coder, not a designer" is what happens at OSCON.
There are costs of JavaScript: development, download, parse time and runtime performance. It's extremely powerful and high-level, but it's slow because it's interpreted. Drawing a box with JavaScript in Firefox 3 isn't too difficult. Another way to do the same thing is with CSS. Doing the same thing in CSS is much faster and requires less code. Surrender To Win. You don't have to code it all. You can hand off part of that to your runtime environment (browser).
CSS Fundamentals
You want to how things flow in a page. Browsers were originally designed to render text. They're built to render flowing text. Other important fundamentals include float, positioning, negative margins, relative units, pseudo-selectors. Lastly, you need to know when to use tables. Gavin is now showing an example of using CSS to modify an image so it scales with the browser window. It's pretty simple:
body { margin: 0; padding: 0; height: 100%; width: 100%; overflow: hidden; } img { position: relative; width: 100%; height: 100%; }
Relatively Absolutely: "Absolute" in relative to position:relative
. If an element has absolute positioning, it will only be positioned relative to it's parent element. To draw a box on an image, it's usually best to calculate the percentage position and sizes in script or on the server. The world's greatest psuedo-selector is :hover
. Unfortunately, :hover
doesn't work on anything but <a> in IE 6.
With floats, you can "float" elements to the top, right, bottom and left. In Firefox and Safari, you can set a border-radius to draw lines that are curved. Before Firefox 3, these lines where terrible looking.
Tables make a lot of sense when you have a grid layout. Use table-layout:fixed
if you don't want table cells to be the width of their contents.
For sizing images and other elements, it's usually best to use em
for the height and width and relative measurements. You can then use style.fontSize
to resize the images. Using this technique is must faster than using JavaScript to set the height and width on an image.
WebKit, the CSS Wonderland. Gavin loves WebKit. It's on Safari (Windows, OS X and iPhone), Android, GNOME and KDE. With WebKit, you can build expanding lists w/o any coding. To do gradients in WebKit, you use:
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#00abeb)); -webkit-border-radius: 0.24em;
To do animation, you can use:
-webkit-transition-property: width, height; -webkit-transition-duration: 250ms; -webkit-transition-timing-function: default;
Another thing you can do with WebKit is rotate images (using -webkit-transform: rotate()
) and just about anything (divs, forms, etc.). WebKit also supports SVN masks, which means you can open up a hole and view an image through it.
Gecko's pretty cool too. Firefox 3 introduced a new rendering engine based on Cairo. You can do an SVG transform in Firefox 3 to create reflections of elements (divs) in your page.
"With browsers, you cut people off at the knees, but everyone's the same height." -- Alex Russell
IE 6 is not the problem, we're the problem. If you drop support for IE 6, you won't have to worry about coding towards it. It might not be possible if you work for a large company, but if you're small, you should definitely think about it hard.
It's funny to think that IE 6 is the new Netscape 4.
Gavin's slides from this talk will be available at http://xdraw.org/oscon2008.html in the next few days.
Posted by Gavin Doughtie on August 03, 2008 at 07:41 AM MDT #