Rebuilding a Site With Standards
Eric A. Meyer
Netscape Communications
Redesigning a Site With Standards
Lining Up For Takeoff
- We'll be talking about today and tomorrow, not yesterday
- Most of you know how to handle the old; let's look at the new
- Wish we had time to explore v4-era compromises, but...
- It is possible to balance new-browser styling and old-browser concessions
- Exercise based on Project 1 of Eric Meyer on CSS
Redesigning With Standards is...
- ...not as scary as it sounds at first
- ...a smoother path to the future of the Web
- ...not about dumping older browsers
- ...a way to reduce markup and page complexity
- ...not about throwing away tables!
A Typical Page
- The usual features: a masthead, three columns, a footer
- Actually we have fairly simple nested tables here
font
elements are used to set font family and text color
- Spacer GIFs hold open spaces between columns
- Page weight: 10.36KB (plus images), 16 spacer GIFs
Visualizing structure
- Use CSS to "sketch out" the structure of a page
- Good for diagnosing table-layout problems
- Great for knowing how elements are nested
- Borders are good, but can have layout implications
- Backgrounds avoid layout shifting, but can be jarring
Step One: Flush the fonts
- We're going to be setting our fonts (and their sizes) with CSS
- They're usually highly redundant anyway
- We'll get rid of the non-breaking spaces as well
- So let's check the end result of a major find-and-replace
- Page weight: 7.08KB
Step Two: Strip the Spacers
- Again, the space those images hold open will be handled with CSS
- Drawing all those images slows page rendering
- While we're at it, the elements containing the images will be purged
- Again, after some find/replace and manual stripping...
- Page weight: 5.79KB
Step Three: Further Simplification
- Rip out background and alignment attributes
- The page title and pullquote both can be made much simpler
- Navigation sidebar can be replaced with an unordered list
- On the left, we have a few lists separated by headings
- Page weight: 4.06KB
Step Four: Add Class and ID
- Add classes to things that have common aspects—external links, asides, captioned pictures, etc.
- Classes are more commonly used because they were a little better supported in old browsers
- IDs are quite safe, and woefully underused
- Add IDs to unique elements—navigation sidebars, mastheads, footers, etc.
- Extensive use of IDs can give you the effect of writing "XML lite"
Let the REstyling Begin!
- We'll basically be trying to recreate the effect of the original HTML-based presentation
- Straight copies of color values is easy enough
- For the spacer GIFs, we'll have to use pixel lengths and factor in the effects of cellpadding and cellspacing
- In real life, there's a lot more experiment-and-reload
- The first steps are pretty simple: recreate the
body
attributes and set some "global" styles
The Top of the Page
- We need to nail down the width of the advertisement and let the title slide
- The title gets its background back via CSS
- Other font, color, and adjustment effects are dropped in as needed
- A rough equivalent to
-1
is 85%
The Navbar
- Again, there was a defined width here, so we'll set it via CSS
- The list needs to lose its bullets and indentation
- The separators between links are created as borders on the list items
- A little extra padding on the items spaces them out subtly
Recreating the Content
- We'll need to recreate the spacers with padding on the right and left of the table cell
- The page's title, now in an
h1
, is all we need to set up the original appearance
- Similarly, we can set up the pullquote to look like it did before
- The big advantage here is flexibility: changing the CSS is a lot easier than hacking nested tables
Basic Styles for the Summary
- A reasonable approximation of
-2
is 66%
- We reduce the width by a couple of pixels to simulate the effects of cellspacing
- We also want to recreate the cellpadding of the ratings table
- Now that we have simple heading elements, styling them is a snap
More Summary Styles
- As before, we need to eliminate the bullets and indentation of the list
- The even-odd classes on the ratings table make alternate-row highlighting really simple
- We set the colors of the actual ratings via their classes as well
- The travel tip is (again) easy to style via its
class
Fixing the Footer
- First we need replacements for the alignment attributes
- Then the background color and the "separator" can be handled with background colors and some top borders
- The copyright gets some special styles of its own
- ...and we're done!
Comparing the Two
Approach |
HTML size |
CSS size |
Page weight |
HTML-only |
10.36KB |
-- |
10.36KB |
HTML+CSS |
4.30KB |
2.33KB |
6.63KB |
Savings |
6.06KB |
-2.33KB |
3.73KB |
- HTML: 58.5% reduction
- Markup: 36% reduction
- Image count: dropped from 17 to 1, plus one background
Taking Things Further
- Let's try reducing the table layout even further
- We're not going to kill off all the tables, mostly because we don't have time
- Let's convert the three column cells into
div
s (which requires adjusting the table open and close tags)
Positioning (And Not)
- We're going to position the navbar and summary columns, but not the main content
- Why? We need the main column to keep the footer at the bottom of the page
- We'll also need to clear space for the positioned columns
- By converting the columns to
div
s, we've "broken" the font sizing
- How do we make sure the side columns don't overlap the mathead, and what about the navbar's background?
Establishing a Container
- Absolutely positioned elements are placed with respect to their containing block
- If there isn't one, the root element acts as the containing block
- Any positioned element acts as a containing block for its descendants
- So we'll wrap the main content of the page in a
div
and position it
Filling Out the Navbar
- A positioned element is typically only as large as its content
- You can resize an element, but it's difficult to make it the same height as another element
- However, since positioned elements sit above non-positioned elements, we can cheat
- We'll set a 120-pixel-wide border on the main
div
and remove the margin
Re-Adjusting the Footer
- Since we had to turn this into its own table, the connection to the column widths has been broken
- Fortunately, all we need to do is declare the same widths we're using for the columns
Some Caveats
- Browsers deal with padding on table cells differently
- In theory, padding should be added to width (as it is in regular block-level elements) but Explorer doesn't act that way
- Thus mixing positioned and table-bound layout elements can be a problem, at least if you're using padding
- You could set no padding on the cells and wrap the content in
div
s
- Better to avoid mixing: be consistent in how basic design elements are enforced
Wrapping It Up
- By stripping markup down to its basics, you can dramatically reduce the page weight
- The stylesheet adds a bit of weight back, but you can externalize it and then make the user download it only once
- Restyled lists can be used for a lot of purposes
- Positioning requires some extra work, but can be worth it
- Try not to mix tables and positioning for creating basic design elements