


A detailed introduction to the calculation rules for table cell width in HTML
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)
th1 | th2 | th3 | th4 | th5 | th6 | th7 | th8 | th9 | th10 |
---|---|---|---|---|---|---|---|---|---|
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
th1 | th2 | th3 | th4 | th5 | th6 | th7 | th8 | th9 | th10 |
---|---|---|---|---|---|---|---|---|---|
row1row1row1row1row1row1row1row1 | row2row2row2row2row2row2row2row2 | row3 | row4row4row4row4row4row4row4row4 | row5row5row5row5row5row5row5row5 | row6 | row7row7row7row7row7row7row7row 7 | row8row8row8row8row8row8row8row8 | row9row9row9row9row9row9row9row9 | row10row10row10row10row10row10row10 |
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
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.
th2 | th3 | th4 | th5 | th6 | th7 | #th8 | th9 | th10 | |
---|---|---|---|---|---|---|---|---|---|
row2row2row2row2row2row2row2row2 | row3 | row4 | row5 | row6 | row7 | row8 | row9 | row10 |
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 |
##th1
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 widthps: 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
|
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
