446

Given the following markup, how could I use CSS to force one cell (all cells in column) to fit to the width of the content within it rather than stretch (which is the default behaviour)?

td.block { border: 1px solid black; }
<table style="width: 100%;"> <tr> <td class="block">this should stretch</td> <td class="block">this should stretch</td> <td class="block">this should be the content width</td> </tr> </table>

I realize I could hard code the width, but I'd rather not do that, as the content which will go in that column is dynamic.

Looking at the image below, the first image is what the markup produces. The second image is what I want.

enter image description here

3
  • Possible duplicate of CSS table column autowidth Commented Jun 24, 2017 at 12:00
  • <table style="width: auto;"> <tr> <td class="block">this should stretch</td> <td class="block">this should stretch</td> <td class="block">this should be the content width</td> </tr> </table> Commented Oct 10 at 6:40
  • table{table-layout: auto; width: 100%;} td.block {border: 1px solid black;} <table> <tr> <td class="block">this should stretch</td> <td class="block">this should stretch</td> <td class="block">this should be the content width</td> </tr> </table> Commented Oct 10 at 6:55

9 Answers 9

698

I'm not sure if I understand your question, but I'll take a stab at it:

td { border: 1px solid #000; } tr td:last-child { width: 1%; white-space: nowrap; }
<table style="width: 100%;"> <tr> <td class="block">this should stretch</td> <td class="block">this should stretch</td> <td class="block">this should be the content width</td> </tr> </table>

Sign up to request clarification or add additional context in comments.

13 Comments

why you write <table style="width:100%"> instead of <table width="100%" >
@diEcho I didn't write that part, that was from the example code. Regardless, it is bad practice to use the width attribute on elements. In this quick example, the inline CSS is fine, but it should be abstracted into a file if this was production-level code.
A cleaner way to do this IMO would be to define a style called "nostretch" (or something like that), and then just define nostretch in the CSS to have width:1% and the nowrap. Then the last TD would have 'class="nostretch block"'. That way you can "nostretch" any cell you want.
This is a good solution, but sadly, in Bootstrap 3 my button groups will wrap from this: jsfiddle.net/wexdX/326 Any ideas how I can suppress it?
This only works when the content is wider then width:1% - right? Because if the content is smaller then width:1%, the cell will be larger.
|
77

Setting

max-width:100%; white-space:nowrap; 

will solve your problem.

4 Comments

What element should this be applied to?
Try with <td>, <div>
This answer would be better with more context. Also, don't beg for points in comments.
@SlavaFominII I just applied these settings inside the style attribute of the table element. So style="the_above_settings".
23

For me, this is the best autofit and autoresize for table and its columns (use css !important ... only if you can't without)

.myclass table { table-layout: auto !important; } .myclass th, .myclass td, .myclass thead th, .myclass tbody td, .myclass tfoot td, .myclass tfoot th { width: auto !important; } 

Don't specify css width for table or for table columns. If table content is larger it will go over screen size to.

Comments

19

First, setting

td { max-width: 0; overflow: hidden; text-overflow: ellipsis; } 

ensures that your your table cells will stretch.

Now you only need

td.fit { width: 0; min-width: fit-content; } 

on your fitting cells.

Note that now the table will only overflow in width if content of all fitting cells is too much to fit into a table-width of 100%.

table { white-space: nowrap; width: 100%; } td { border: 1px solid black; } td { max-width: 0; overflow: hidden; text-overflow: ellipsis; } td.fit { width: 0; min-width: fit-content; }
<table> <tr> <td class="stretch">this should stretch</td> <td class="stretch">this should stretch</td> <td class="fit">this should be the content width</td> </tr> </table>

1 Comment

Not working? I can barely see the third cell
14

There are many ways to do this!

correct me if I'm wrong but the question is looking for this kind of result.

table, td { border: 1px solid black; }
<table style="white-space:nowrap;width:100%;"> <tr> <td class="block" style="width:50%">this should stretch</td> <td class="block" style="width:50%">this should stretch</td> <td class="block" style="width:auto">this should be the content width</td> </tr> </table>

The first 2 fields will "share" the remaining page (NOTE: if you add more text to either 50% fields it will take more space), and the last field will dominate the table constantly.

If you are happy to let text wrap you can move white-space:nowrap; to the style of the 3rd field
will be the only way to start a new line in that field.

alternatively, you can set a length on the last field ie. width:150px, and leave percentage's on the first 2 fields.

Hope this helps!

Comments

4

Setting CSS width to 1% or 100% of an element according to all specs I could find out is related to the parent. Although Blink Rendering Engine (Chrome) and Gecko (Firefox) at the moment of writing seems to handle that 1% or 100% (make a columns shrink or a column to fill available space) well, it is not guaranteed according to all CSS specifications I could find to render it properly.

One option is to replace table with CSS4 flex divs:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

That works in new browsers i.e. IE11+ see table at the bottom of the article.

Comments

2

For some scenarios, this may work for you. One that I came across was when I need all the td tags to fit the content. Leaving rest of the horizontal space empty. Here's what I did:

table { width: 100%; background-color: Cornflowerblue; } td { border: 1px solid lightyellow; white-space: nowrap; } td:last-child { width: 100%; /*hiding border*/ border: none; }
<table> <tr> <td> Content 1 </td> <td> Content 2 </td> <td> </td> </tr> </table>

Comments

-1

I had a similar problem, what I did was I placed a div inside the td and gave it a style.

width: fit-content; 

Seemed to work for my usecase.

2 Comments

table{table-layout: auto; width: 100%;}
table{table-layout: auto; width: 100%;} td.block {border: 1px solid black;} <table> <tr> <td class="block">this should stretch</td> <td class="block">this should stretch</td> <td class="block">this should be the content width</td> </tr> </table>
-13

Simple :

 <div style='overflow-x:scroll;overflow-y:scroll;width:100%;height:100%'> 

1 Comment

Don't see how this answer is relevant at all to this 8 year old question. I'ts a <div>, the question was about tables. And your answer doesn't make the column cells width shrink to its content.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.