How Can I Limit Table Column Width? - Stack Overflow

Sorry, we no longer support your browser Please upgrade to Microsoft Edge, Google Chrome, or Firefox. Learn more about our browser support.
    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 How can I limit table column width? Ask Question Asked 13 years, 3 months ago Modified 5 years, 11 months ago Viewed 187k times 100

One of the columns in my table can contain a long text without whitespaces. How can I limit its width to, say, 150px? I don't want it to be 150px always (if it's empty it should be narrow), but if there is a long text I want it to be limited with 150px and the text to be wrapped.

Here is a test example: http://jsfiddle.net/Kh378/ (let's limit the 3rd column).

Thank you in advance.

Update: Setting this styles:

word-wrap: break-word; max-width: 150px;

does not work in IE8 (tested on different computers) and I suppose it does not work in any version of IE.

Share Improve this question Follow edited Jan 20, 2019 at 10:29 Brian Tompsett - 汤莱恩's user avatar Brian Tompsett - 汤莱恩 5,87572 gold badges61 silver badges133 bronze badges asked Sep 14, 2011 at 2:56 nightcoder's user avatar nightcodernightcoder 13.5k18 gold badges67 silver badges73 bronze badges 5
  • As this question is almost three years old and it currently does not have a bounty, I was wondering whether it still needs an answer? – Frank Conijn - Support Ukraine Commented Jul 7, 2014 at 22:14
  • 1 The question was answered in another StackOverflow thread. In short, you should just use width instead of max-width. – Mikhail Larionov Commented Dec 30, 2014 at 0:08
  • 1 @Frank Conijn, Yes, it does! :) – nightcoder Commented Mar 11, 2016 at 19:38
  • That 2 years later reply though – Developer Commented Feb 16, 2017 at 16:06
  • The answer won't change, so the age does not matter :) – Reversed Engineer Commented Jun 7, 2017 at 15:19
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) 101

Updated

On the table's td and th tags I added:

word-wrap: break-word; max-width: 150px;

Not fully compatible in every browser but a start.

Share Improve this answer Follow answered Sep 14, 2011 at 3:01 Brandon's user avatar BrandonBrandon 2,1154 gold badges25 silver badges33 bronze badges 2
  • 1 Unfortunately, it doesn't work in IE8 (and I suppose it does not work in any version of IE...). – nightcoder Commented Sep 14, 2011 at 3:19
  • 2 Can this be done as a percentage, because it only takes effect as px on my end... – User Commented Oct 28, 2018 at 18:09
Add a comment | 15

You need to use width instead.

<table> <thead> <tr> <th>Name</th> <th>Age</th> <th style="width: 150px;"></th> </tr> </thead> <tbody> </tbody> </table>

But just in the <thead> section.

Share Improve this answer Follow edited Dec 26, 2017 at 18:53 answered Aug 14, 2015 at 21:09 Carlos Toledo's user avatar Carlos ToledoCarlos Toledo 2,65526 silver badges25 bronze badges 2
  • 4 What if you don't have a <thead> section? – Timmmm Commented May 18, 2020 at 10:08
  • just in case anyone wonders what to do without a <thead>, you can use column element: developer.mozilla.org/en-US/docs/Web/HTML/Element/col or just set width on the first row's td. – Long Nguyen Commented May 17 at 4:45
Add a comment | 8

You should be able to style your column with:

max-width: 150px; word-wrap: break-word;

Note that word-wrap is supported in IE 5.5+, Firefox 3.5+, and WebKit browsers (Chrome and Safari).

Share Improve this answer Follow edited Aug 27, 2017 at 19:40 Taylor D. Edmiston's user avatar Taylor D. Edmiston 13k6 gold badges58 silver badges76 bronze badges answered Sep 14, 2011 at 3:03 isNaN1247's user avatar isNaN1247isNaN1247 18.1k13 gold badges74 silver badges119 bronze badges 1
  • See this in more detail, especially the max-width and (currently) the problems there, in the similar/equivalent solution this page stackoverflow.com/a/24620364/2255628 – Destiny Architect Commented Jul 7, 2014 at 21:54
Add a comment | 5

