When an element is positioned absolutely, it is completely removed
from the document flow. It is then positioned with respect to its
containing block, and its edges are placed using the side-offset
properties. The positioned element does not flow around the content
of other elements, nor does their content flow around the positioned
element. This implies that an absolutely positioned element may
overlap other elements, or be overlapped by them. (We'll see
how you can affect the overlapping order at the end of the chapter.)
Remember that the containing block of an absolutely positioned
element is not necessarily its parent element. In fact, it often is
not, unless the author takes steps to correct this situation.
Fortunately, that's easy to do. Just pick the element that you
want to use as the containing block for the absolutely positioned
element, and give it a position of
relative with no offsets. Thus:
P.contain {position: relative;}
Figure 9-19. The effects of absolute positioning
For the most part, the text in both paragraphs looks fairly normal.
In the second one, however, the place where the boldface element
would have appeared is simply closed up, and the positioned text
overlaps the some of the content. There is no way to avoid this,
short of positioning the boldfaced text outside of the paragraph (by
using a negative value for right) or by specifying
a padding for the paragraph that is wide enough to accommodate the
positioned element. Also, since it has a transparent background, the
parent element's text shows through the positioned element. The
only way to avoid this is to set a background for the positioned
element.
Figure 9-20. An absolutely positioned paragraph
The paragraph is now positioned at the very beginning of the
document, half as wide as the document's width and overwriting
the first few elements!
In addition, if the document is scrolled, the paragraph will scroll
right along with it. This is because the element's containing
block is the BODY element's content area,
not the viewport. If you want to position elements so that
they're placed relative to the viewport and don't scroll
along with the rest of the document, then the next section is for
you.
SPAN.change {position: absolute; top: static-position; left: -5em; width: 4em;
font-weight: bold;}
P {margin-left: 5em; position: relative;}
<P> Lorem ipsum, dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut <SPAN CLASS="change">***</SPAN>
laoreet dolore magna aliquam erat volutpat.</P>