Fixed Table Cell Width - Stack Overflow

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    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 Fixed Table Cell Width Ask Question Asked 14 years ago Modified 3 years, 3 months ago Viewed 754k times 200

A lot of people still use tables to layout controls, data etc. - one example of this is the popular jqGrid. However, there is some magic happening that I cant seem to fathom (its tables for crying out loud, how much magic could there possibly be?)

How is it possible to set a table's column width and have it obeyed like jqGrid does!? If I try to replicate this, even if I set every <td style='width: 20px'>, as soon as the content of one of those cells is greater than 20px, the cell expands!

Any ideas or insights?

Share Improve this question Follow edited Dec 10, 2013 at 18:21 C R's user avatar C R 2,2425 gold badges33 silver badges41 bronze badges asked Nov 15, 2010 at 15:21 Jimbo's user avatar JimboJimbo 22.9k43 gold badges125 silver badges162 bronze badges 0 Add a comment |

7 Answers 7

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

You could try using the <col> tag manage table styling for all rows but you will need to set the table-layout:fixed style on the <table> or the tables css class and set the overflow style for the cells

http://www.w3schools.com/TAGS/tag_col.asp

<table class="fixed"> <col width="20px" /> <col width="30px" /> <col width="40px" /> <tr> <td>text</td> <td>text</td> <td>text</td> </tr> </table>

and this be your CSS

table.fixed { table-layout:fixed; } table.fixed td { overflow: hidden; } Share Improve this answer Follow edited Sep 29, 2020 at 17:36 answered Nov 15, 2010 at 15:27 hunter's user avatar hunterhunter 63.4k19 gold badges114 silver badges115 bronze badges 7
  • 18 col wouldn't work in html5, set width of individual td instead by inline css or css classes – Pankaj Phartiyal Commented May 22, 2013 at 17:58
  • 12 The accepted answer does not work unless you explicitly apply a width for the desired table. Works perfectly whether you use a unit or a percentage. Figured I'd point this out since @totymedli's answer didn't get any votes and I didn't want users to ignore it. However, the word-break suggestion isn't necessary unless that's what you're aiming for. – thebdawk05 Commented Jul 29, 2013 at 18:12
  • What happens when you have rows with only 1 TD element that spans multiple columns? Using your sample code, if you had <TD colspan="3"> in a row by itself would it be 90px? – sab669 Commented Jun 17, 2015 at 13:43
  • Thanks it helped. For data overlapping, add this table.fixed td { word-wrap: break-word; }. – Parag Tyagi Commented Jan 5, 2016 at 10:52
  • The word-break suggestion isn't necessary, but it is useful for the rest of SO users and quite pertinent to the question, as overflowing content in fixed width tables is almost certain to ocurr in most scenarios. – Amy Pellegrini Commented Feb 24, 2017 at 16:41
| Show 2 more comments 114

Now in HTML5/CSS3 we have better solution for the problem. In my opinion this purely CSS solution is recommended:

table.fixed {table-layout:fixed; width:90px;}/*Setting the table width is important!*/ table.fixed td {overflow:hidden;}/*Hide text outside the cell.*/ table.fixed td:nth-of-type(1) {width:20px;}/*Setting the width of column 1.*/ table.fixed td:nth-of-type(2) {width:30px;}/*Setting the width of column 2.*/ table.fixed td:nth-of-type(3) {width:40px;}/*Setting the width of column 3.*/ <table class="fixed"> <tr> <td>Veryverylongtext</td> <td>Actuallythistextismuchlongeeeeeer</td> <td>We should use spaces tooooooooooooo</td> </tr> </table>

You need to set the table's width even in haunter's solution. Otherwise it doesn't work. Also a new CSS3 feature that vsync suggested is: word-break:break-all;. This will break the words without spaces in them to multiple lines too. Just modify the code like this:

table.fixed { table-layout:fixed; width:90px; word-break:break-all;}

Final result

Rendered table

Share Improve this answer Follow edited May 23, 2017 at 11:33 Community's user avatar CommunityBot 11 silver badge answered Jun 28, 2013 at 6:42 totymedli's user avatar totymedlitotymedli 30.9k22 gold badges138 silver badges169 bronze badges 3
  • If you throw a row in with a colspan, this seems to break this solution. Any suggestions on how to utilize this solution with a table that contains colspan > 1? – Eric K Commented Jan 16, 2014 at 16:31
  • @QuantumDynamix I fixed this by including as the first row, a row with no colspans, just all individual columns with widths. I then hid that row with 0 height, border:none, visibility:hidden, padding:0px etc., then the second row is the first one I want seen where there are colspans. The first row with individual fixed width cells establishes the widths. – AaronLS Commented Mar 18, 2014 at 16:42
  • @totymedli Any ideas how to get my table to take on the sum of all my column widths? My table is generated dynamically a bit and I so can't calculate in advance the table width, but I don't want to use 100% because that stretches some elements. – AaronLS Commented Mar 18, 2014 at 16:43