After some experimentation in Chrome I came to the following:

  • if your table has a <th> that should have either a "width" or a "max-width" set
  • the <td>'s must have set a "max-width" and "word-wrap: break-word;"

If you use a "width" in the <th> its value will be used. If you use a "max-width" in the <th> the "max-width" value of the <td>'s is used instead.

So this functionality is quite complex. And I didn't even study tables without headers or without long strings that need a "break-word".

Share Improve this answer Follow edited Oct 11, 2017 at 7:48 answered Oct 10, 2017 at 18:34 user2587656's user avatar user2587656user2587656 4194 silver badges7 bronze badges 1
  • Can I have a reference of this? – Ziyuan Commented Aug 18, 2023 at 14:38
Add a comment | 3

there is an option to simulate max-width:

simulateMaxWidth: function() { this.$el.find('th').css('width', ''); // clear all the width values for every header cell this.$el.find('th').each(function(ind, th) { var el = $(th); var maxWidth = el.attr('max-width'); if (maxWidth && el.width() > maxWidth) { el.css('width', maxWidth + 'px'); } }); }

this function can be called on $(window).on('resize', ...) multiple times

this.$el

represents parent element, in my case I have marionette

for those th elements which should have max-width, there should be a max-width attribute like:

<th max-width="120"> Share Improve this answer Follow answered Jun 28, 2016 at 10:12 user2846569's user avatar user2846569user2846569 2,8322 gold badges25 silver badges25 bronze badges Add a comment | 1

1) Specify width for each column in <th> according to your need.

2) Add word wrap for each <td> in <tr> of the <table>.

word-wrap: break-word; Share Improve this answer Follow edited Aug 27, 2017 at 19:14 Taylor D. Edmiston's user avatar Taylor D. Edmiston 13k6 gold badges58 silver badges76 bronze badges answered May 9, 2017 at 11:43 Amay Kulkarni's user avatar Amay KulkarniAmay Kulkarni 82814 silver badges16 bronze badges Add a comment | -7

it's very easy

You could add attributes like these

max-width

max-height

They can be set in html as an attribute or in CSS as a style

Share Improve this answer Follow answered Oct 11, 2017 at 8:07 Kano's user avatar KanoKano 31 bronze badge 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
  • Why do developers love clean code but hate writing documentation?
  • This developer tool is 40 years old: can it be improved?
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

69 Table columns, setting both min and max width with css 19 Limiting table width 11 How to make width of a column fit to its contents in HTML table? 0 Set a table columns width 0 How limit the width of a table with CSS or event HTML? 18 Limit width of HTML table 2 Table column width as needed (HTML/CSS) 7 Force table column to not excess given css width 0 How can I restrict the width of an HtmlTable column? 3 How to give fixed width for the columns inside table in HTML? 1 How can I set max-width of table columns?

Hot Network Questions

  • Summoning knights from the other world
  • Help in identifying this dot-sized insect crawling on my bed
  • A SAT question about SAT property
  • Tikz: Wrapping labels in a node on a tree
  • Meaning of the diameter of a space-distorting object
  • Why is the spectrum of the Laplacian on the torus discrete?
  • Product of all binomial coefficients
  • Can we live life without "beliefs" or "leaps of faith"?
  • Horizontal arrow between two vertical arrows
  • When shouldn't I use possessive s?
  • Convergence of a power series taking values on distributions
  • Exploiting MSE for fast search
  • Why do many PhD application sites for US universities prevent recommenders from updating recommendation letters, even before the application deadline?
  • why would a search warrant say that the items to search for were the following: hair, fibers, clothing, rope wire, and binding material?
  • How to do a batch of changes in `about:config` in Firefox?
  • What is the difference between quantum field theory (QFT) and relativistic quantum theory?
  • How feasible would it be to "kill" the Sun by using blood?
  • If you are working remotely as a contractor, can you be allowed to applying as a business vistor to Australia?
  • heute Nacht = tonight or last night?
  • An extension of Lehmer's conjecture on Ramanujan's tau function
  • What explains the definition of true and false in untyped lambda calculus?
  • Should parameter names describe their object type?
  • How to Speed Up the Summation of a Sequence?
  • Single-producer single-consumer queue
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 Col Max Width