Though I cringe (as AKMA surely knew I would) to hear anything I’ve done with his template redesign called “art,” I am reasonably pleased with it as craft, and I very much appreciate his kind praises.
I do want to quibble briefly with this, however: ‘Dorothea can explain why things look the way they do, whereas when I edit my page I take a sort of “Let�s see what happens if I put this tag in” approach.’ AKMA? I do the same thing. All the time. That’s how I learn. That’s how lots of people learn. There’s nothing wrong with that.
You’ve had that part of the process hidden from you to a large extent during this redesign, because I didn’t post something until I was reasonably happy with it. The behind-the-scenes fiddling and swearing (I have a terrible mouth on me while I’m coding) you didn’t see or hear, lucky you.
Positioning large chunks of a page using CSS brings out the worst in many coders, not just me. (Bet you were wondering how I’d segue into the main topic, huh?) A few central notions, however, suffice to permit individual experimentation and appropriate expostulation.
HTML was originally envisioned as presenting very sequential kinds of texts, like papers in scientific journals. The only way to move out-of-line was via the hyperlink. Save for that, elements simply marched down the page one after another. Nothing wrong with that—it’s just boring. That’s why papers in scientific journals do it. (Isn’t it?)
There is, however, one gaping hole in the otherwise-sequential presentation of material in HTML: the table, which presents things side-by-side as well as top-to-bottom. It didn’t take long for some bright web designer to figure out that table markup could place, say, a sidebar next to the main content of the page. And thus was table-based layout born.
The semantic-markup wonks held up their hands in utter horror at this. Tables mean rows and columns of boring-but-important data, not sidebars and cute little menus, they cried. And they looked at the CSS working group and sternly said, “Make it possible to do better.”
The CSS working group came up with three (more or less) angles on making layout tables unnecessary. The first I will deal with only briefly, as AKMA’s template doesn’t use it: the single-image background, to an entire page or to part of it. This is set via the background-image
property, which takes a value url('URLtoimage')
. The background-repeat
property controls whether the background appears once (value no-repeat
), repeats horizontally only (value repeat-x
), repeats vertically only (value repeat-y
) or repeats both horizontally and vertically (value repeat
). The background-attachment
property (values fixed
and scroll
) determines whether the image scrolls along with the text or stays fixed in place. (You can see fixed background images in—er, inaction at Stavros’s and Kalilily’s blogs.) Positioning the image on the page is done with the background-position
property.
Whew. Enough of that.
The second attack by CSS on layout tables is the float
property. This allows you to put one block (such as a pull quote or an image) alongside the block that appears immediately after it in the markup. The possible values are left
and right
, and they put the floated content to the left or right of the following content, respectively.
Floats can be used for major chunks of a page such as sidebars—CavLec’s sidebar used to be a float—but they aren’t ideal for the purpose. For one thing, they constrain the order of the major chunks of your markup, which may not be desirable. For another, as I learned with CavLec, large floats can take a while to render, and some browsers make them do weird leaps across the page. Play with floats by all means, but I don’t recommend them as your principal layout device.
What’s left that works? A gizmo called “absolute positioning.” (There is also a gizmo called “relative positioning,” but last I checked support for it was so lacking that I don’t think it’s worth talking about.) This gizmo is what it sounds like. Pick up a block, plunk one corner into a specific spot, set a width, let the browser handle the rest.
To absolutely position a block, set the position
property to absolute
. Then figure out where you want the top left corner of the block to sit. With a top-level block like a sidebar, you will be figuring this relative to the browser screen; with a block inside another block, you will be figuring this relative to the outer block. You can figure in pixels, ems, percents, or any of the other standard CSS length units. I used ems for AKMA’s page, which worked moderately well after some futzing.
Once you have numbers, set the top
and left
properties appropriately, set the width
property to something reasonable, and there you are.
Or almost. You probably have some overlapping blocks at this point. CSS totally ignores the position of an absolutely positioned block when positioning other blocks on the page, happily allowing one block to run roughshod over another.
There are a few ways to fix this—you could simply absolutely-position everything—but I find that it’s easier to wrap my mind around a layout if the main chunk uses margins rather than absolute positioning to avoid absolutely-positioned chunks to either side.
Say what? Well, say I absolutely position a sidebar with top: 13em
, left: 0
and width: 25%
, that last meaning I want the sidebar to whack out 25% of the browser screen for itself. If I set margin-left
on the main chunk of blog text to, say, 28%, there can be no crashes and I still haven’t needed to absolutely position the main text chunk.
(To absolutely position? To position absolutely? To absolute-position? Sorry, spot of infinitive trouble there.)
You will need to employ the Box Model Hack to fix widths in IE5/Win, but you’re probably used to that by now.
Advanced students will want to look into fixed positioning and funky z-index tricks, but be aware that these can bite, injecting nasty unwanted scrollbars and such into your nice layout. With CSS background properties, floats, and absolute positioning, however, most table layouts can go the way of the dinosaur. The sooner, the better.