search
HomeWeb Front-endHTML TutorialA 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

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

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


##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!

Statement
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
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment