How Is Column Width Determined in Browser

How is column width determined in browser?

From http://www.w3.org/TR/CSS21/tables.html

This was not overriden by CSS3 yet, but this part of spec is non-normative, so probably browsers does not comply to it exactly (there is no normative definition of this behavior afaik).

Column widths are determined as follows:

  1. Calculate the minimum content width (MCW) of each cell: the formatted
    content may span any number of lines but may not overflow the cell
    box. If the specified 'width' (W) of the cell is greater than MCW, W
    is the minimum cell width. A value of 'auto' means that MCW is the
    minimum cell width.
    Also, calculate the "maximum" cell width of each cell: formatting the
    content without breaking lines other than where explicit line breaks
    occur.
  2. For each column, determine a maximum and minimum column width from the
    cells that span only that column. The minimum is that required by the
    cell with the largest minimum cell width (or the column 'width',
    whichever is larger). The maximum is that required by the cell with
    the largest maximum cell width (or the column 'width', whichever is
    larger).
  3. For each cell that spans more than one column, increase the minimum
    widths of the columns it spans so that together, they are at least as
    wide as the cell. Do the same for the maximum widths. If possible,
    widen all spanned columns by approximately the same amount.
  4. For each column group element with a 'width' other than 'auto',
    increase the minimum widths of the columns it spans, so that together
    they are at least as wide as the column group's 'width'.

This gives a maximum and minimum width for each column.

The caption width minimum (CAPMIN) is determined by calculating for
each caption the minimum caption outer width as the MCW of a
hypothetical table cell that contains the caption formatted as
"display: block". The greatest of the minimum caption outer widths is
CAPMIN.

Column and caption widths influence the final table width as follows:

  1. If the 'table' or 'inline-table' element's 'width' property has a
    computed value (W) other than 'auto', the used width is the greater of
    W, CAPMIN, and the minimum width required by all the columns plus cell
    spacing or borders (MIN). If the used width is greater than MIN, the
    extra width should be distributed over the columns.
  2. If the 'table' or
    'inline-table' element has 'width: auto', the used width is the
    greater of the table's containing block width, CAPMIN, and MIN.
    However, if either CAPMIN or the maximum width required by the columns
    plus cell spacing or borders (MAX) is less than that of the containing
    block, use max(MAX, CAPMIN).

A percentage value for a column width is relative to the table width.
If the table has 'width: auto', a percentage represents a constraint
on the column's width, which a UA should try to satisfy. (Obviously,
this is not always possible: if the column's width is '110%', the
constraint cannot be satisfied.)

Computed column width is different than css declared width for column. How does browser decide width?

When you set the width of table columns, it's just a recommended minimum width, that the browser tries to honor if possible. The actual width of the columns are calculated from the size of their content, dividing the leftover space evenly between the columns.

If any of the columns would end up narrower than the width that you specified, the browser would try to adjust it if there is free space in other columns, but in your case the columns are already over the minimum.

The actual algorithm to calculate the cell sizes differs from browser to browser, which means that the columns will end up slightly different depending on what browser you use.

How are table cell widths calculated in html?

This question was answered here : How is column width determined in browser?

Now, to help you solve this issue, consider the way you are using colgroups.

I cleaned it up a little for you in your fiddle. But generally, I'd use the colgroups this way instead

  <colgroup width="50%" />
<colgroup width="50%" />

See DEMO

Setting table column width

<table style="width: 100%">    <colgroup>       <col span="1" style="width: 15%;">       <col span="1" style="width: 70%;">       <col span="1" style="width: 15%;">    </colgroup>                <!-- Put <thead>, <tbody>, and <tr>'s here! -->    <tbody>        <tr>            <td style="background-color: #777">15%</td>            <td style="background-color: #aaa">70%</td>            <td style="background-color: #777">15%</td>        </tr>    </tbody></table>

How are `display: table-cell` widths calculated?

This is the result of the automatic table layout algorithm as implemented by the browser. This can vary across browsers because the CSS2.1 spec doesn't specify all auto layout behavior, but it does outline an algorithm commonly used by browsers with HTML tables, because the CSS table model is based on the HTML table model for the most part.

In general, if the table doesn't have a specified width (i.e. it uses the default auto), then the cell with the percentage width is as wide as required by its contents, and no more. That calculated width (together with any other widths specified on other cells) is then used as the percentage to find the maximum width of the entire table, and the rest of the columns are resized accordingly. Note that the table can still be constrained by its containing block (in your example, it's the initial containing block established by the result pane).

On my PC running Firefox, .cell-1 has a computed width of 33 pixels. When you specify its width as 1%, the maximum width that the table will have is 3300 pixels (33 × 100 = 3300). You would need a very large screen to see this in your example, so I removed the padding which drastically reduces the width of .cell-1 to 9 pixels and thus the maximum table width to 900 pixels. If you resize the preview pane to greater than 900 pixels, you will see that the table stops growing at that point.

When you increase the width of .cell-1 to 10%, the same 33 pixels now becomes 10% of the maximum width of the table. In turn, the maximum table width becomes 330 pixels (which is in fact 10% of 3300 pixels, because 100 ÷ 10 = 10).

The moment you specify the width of .table as 100%, the table is sized according to its containing block. In your case, that just means stretching it to the full width of the result pane. .cell-1 still has to obey its width: 10% declaration, so it stretches to 10% of the width of the table since the content doesn't require anything wider. If you resize the result pane to a very narrow width, you'll see that .cell-1 shrinks along with the table, but not past its minimum content width.



Related Topics



Leave a reply



Submit