Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Table width in percentage, table rows separated equally

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.

like image 206
user977191 Avatar asked Oct 09 '11 00:10

user977191


People also ask

How do I make table cells the same width in HTML?

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.

Can I use percentage values for td width?

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.

How fix HTML table size?

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.


2 Answers

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.

like image 91
Chinoto Vokro Avatar answered Oct 22 '22 00:10

Chinoto Vokro


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> 
like image 30
vantrung -cuncon Avatar answered Oct 22 '22 00:10

vantrung -cuncon