CSS Table Column Autowidth - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs CSS table column autowidth Ask Question Asked 13 years, 11 months ago Modified 2 years, 3 months ago Viewed 319k times 138

Given the following how do i make my last column auto size to its content? (The last column should autosize-width to the content. Suppose i have only 1 li element it should shrink vs. having 3 li elements etc):

table { table-layout: fixed; width: 100%; } table tr { border-bottom: 1px solid #e9e9e9; } table thead td, th { border-left: 1px solid #f2f2f2; border-right: 1px solid #d5d5d5; background: #ddd url("../images/sprites4.png") repeat-x scroll 0 100%; font-weight: bold; text-align: left; } table tr td, th { border: 1px solid #D5D5D5; padding: 15px; } table tr:hover { background: #fcfcfc; } table tr ul.actions { margin: 0; } table tr ul.actions li { display: inline; margin-right: 5px; } <table cellspacing="0" cellpadding="0" border="0"> <thead> <!-- universal table heading --> <tr> <td class="tc first"><input type="checkbox" value="true" name="data-1-check-all" id="data-1-check-all"></td> <td class="tc">Type</td> <th>File</th> <th>Sample</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>Division 1</td> <td>Division 2</td> <td>Division 3</td> <td>Division 4</td> <td>Division 5</td> </tr> <tr> <td>Division 1</td> <td>Division 2</td> <td>Division 3</td> <td>Division 4</td> <td class="last"> <ul class="actions"> <li><a class="iconStats" href="#">Statystyki</a></li> <li><a class="iconEdit" href="#">Edytuj</a></li> <li><a class="iconDelete" href="#">Usuń</a></li> </ul> </td> </tr> </tbody> </table>

Share Improve this question Follow edited Oct 3, 2022 at 23:04 Nickofthyme's user avatar Nickofthyme 4,26828 silver badges46 bronze badges asked Jan 21, 2011 at 10:44 ShaneKm's user avatar ShaneKmShaneKm 21.3k46 gold badges174 silver badges307 bronze badges Add a comment |

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 281

The following will solve your problem:

td.last { width: 1px; white-space: nowrap; }

Flexible, Class-Based Solution

And a more flexible solution is creating a .fitwidth class and applying that to any columns you want to ensure their contents are fit on one line:

td.fitwidth { width: 1px; white-space: nowrap; }

And then in your HTML:

<tr> <td class="fitwidth">ID</td> <td>Description</td> <td class="fitwidth">Status</td> <td>Notes</td> </tr> Share Improve this answer Follow edited Apr 3, 2019 at 23:41 Joshua Pinter's user avatar Joshua Pinter 47.4k23 gold badges256 silver badges252 bronze badges answered Jun 1, 2012 at 15:36 Doug Amos's user avatar Doug AmosDoug Amos 4,3631 gold badge23 silver badges24 bronze badges 11
  • 3 Works perfectly! (my case having table width 100% and no other columns have widths. Applied this to one column). Tested in IE7/8/9, Firefox 12 and Chrome 19. – marcovtwout Commented Jun 20, 2012 at 14:12
  • wow, never thought this is possible with html tables. Thank you! – kulpae Commented Nov 12, 2012 at 22:04
  • 14 Instead of manually adding the '.last' class, you can just do 'td:last-child' as the selector. – T.Ho Commented Feb 13, 2013 at 0:34
  • 5 This solution works well without table-layout: fixed that is set in the css in the question jsfiddle.net/hCkch/1 – David Sherret Commented Jul 8, 2013 at 19:34
  • 1 Effing brilliant, mate! Really clever trick to "prioritize" certain columns to make sure they don't squished onto multiple lines by large "text" columns, like "Description" or "Notes". Nice one! To make it even more useful, I would call the class .fitwidth and then just set the class on the columns that you don't want going to multiple lines. I'll edit your answer to add that. – Joshua Pinter Commented Apr 3, 2019 at 23:36
| Show 6 more comments 29

If you want to make sure that last row does not wrap and thus size the way you want it, have a look at

td { white-space: nowrap; } Share Improve this answer Follow answered Jan 21, 2011 at 10:46 Sander's user avatar SanderSander 1,2741 gold badge13 silver badges26 bronze badges 0 Add a comment | 3

You could specify the width of all but the last table cells and add a table-layout:fixed and a width to the table.

You could set

table tr ul.actions {margin: 0; white-space:nowrap;}

(or set this for the last TD as Sander suggested instead).

This forces the inline-LIs not to break. Unfortunately this does not lead to a new width calculation in the containing UL (and this parent TD), and therefore does not autosize the last TD.

This means: if an inline element has no given width, a TD's width is always computed automatically first (if not specified). Then its inline content with this calculated width gets rendered and the white-space-property is applied, stretching its content beyond the calculated boundaries.

So I guess it's not possible without having an element within the last TD with a specific width.

Share Improve this answer Follow edited Jan 24, 2011 at 13:37 answered Jan 21, 2011 at 10:46 acme's user avatar acmeacme 14.8k7 gold badges76 silver badges114 bronze badges 2
  • no good. that's exacly what i'm trying to awoid. i don't want o specify col widths. columns should auto size and the last col should be sized down to content inside it. – ShaneKm Commented Jan 21, 2011 at 11:01
  • Can you provide the CSS-definitions for the classes you used? – acme Commented Jan 21, 2011 at 11:09
Add a comment | -3

use auto and min or max width like this:

td { max-width:50px; width:auto; min-width:10px; } Share Improve this answer Follow answered Jan 24, 2011 at 13:42 community wiki Abudayah 2
  • 1 This does not work as required, when I have smaller content it still uses max-width. – marcovtwout Commented Jun 20, 2012 at 14:14
  • min-width doe not apply to table cell. – Nick Commented Sep 11, 2013 at 16:25
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • How AI apps are like Google Search
  • WBIT #2: Memories of persistence and the state of state
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
Visit chat

Linked

434 Fit cell width to content 26 Adjust table column width to content size 4 How to make a column auto width in fixed layout table in 2018 3 CSS table column autowidth in HTML Outlook e-mail 1 Get table cells to collapse horizontally 0 How can I make the width of a <td> to auto? 1 possible to get table columns to auto width to fill screen? 11 Auto width on tables 0 Setting column width in HTML table 9 Fixed column width in HTML table 0 Intelligent HTML Table Column Widths 1 Table columns to be fixed width no matter what content? 1 setting HTML table column width 2 HTML table, fixed width autofit columns 1 Create a Table with auto width <td> 4 html table column width

Hot Network Questions

  • Hardy's ratings of mathematicians
  • What type of valve has a screwdriver slot and no handle?
  • Measuring Hubble expansion in the lab
  • How does FM preemphasis interact with maximum deviation?
  • Manathermy: effects on the ecosystem
  • Is this position possible to have been made legally?
  • A world with limited collective ability
  • 2 network cards on 1 ubuntu server netplan config
  • What do people mean when they say "Everything is connected"?
  • Why does Bereishis Rabbah ask why Vaychi has no break?
  • When re-implementing software, does analyzing the original software's kernel-calls make the re-implementation a derived work?
  • Pancakes: Avoiding the "spider batch"
  • When does a noun take the accusative rather than dative form
  • Escape braces in C# interpolated raw string literal
  • Helping daughter with taxes - How do I report some freelance work for her?
  • How can I put logo on left of the page next to institute name?
  • Colombian passport expires in 5 months
  • Air launch separation mechanism
  • exploratory factor analysis, non-normal data
  • Simple U-Arrow above Arrow in tikz
  • Is this 240V compressor plug wired correctly?
  • Does light travel in a straight line? If so, does this contradict the fact that light is a wave?
  • Publication in a journal that has now disappeared entirely. Can I publish the paper elsewhere?
  • Why does the engine prefer a5 (pass pawn) over axb5 (pass pawn+win a pawn)?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-html

Từ khóa » Html Td Column Width Auto