📄 tables.html
字号:
</div></div><h2><a name="q7">17.5 Visual layout of table contents</a></h2><P>Like other elements of the <ahref="conform.html#doclanguage">document language</a>, internal tableelements generate rectangular <ahref="box.html#box-dimensions">boxes</a> with content, padding, andborders. They do not have margins, however.<p>The visual layout of these boxes is governed by a rectangular,irregular grid of rows and columns. Each box occupies a whole numberof grid cells, determined according to the following rules.These rules do not apply to HTML 4.0 or earlier HTML versions; HTML imposes its own limitations on row and column spans.</p><ol><li>Each row box occupies one row of grid cells. Together, the rowboxes fill the table from top to bottom in the order they occur in thesource document (i.e., the table occupies exactly asmany grid rows as there are row elements).<li>A row group occupies the same grid cells as the rows it contains.<li>A column box occupies one or more columns of grid cells. Columnboxes are placed next to each other in the order they occur. The firstcolumn box may be either on the left or on the right, depending on thevalue of the <a href="visuren.html#propdef-direction" class="noxref"><span class="propinst-direction">'direction'</span></a>property of the table.<li>A column group box occupies the same grid cells as the columns itcontains. <li>Cells may span several rows or columns. (Although CSS2 doesn'tdefine how the number of spanned rows or columns is determined, a useragent may have special knowledge about the source document; a futureversion of CSS may provide a way to express this knowledge in CSSsyntax.) Each cell is thus a rectangular box, one or more grid cellswide and high. The top row of this rectangle is in the row specifiedby the cell's parent. The rectangle must be as far to the left aspossible, but it may not overlap with any other cell box, and must beto the right of all cells in the same row that are earlier in thesource document. (This constraint holds if the 'direction' property ofthe table is 'ltr'; if the <a href="visuren.html#propdef-direction" class="noxref"><spanclass="propinst-direction">'direction'</span></a> is 'rtl', interchange"left" and "right" in the previous sentence.)<li>A cell box cannot extend beyond the last row box of a table orrow-group; theuser agents must shorten it until it fits. </ol><p><strong>Note.</strong>Table cells may be relatively and absolutely positioned, butthis is not recommended: positioning and floating remove a boxfrom the flow, affecting table alignment.<div class="html-example"><p>Here are two examples. The first is assumed to occur in an HTMLdocument:<pre><TABLE><TR><TD>1 <TD rowspan="2">2 <TD>3 <TD>4<TR><TD colspan="2">5</TABLE></pre><pre><TABLE><ROW><CELL>1 <CELL rowspan="2">2 <CELL>3 <CELL>4<ROW><CELL colspan="2">5</TABLE></pre><p>The second table is formatted as in the figure on theright. However, the HTML table's rendering is explicitly undefined byHTML, and CSS doesn't try to define it. User agents are free to renderit, e.g., as in the figure on the left.<div class="figure"><p><img src="images/table-overlap.gif" alt="One table with overlappingcells and one without"><SPAN class="dlink"> <A name="img-table-overlap" href="images/longdesc/table-overlap-desc.html" title="Long description for example showing how structurallyoverlapping cells are rendered">[D]</A></SPAN> <p class="caption">On the left, one possiblerendering of an erroneous HTML 4.0 table; on the right, the onlypossible formatting of a similar, non-HTML table.</div></div><h3>17.5.1 <a name="table-layers">Table layers and transparency</a></h3><p>For the purposes of finding the background of each table cell, thedifferent table elements may be thought of as being on sixsuperimposed layers. The background set on an element in one of thelayers will only be visible if the layers above it have a transparentbackground.<div class="figure"><p><img src="images/tbl-layers.gif" alt="schema of table layers"><SPAN class="dlink"> <A name="img-tbl-layers" href="images/longdesc/tbl-layers-desc.html" title="Long description of example of cell background calculation">[D]</A></SPAN> <pclass="caption">Schema of table layers.</div><ol><li><p>The lowest layer is a single plane, representing the table boxitself. Like all boxes, it may be transparent.</p></li><li><p>The next layer contains the column groups. The columns groups areas tall as the table, but they need not cover the whole tablehorizontally.</p></li><li><p>On top of the column groups are the areas representing the columnboxes. Like column groups, columns are as tall as the table, but neednot cover the whole table horizontally.</p></li><li><p>Next is the layer containing the row groups. Each row group is aswide as the table. Together, the row groups completely cover the tablefrom top to bottom.</p></li><li><p>The next to last layer contains the rows. The rows also cover thewhole table.</p></li><li><p>The topmost layer contains the cells themselves. As the figureshows, although all rows contain the same number of cells, not everycell may have specified content. These "empty" cells are transparent,letting lower layers shine through.</p></li></ol><div class="html-example"><P>In the following example, the first row contains four cells, butthe second row contains no cells, and thus the table background shinesthrough, except where a cell from the first row spans into thisrow. The following HTML code and style rules</p><PRE><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><HTML> <HEAD> <STYLE type="text/css"> TABLE { background: #ff0; border-collapse: collapse } TD { background: red; border: double black } </STYLE> </HEAD> <BODY> <P> <TABLE> <TR> <TD> 1 <TD rowspan="2"> 2 <TD> 3 <TD> 4 </TR> <TR><TD></TD></TR> </TABLE> </BODY></HTML></PRE><P>might be formatted as follows:</p><div class="figure"><p><img src="images/tbl-empty.gif" alt="Table with three empty cellsin bottom row"><SPAN class="dlink"> <A name="img-tbl-empty" href="images/longdesc/tbl-empty-desc.html" title="Long description of example illustrating background calculation">[D]</A></SPAN><p class="caption"> Table with three empty cells in thebottom row.</div></div><h3>17.5.2 <a name="width-layout">Table width algorithms:</a>the <a href="tables.html#propdef-table-layout" class="noxref"><span class="propinst-table-layout">'table-layout'</span></a>property</h3><p>CSS does not define an "optimal" layout for tables since, in manycases, what is optimal is a matter of taste. CSS does defineconstraints that user agents must respect when laying out atable. User agents may use any algorithm they wish to do so, and arefree to prefer rendering speed over precision, except when the "fixedlayout algorithm" is selected.<div class="propdef"><dl><dt><span class="index-def" title="'table-layout'"><a name="propdef-table-layout" class="propdef-title"><strong>'table-layout'</strong></a></span><dd><table class="propinfo" cellspacing=0 cellpadding=0><tr valign=baseline><td><em>Value:</em> <td>auto | fixed | <a href="cascade.html#value-def-inherit" class="noxref"><span class="value-inst-inherit">inherit</span></a><tr valign=baseline><td><em>Initial:</em> <td>auto<tr valign=baseline><td><em>Applies to:</em> <td>'table' and 'inline-table' elements<tr valign=baseline><td><em>Inherited:</em> <td>no<tr valign=baseline><td><em>Percentages:</em> <td>N/A<tr valign=baseline><td><em>Media:</em> <td><a href="media.html#visual-media-group" class="noxref">visual</a></table></dl></div><p>The <a href="tables.html#propdef-table-layout" class="noxref"><span class="propinst-table-layout">'table-layout'</span></a>property controls the algorithm used to lay out the table cells, rows,and columns. Values have the following meaning:</p><dl><dt><strong>fixed</strong><dd>Use the fixed table layout algorithm<dt><strong>auto</strong><dd>Use any automatic table layout algorithm</dl><P>The two algorithms are described below.<h4> <a name="fixed-table-layout">Fixed table layout</a></h4><P>With this (fast) algorithm, the horizontal layout of the table doesnot depend on the contents of the cells; it only depends on thetable's width, the width of the columns, and borders or cell spacing.<P>The table's width may be specified explicitly with the <a href="visudet.html#propdef-width" class="noxref"><spanclass="propinst-width">'width'</span></a> property. A value of 'auto' (forboth 'display: table' and 'display: inline-table') means use the <ahref="#auto-table-layout">automatic table layout</a> algorithm.<P>In the fixed table layout algorithm, the width of each column isdetermined as follows:</p><ol><li>A column element with a value other than 'auto' for the<a href="visudet.html#propdef-width" class="noxref"><span class="propinst-width">'width'</span></a> property sets the widthfor that column.<li>Otherwise, a cell in the first row with a value other than 'auto'for the <a href="visudet.html#propdef-width" class="noxref"><span class="propinst-width">'width'</span></a> property sets thewidth for that column. If the cell spans more than one column, thewidth is divided over the columns.<li>Any remaining columns equally divide the remaining horizontaltable space (minus borders or cell spacing).</ol><P>The width of the table is then the greater of thevalue of the <a href="visudet.html#propdef-width" class="noxref"><span class="propinst-width">'width'</span></a> propertyfor the table element and the sum of the column widths (pluscell spacing or borders). If the table is wider than the columns,the extra space should be distributed over the columns.<p>In this manner, the user agent can begin to lay out the table oncethe entire first row has been received. Cells in subsequent rows donot affect column widths. Any cell that has content that overflowsuses the <a href="visufx.html#propdef-overflow" class="noxref"><span class="propinst-overflow">'overflow'</span></a> property todetermine whether to clip the overflow content.<h4> <a name="auto-table-layout">Automatic table layout</a></h4><P>In this algorithm (which generally requires no more than twopasses), the table's width is given by the width of its columns (andintervening <a href="#borders">borders</a>). This algorithm reflectsthe behavior of several popular HTML user agents at the writing ofthis specification. UAs are not required to implement this algorithmto determine the table layout in the case that <a href="tables.html#propdef-table-layout" class="noxref"><spanclass="propinst-table-layout">'table-layout'</span></a> is 'auto'; theycan use any other algorithm.<P>This algorithm may be inefficient since it requires the user agent tohave access to all the content in the table before determining thefinal layout and may demand more than one pass.<p>Column widths are determined as follows:</p><ol><li>Calculate the minimum content width (MCW) of each cell: theformatted content may span any number of lines but may not overflow the cell box. If the specified<a href="visudet.html#propdef-width" class="noxref"><span class="propinst-width">'width'</span></a> (W) of the cellis greater than MCW, W is the minimum cell width. A valueof 'auto' means that MCW is the minimum cell width.<P>Also, calculate the "maximum" cell width of each cell: formattingthen content without breaking lines other than where explicit linebreaks occur.<li>For each column, determine a maximum and minimum column width fromthe cells that span only that column. The minimum is that required bythe cell with the largest minimum cell width (or the column <a href="visudet.html#propdef-width" class="noxref"><spanclass="propinst-width">'width'</span></a>, whichever is larger). Themaximum is that required by the cell with the largest maximum cellwidth (or the column <a href="visudet.html#propdef-width" class="noxref"><span class="propinst-width">'width'</span></a>,whichever is larger).<li>For each cell that spans more than one column, increase theminimum widths of the columns it spans so that together, they are atleast as wide as the cell. Do the same for the maximum widths. Ifpossible, widen all spanned columns by approximately the same amount.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -