A detailed introduction to the calculation rules for table cell width in HTML

黄舟
Release: 2017-05-25 10:51:40
Original
2584 people have browsed it


About the rendering rules of tablewidth

The calculation method of table cell width is mainly divided into two methods: Fixed table layout, automatic table layout, People who often write CSS should still know this, but we often find that setting the width of the table does not work, or it is rendered without a fixed width. Is this normal? Let’s introduce how these two methods calculate rendering.

First set a few common variables:

  • tableWidth=Table width=100%

  • tableBorderWidth=The left and right border width of the table

  • tdBorderWidth=The sum of the left and right border widths of all columns (the combined border is counted as 1px)

  • tdPadding=The left and right padding sum of all columns

  • tdWidth=The width sum of all columns with width defined

  • tdLength=number of columns

1. Fixed table layout, table additiontable-layout:fixed

ps: In a fixed table layout, the width of a table column has nothing to do with the column content. It is only related to the table width, column width, table left and right borders, column left and right borders, and column left and right padding

By using a fixed table layout, the user agent can display the table after receiving the first row, i.e. only the width of the first row will work

width is the width of the column with auto (i.e. where width is not defined The width of the column, if the calculation result is negative, it is 0) = (tableWidth-tableBorderWidth-tdBorderWidth-tdPadding-tdWidth)/tdLength

1. All th widths are undefined

The width of each column Evenly distributed by table width

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row 7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10


2. All th have a defined width, and the sum of the widths of all columns is less than Table width (tableBorderWidth+tdBorderWidth+tdPadding+tdWidth <= tableWidth)

The width of each column is evenly distributed through the total width; the width of the table is its defined width

#row1row1row1row1row1row1row1row1row2row2row2row2row2row2row2row2 row3row4row4row4row4row4row4row4row4row5row5row5row5row5row5row5row5row6row7row7row7row7row7row7row7row7row8row8row8row8row8row8row8row8row9row9row9row9row9row9row9row9row10row10row10row10row10row10row10


3. All th have a defined width, and the sum of all column widths is greater than the table width (tableBorderWidth+tdBorderWidth+tdPadding+tdWidth > tableWidth)

The width of each column is self-defined Width; the width of the table is the sum of the widths of all columns (which will exceed the width defined by the table)


th1th2th3th4th5th6th7 th8th9th10
th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

4. The width of part th is set, and the width of the column with th width set at the same time is smaller than the table width (tableBorderWidth+tdBorderWidth+tdPadding+tdWidth <= tableWidth)

ps: Column with dark gray background For columns with a defined width

The width of a column with a defined width is its own defined width, and the width of other columns without a defined width is the total width of the table minus the sum of the defined widths and then evenly distributed


th1th2th3th4th5th6th7th8th9th10
row1row1row1row1row1row1row1row1row2row2row2row2row2row2row2row2row3row4row4row4row4row4row4row4row4row5row5row5row5row5row5row5row5row6row7row7row7row7row7row7row7row 7row8row8row8row8row8row8row8row8row9row9row9row9row9row9row9row9row10row10row10row10row10row10row10

5. Part of the th width is set, and the width of the column with the th width is set to be greater than Table width (tableBorderWidth+tdBorderWidth+tdPadding+tdWidth > tableWidth)

ps: The columns with dark gray background are columns with defined widths

The actual width of the columns with defined widths is self-defined Width, the width of other columns with no defined width is the total width of the table minus the sum of the defined widths and then evenly distributed. The width after even distribution is less than zero, then Other The width of other columns with no defined width is 0


##th1th2th3th4th5th6th7#th8th9th10row1row1row1row1row1row1row1row1row2row2row2row2row2row2row2row2row3row4row4row4row4row4row4row4row4row5row5row5row5row5row5row5row5row6row7row7row7row7row7row 7row7row7row8row8row8row8row8row8row8row8row9row9row9row9row9row9row9row9row10row10row10row10row10row10row10

2. Automatic table layout, table setting table-layout:auto (the propertydefault value is auto)

The width of each column is determined by the width of the cell without folding. This algorithm is sometimes very slow because it needs to access all columns in the table before determining the final layout

1. The minimum width of all th is not determined

The width of each column is completely determined by the content inside.


##th1th2th3th4th5th6th7#th8th9th10row1row1row1row1row1row1row1row1row2row2row2row2row2row2row2row2row3row4row4row4row4row4row4row4row4row5row5row5row5row5row5row5row5row6row7row7row7row7row7row 7row7row7row8row8row8row8row8row8row8row8row9row9row9row9row9row9row9row9row10row10row10row10row10row10row10
#2. All th have a minimum width defined, and all th are calculated based on the content. The sum of the columns is less than the table width

The width of each column is first calculated based on the content, and cannot be less than the defined minimum width. The excess width is evenly distributed on each column.


##th1row1##3. All th have a minimum width defined, and all ths are calculated based on the content. The sum of columns is greater than the table width
th2 th3 th4 th5 th6 th7 #th8 th9 th10
row2row2row2row2row2row2row2row2 row3 row4 row5 row6 row7 row8 row9 row10
The width of each column is first calculated based on the content, and secondly it cannot be less than the defined minimum width


# #th1th2##row1row2row2row2row2row2row2row2row2row3row4row4row4row4row4row4row4row4row5row6row6row6row6row6row6row6row6row7row6row6row6row6row6row6row6row6row9row10row10row10row10row10row10row10ps: The columns with dark gray background define the minimum width The width of each column
th3 th4 th5 th6 th7 th8 th9 th10
4. Part th defines the minimum width, and the sum of all columns calculated based on the content is less than the table width
is first calculated based on the content, secondly it cannot be less than the defined minimum width, and finally the width rendered by the table cannot be less than the width defined by the table itself.

##th1

th2th3row2row3
th4 th5 th6 th7 #th8 th9 th10 row1
row4row4row4row4row4row4row4row4 row5 row6 row7 row6 row9 row10

5. Part th defines the minimum width, and the sum of all columns calculated based on the content is less than the table width

ps: The columns with dark gray background define the minimum width of the columns

Every The column width is first calculated based on the content, and secondly it cannot be less than the defined minimum width


##th1th2th3th4th5th6th7th8th9th10##row1
row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5 row6row6row6row6row6row6row6row6 row7 row6row6row6row6row6row6row6 row9 row10row10row10row10row10row10row10

The above is the detailed content of A detailed introduction to the calculation rules for table cell width in HTML. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!