Fit cell width to content

HtmlCssHtml Table

Html Problem Overview


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)?

<style type="text/css">
td.block
{
border: 1px solid black;
}
</style>
<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>

EDIT: 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

Html Solutions


Solution 1 - Html

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>

Solution 2 - Html

Setting

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

will solve your problem.

Solution 3 - Html

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.

Solution 4 - Html

There are many ways to do this!

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

<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!

Solution 5 - Html

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.

Solution 6 - Html

Simple :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMansfieldView Question on Stackoverflow
Solution 1 - HtmlMetalFrogView Answer on Stackoverflow
Solution 2 - HtmlShubham NigamView Answer on Stackoverflow
Solution 3 - HtmlCristian FlorescuView Answer on Stackoverflow
Solution 4 - HtmlChristian NewmanView Answer on Stackoverflow
Solution 5 - HtmlMladen AdamovicView Answer on Stackoverflow
Solution 6 - HtmlParth JaniView Answer on Stackoverflow