Employing Auto to Control Layout
Of the four possible zones of a Web page — content, padding, border, and margin — only two, the element’s content and optional margin, can be set to auto. Auto allows the browser to automatically resize content and margins: This enables you to do some pretty interesting things, such as stabilizing a graphic in the center of the Web page, no matter how the user resizes the browser.
If you specify left and right margins, but don’t specify width, an element stretches its width to accommodate and maintain the requested margins. For example, if you specify that the left and right margins should be 150 pixels, the width property of the paragraph then becomes (of necessity) auto.
However, at the time of this writing, Internet Explorer version 6 chokes on the preceding code and simply sticks the paragraph against the left side (using no left margin). Mozilla Firefox and other browsers get it right.
This is one of the few instances where Internet Explorer 6 fails to correctly interpret a CSS style. This technique of setting margin left and right to auto is technically the correct way to center and freeze the size of elements in a CSS style. I’m hoping that IE gets on the bandwagon soon.
Using! DOCTYPE to force IE to comply
This! DOCTYPE declaration can have unintended side effects such as changing proprietary IE behaviors that you might be exploiting (relative font sizes, for example, can render differently). But if you want to try using the declaration, it does force IE 6 to switch to its CSS standards-compliant mode.
Vertical Positioning with Auto
Just as you can set three properties to auto when sizing and positioning horizontally (width and the left and right margins), so, too, can you in theory use the auto value with the equivalent vertical properties: height and the top and bottom margins.
Strangely, centering an element when using normal flow (the default) is rather difficult because if you try to use auto as the value for the margin-top or margin-bottom properties, the value is evaluated as zero! Auto is just ignored in this case. Recall from the discussion of horizontal centering earlier in this chapter that auto margins can be used for centering — and indeed using auto margins is the recommended technique for CSS centering.
Handling Inline Elements
In CSS, an inline element is a small element right within a line of text (such as the or elements). You can fool around with inline elements in text in many ways: drawing borders around individual words or phrases, padding, including margins, inserting extra-large characters or words, dropping graphic images into the middle of a paragraph, and other annoying tricks.
Truthfully, I’ve yet to see — or even imagine — a use for these kinds of inline element manipulations, but I’m equally sure that someone, somewhere has invented a clever technique involving just these properties applied to just these inline elements.