When I create a table in html, a table with a width of 100%, if I want all the cells (tds) to be divided in equal parts, do I have to enter the width % for each cell? Am I "obliged" to do it?
E.g.:
<table cellpadding="0" cellspacing="0" width="100%" border="0"> <tr> <td width="25%"></td> <td width="25%"></td> <td width="25%"></td> <td width="25%"></td> </tr> </table>
OR the following could also be the right procedure, not to write the width in each tds if I want each of them to be devided equally:
<table cellpadding="0" cellspacing="0" width="100%" border="0"> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table>
I know it works with both manners but I just want to know the "legit" way to do it.
Using table-layout: fixed as a property for table and width: calc(100%/3); for td (assuming there are 3 td 's). With these two properties set, the table cells will be equal in size.
Note: Using a percentage as the size unit for a width means how wide will this element be compared to its parent element, which in this case is the <body> element.
To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.
Use the property table-layout:fixed;
on the table to get equally spaced cells. If a column has a width set, then no matter what the content is, it will be the specified width. Columns without a width set will divide whatever room is left over among themselves.
<table style='table-layout:fixed;'> <tbody> <tr> <td>gobble de gook</td> <td>mibs</td> </tr> </tbody> </table>
Just to throw it out there, you could also use <colgroup><col span='#' style='width:#%;'/></colgroup>
, which doesn't require repetition of style per table data or giving the table an id to use in a style sheet. I think setting the widths on the first row is enough though.
You need to enter the width % for each cell. But wait, there's a better way to do that, it's called CSS:
<style> .equalDivide tr td { width:25%; } </style> <table class="equalDivide" cellpadding="0" cellspacing="0" width="100%" border="0"> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With