Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting width of table cell contents based on available width

Tags:

html

css

I have an HTML table whose cells contain, among other things, spans, like this:

...
<td>
    <span style="height: 20px; width: 20px; margin-left: 2px;">
    <span style="height: 20px; width: 20px; margin-left: 2px;">
    <span style="height: 20px; width: 20px; margin-left: 2px;">
</td>
...

I'm looking for a way to shrink the width of those spans, rather than line wrap them, when the containing table cell is too narrow to show them all on one line. I tried playing around with setting the spans' max-width to 20px and then using a percent for the width, but that does not work because the table cell tries to be only as wide as its contents.

The minimum table cell width would be the width needed to display the header on 1 line.

For the visual types, here's what I currently have when there is enough width:

enter image description here

Here's what I currently have when there is not enough width:

enter image description here

And here's what I would like it to look like when there is not enough width for each span to be a full 20px:

enter image description here

In case it's not obvious, the spans are the colored squares in the TXEs, RDBs, and RavenNets columns.

like image 412
Matt Ball Avatar asked Feb 17 '10 19:02

Matt Ball


People also ask

How do you automatically resize the columns in a table to fit the contents of cells?

To make the columns in a table automatically fit the contents, click on your table. On the Layout tab, in the Cell Size group, click AutoFit, and then click AutoFit Contents. To use the ruler, select a cell in the table, and then drag the markers on the ruler.

How do you set cell width in a table?

If you want to set the width of the table column, you can use some CSS. You need to use the width property. In the example below, we set the width of the the <table> element to 100%. Then, in the HTML part, we distribute the table width of 100% among <col> elements having span attributes.

How do I resize individual cell size in a Word table separately from the other cells?

You can also resize one or more rows, columns, or individual cells in a table. Click the table. appears, and then drag the table boundary until the table is the size that you want.

How do you fix a cell width in a table?

There are multiple ways to fix the width for <td> tag. Some of them are mentioned below: Using width attribute: The <td> tag has width attribute to control the width of a particular column. By assigning a numeric value to this attribute between 0 to 100 in terms of percentage(or you can use pixel format).


1 Answers

Use <td nowrap> or <td style="white-space:nowrap;"> to avoid the wrapping. A table cell should generally expand to fit its contents, unless it is allowed to wrap, or you have constrained its width in some other way.

like image 153
Ray Avatar answered Oct 14 '22 16:10

Ray