I have an HTML table that has rows added in a PHP loop. This is what the loop body looks like
<tr>
<td style="width:220px"><?php echo $a; ?></td>
<td style="width:150px"><?php echo $b; ?></td>
<td style="width:70px"><?php echo $c; ?></td>
<td style="width:70px"><?php echo $d; ?></td>
</tr>
The problem is, when the contents in the second column of any row are slightly large, the width of the second column exceeds 150px, and to compensate the width of the first column reduces. How can I prevent that from happening. I want to widths to not change, and if the contents in any particular cell are too large to fit, the height should increase to accomodate.
The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.
To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.
To set the cell width and height, use the CSS style. The height and width attribute of the <td> cell isn't supported in HTML5. Use the CSS property width and height to set the width and height of the cell respectively.
you should try this CSS instruction:
td { word-wrap: break-word; }
that works in many browsers (yes, including IE 6, even IE 5.5 but not Fx 3.0. It's only recognized by Fx3.5+. Also good for Saf, Chr and Op but I don't know the exact version for these ones) and don't do any harm in the other ones.
If table's width is still messed up, there is also:
table { table-layout: fixed; }
th, td { width: some_value; }
that will force the browser to use the other table algorithm, the one where it doesn't try to adapt many situations including awkward ones but stick to what the stylesheet says.
<tr>
<td style="word-wrap:break-word;width:200px;"><?php echo $a; ?></td>
<td style="word-wrap:break-word;width:150px"><?php echo $b; ?></td>
<td style="word-wrap:break-word;width:70px"><?php echo $c; ?></td>
<td style="word-wrap:break-word;width:70px"><?php echo $d; ?></td>
</tr>
You can try word-wrap:break-word;
About the Property
This property specifies whether the current rendered line should break if the content exceeds the boundary of the specified rendering box for an element (this is similar in some ways to the ‘clip’ and ‘overflow’ properties in intent.) This property should only apply if the element has a visual rendering, is an inline element with explicit height/width, is absolutely positioned and/or is a block element.
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