Read CSS: The Definitive Guide, 3rd Edition Online
Authors: Eric A. Meyer
Tags: #COMPUTERS / Web / Page Design
It's possible that a markup language might not
contain enough elements to fully represent tables as they are defined in CSS, or that
an author will forget to include all the necessary elements. For example, consider
this XHTML:
Name: |
You might glance at this markup and assume that it defines a two-cell table of a
single row, but structurally, there is no element defining a row (because thetr
is missing).
To cover such possibilities, CSS defines a mechanism for inserting "missing" table
components as anonymous objects. To illustrate how this works, let's revisit our
missing-row XHTML example. In CSS terms, what effectively happens is that an
anonymoustable-row
object is inserted between thetable
element and its descendant table cells:
Name: |
A visual representation of this process is given in
Figure 11-2
.
Figure 11-2. Anonymous object generation in table formatting
Seven different kinds of anonymous-object insertions can occur in the CSS table
model. Like inheritance and specificity, these seven rules are, an example of a
mechanism that attempts to impose intuitive sense on the way CSS behaves.
If atable-cell
element's parent is
not atable-row
element, then an
anonymoustable-row
object is inserted
between thetable-cell
element and its
parent. The inserted object will include all consecutive siblings of thetable-cell
element. Consider the
following styles and markup:
system {display: table;}
name, moons {display: table-cell;}Mercury 0
The anonymoustable-row
object is
inserted between the cell elements and thesystem
element, and it encloses both thename
andmoons
elements.
The same rule holds true even if the parent element is atable-row-group
. To extend the example, assume
that the following applies:
system {display: table;}
planet {display: table-row-group;}
name, moons {display: table-cell;}Mercury 0 Venus 0
In this example, both sets of cells will be enclosed in an anonymoustable-row
object that is inserted
between them and theplanet
elements.
If atable-row
element's parent is not
atable
,inline-table
, ortable-row-group
element, then an anonymoustable
element is inserted between thetable-row
element and its parent. The inserted
object will include all consecutive siblings of thetable-row
element. Consider the following styles and markup:
docbody {display: block;}
planet {display: table-row;}Mercury 0 Venus 0
Because thedisplay
value of theplanet
elements' parent isblock
, the anonymoustable
object is inserted between theplanet
elements and thedocbody
element. This object will enclose bothplanet
elements because they are consecutive
siblings.
If atable-column
element's parent is
not atable
,inline-table
, ortable-column-group
element, then an anonymoustable
element is inserted between thetable-column
element and its parent. This is
much the same as thetable-row
rule just
discussed, except for its column-oriented nature.
If the parent element of atable-row-group
,table-header-group
,table-footer-group
,table-column-group
, ortable-caption
element is not atable
element, then an anonymoustable
object is inserted between the element and its
parent.
If a child element of atable
orinline-table
element is not atable-row-group
,table-header-group
,table-footer-group
,table-row
, ortable-caption
element, then an anonymoustable-row
object is inserted between thetable
element and its child element. This anonymous object spans all of the
consecutive siblings of the child element that are nottable-row-group
,table-header-group
,table-footer-group
,table-row
, ortable-caption
elements. Consider the following markup and styles:
system {display: table;}
planet {display: table-row;}
name, moons {display: table-cell;}Mercury 0 Venus 0
Here, a single anonymoustable-row
object will be inserted between thesystem
element and the second set ofname
andmoons
elements.
Theplanet
element is not enclosed by the
anonymous object because itsdisplay
istable-row
.
If a child element of atable-row-group
,table-header-group
, ortable-footer-group
element is not atable-row
element, then an anonymoustable-row
object is inserted between the element and its child
element. This anonymous object spans all consecutive siblings of the child
element that are nottable-row
objects
themselves. Consider the following markup and styles:
system {display: table;}
planet {display: table-row-group;}
name, moons {display: table-cell;}Mercury 0 Venus 0
In this case, each set ofname
andmoons
elements will be enclosed in an
anonymoustable-row
element. For the
second set, the insertion takes place in accord with Rule 5. For the first
set, the anonymous object is inserted between theplanet
element and its children because theplanet
element is atable-row-group
element.
If a child element of atable-row
element is not atable-cell
element, then
an anonymoustable-cell
object is
inserted between the element and its child element. This anonymous object
encloses all consecutive siblings of the child element that are nottable-cell
elements themselves. Consider the
following markup and styles:
system {display: table;}
planet {display: table-row;}
name, moons {display: table-cell;}Mercury 0
Because the elementnum
does not have
a table-relateddisplay
value, an
anonymoustable-cell
object is inserted
between theplanet
element and thenum
element.
This behavior also extends to the encapsulation of anonymous inline
boxes. Suppose that thenum
element was
not included:
Mercury
0
The0
would still be enclosed in an
anonymoustable-cell
object. To further
illustrate this point, here is an example adapted from the CSS
specification:
example {display: table-cell;}
row {display: table-row;}
hi {font-weight: 900;}This is the
toprow.This is the
bottomrow.
Within eachrow
element, the text
fragments andhi
element are enclosed in
an anonymoustable-cell
object.
To
assemble a table's presentation, CSS defines six individual "layers" on which the
various aspects of a table are placed.
Figure
11-3
shows these layers.
Figure 11-3. The formatting layers used in table presentation
Basically, the styles for each aspect of the table are drawn on their individual
layers. Thus, if thetable
element has a green
background and a one-pixel black border, then those styles are drawn on the lowest
layer. Any styles for the column groups are drawn on the next layer up, the columns
themselves on the layer above that, and so on. The top layer, which corresponds to
the table cells, is drawn last.
For the most part, this is simply a logical process; after all, if you declare a
background color for table cells, you would want that drawn over the background for
thetable
element. The most important point
revealed by
Figure 11-3
is that column
styles come below row styles, so a row's background will overwrite a column's
background.
It is important to remember that, by default, all elements have transparent
backgrounds. Thus, in the following markup, thetable
element's background will be visible "through" cells, rows, columns,
and so forth that do not have a background of their own, as illustrated in
Figure 11-4
:
hey | there |
what's | up? |
tiger | lilly |
Figure 11-4. Seeing the background of table-formatting layers through other layers