Add a comment | 20 table td { table-layout:fixed; width:20px; overflow:hidden; word-wrap:break-word; } Share Improve this answer Follow edited May 9, 2013 at 15:50 Bakudan's user avatar Bakudan 19.5k9 gold badges55 silver badges74 bronze badges answered Nov 15, 2010 at 15:27 ajreal's user avatar ajrealajreal 47.3k11 gold badges94 silver badges119 bronze badges 2
  • 11 Are you sure you can use table-layout on a td element? – Nick Commented Feb 8, 2014 at 1:18
  • @Nick - it works okay. Hovewer i have to add min-width and max-width property also (same value as width), and then it became truly fixed width. – Denis Khay Commented May 11, 2018 at 14:27
Add a comment | 18

I had one long table td cell, this forced the table to the edges of the browser and looked ugly. I just wanted that column to be fixed size only and break the words when it reaches the specified width. So this worked well for me:

<td><div style='width: 150px;'>Text to break here</div></td>

You don't need to specify any kind of style to table, tr elements. You may also use overflow:hidden; as suggested by other answers but it causes for the excess text to disappear.

Share Improve this answer Follow answered Dec 7, 2015 at 11:44 Tarik's user avatar TarikTarik 4,50640 silver badges36 bronze badges 1
  • 2 Or you can use max-width instead of width where needed. This will allow you not to break the text unless you reach your width limit. And the table cell won't have empty space if your text is smaller than the specified width. <td><div style='max-width: 150px;'>Text to break here</div></td> – Tarik Commented Jul 11, 2016 at 14:51
Add a comment | 6

for FULLSCREEN width table:

  • table width MUST be 100%

  • if need N colunms, then THs MUST be N+1

example for 3 columns: table.fixed { table-layout: fixed; width: 100%; } table.fixed td { overflow: hidden; } <table class="fixed"> <col width=20 /> <col width=20 /> <col width=20 /> <tr> <th>1</th> <th>2</th> <th>3</th> <th>FREE</th> </tr> <tr> <td>text111111111</td> <td>text222222222</td> <td>text3333333</td> </tr> </table>

Share Improve this answer Follow edited Aug 2, 2021 at 6:10 answered Jun 4, 2020 at 10:25 Ramil Gilfanov's user avatar Ramil GilfanovRamil Gilfanov 5921 gold badge8 silver badges12 bronze badges Add a comment | 2 table { table-layout:fixed; width:200px; } table tr { height: 20px; }

10x10

Share Improve this answer Follow edited Jan 19, 2013 at 23:46 hafichuk's user avatar hafichuk 10.7k11 gold badges40 silver badges54 bronze badges answered Jan 19, 2013 at 23:19 user1993774's user avatar user1993774user1993774 292 bronze badges Add a comment | -2 table { table-layout:fixed; } td,th { width:20px; word-wrap:break-word; }

:first-child ... :nth-child(1) or ...

Share Improve this answer Follow answered Apr 13, 2015 at 19:58 xhdix's user avatar xhdixxhdix 1551 silver badge7 bronze badges Add a comment | Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

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

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

0 Middle table column not centered? 0 How to set a permanent width for a column in HTML tables? -3 My HTML table appears jagged, even though I am using fixed pixel size 0 Give specific width to td & th and make the table scrollable 0 <TD> cells are changing size when grids are populated 58 DataTables fixed headers misaligned with columns in wide tables 3 Table cell (TD) dynamically sizing to width of longest word in column. What style causes this? 2 max-width, width not working on long text with space 1 fix the table td width 2 HTML table wrap td See more linked questions 1 Fixed width in table 92 CSS table td width - fixed, not flexible 9 Fixed column width in HTML table 1 Fixed width table -- with different width cells? 1 Fixed width table cell 1 How to make table's width fixed 0 Forcing a fixed-width table's cell to have the precise defined width 0 How to set fixed width of td within a fixed table layout 1 HTML: Make table column width fixed 0 Width on table cell that have fixed table layout don't work

Hot Network Questions

  • Is there any penalty for providing half cover to another creature?
  • In a Frequentist setting, how are we able to condition on the null hypothesis being True/False?
  • How do different observers decide if they are looking at the same thing?
  • What is the polymorph reached by letting the chocolate cool down?
  • Do referees get to see each other's reports?
  • Can you please advise on kerning?
  • Cauchy-Schwarz divergence between multivariate normal distributions
  • AVX512 assembly breaks when called concurrently from different goroutines
  • How can I protect ungrouted tile over the winter?
  • What is another word for when someone’s statement lacks integrity or meaning?
  • Missile Impact Velocity
  • Is this tv show example of hearsay actually hearsay?
  • How to say "Each one of the following" in German?
  • Do longer papers have lower chances of being accepted because they take up more "space" in a journal issue (STEM)
  • How can I solve my equation with the best numerical precision?
  • What does GB Shaw mean when he says "like the butcher's wife in the old catch" here?
  • TNG episode- Dr. Pulaski instructs another doctor on a sling
  • Counting birds outside my house
  • Plotting wavy curve in Mathematica
  • Is there a better way to implement `\ifblank` condtion check when there are multiple option arguments with default values in a command definition
  • Wouldn't the ban of TikTok violate freedom of speech?
  • Is the A321 XLR really necessary to fly MAD-BOS?
  • Does "bustle about" mean the same thing as "fluster about"?
  • What kind of solvent is effective for removing roofing tar from shoes?
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 Cell Width Fixed