flavour404
flavour404

Reputation: 6312

HTML CSS How to stop a table cell from expanding

I have a table which is built with the contents coming from a returned dataset. What I want to do is stop a 'description' cell from expanding over 280px wide, no matter what the content length (its s string). I have tried:

<td align="left" valign="top" style="overflow:hidden;" nowrap="nowrap" width="280px" >

But this doesn't seem to work. I don't want it to wrap, nor do I want anything over 280px to be displayed.

Upvotes: 49

Views: 160667

Answers (9)

JP Silvashy
JP Silvashy

Reputation: 48525

<table border="1" width="183" style='table-layout:fixed'>
  <col width="67">
  <col width="75">
  <col width="41">
  <tr>
    <td>First Column</td>
    <td>Second Column</td>
    <td>Third Column</td>
  </tr>
  <tr>
    <td>Row 1</td>
    <td>Text</td>
    <td align="right">1</td>
  </tr>
  <tr>
    <td>Row 2</td>
    <td>Abcdefg</td>
    <td align="right">123</td>
  </tr>
  <tr>
    <td>Row 3</td>
    <td>Abcdefghijklmnop</td>
    <td align="right">123456</td>
  </tr>
</table>

I know it's old school, but give that a try, it works.

may also want to add this:

<style>
  td {overflow:hidden;}
</style>

Of course, you'd put this in a separate linked stylesheet, and not in the head of the html... wouldn't you ;)

Upvotes: 28

Ali Kayn
Ali Kayn

Reputation: 1

I've tested these solutions, and I suspect that word-wrap: break-word does not work on URLs. I could get a URL with spaces to break on a space, but the cells still expand despite fixed layout.

Upvotes: 0

Codemaker2015
Codemaker2015

Reputation: 1

Try the following css to stop expanding the table and it's cells.

table {
    table-layout: fixed;
    width: 100%;
}

th, td {
    word-wrap: break-word;
}

table-layout: fixed will make your table fixed. But still the columns / cells will overlow or expand. To fix that issue use word-wrap: break-word

Upvotes: 6

user10266843
user10266843

Reputation:

Simply set the max-width attribute to 280px like this:

<td align="left" valign="top" style="overflow:hidden;" nowrap="nowrap" max-width="280px" width="280px">

This will solve your problem.

Upvotes: 1

Juan David Arce
Juan David Arce

Reputation: 902

This could be useful. Like another answer it is just CSS.

td {
    word-wrap: break-word;
}

Upvotes: 1

makle
makle

Reputation: 1267

To post Chris Dutrow's comment here as answer:

style="table-layout:fixed;" 

in the style of the table itself is what worked for me. Thanks Chris!

Full example:

<table width="55" height="55" border="0" cellspacing="0" cellpadding="0" style="border-radius:50%; border:0px solid #000000;table-layout:fixed" align="center" bgcolor="#152b47">
  <tbody>
    <td style="color:#ffffff;font-family:TW-Averta-Regular,Averta,Helvetica,Arial;font-size:11px;overflow:hidden;width:55px;text-align:center;valign:top;whitespace:nowrap;">

     Your table content here

    </td>
  </tbody>
</table>

Upvotes: 8

John
John

Reputation: 13729

It's entirely possible if your code has enough relative logic to work with.

Simply use the viewport units though for some the math may be a bit more complicated. I used this to prevent list items from bloating certain table columns with much longer text.

ol {max-width: 10vw; padding: 0; overflow: hidden;}

Apparently max-width on colgroup elements do not work which is pretty lame to be dependent entirely on child elements to control something on the parent.

Upvotes: 0

Fran&#231;ois DELAGE
Fran&#231;ois DELAGE

Reputation: 41

No javascript, just CSS. Works fine!

   .no-break-out {
      /* These are technically the same, but use both */
      overflow-wrap: break-word;
      word-wrap: break-word;

      -ms-word-break: break-all;
      /* This is the dangerous one in WebKit, as it breaks things wherever */
      word-break: break-all;
      /* Instead use this non-standard one: */
      word-break: break-word;

      /* Adds a hyphen where the word breaks, if supported (No Blink) */
      -ms-hyphens: auto;
      -moz-hyphens: auto;
      -webkit-hyphens: auto;
      hyphens: auto;

    }

Upvotes: 4

RSolberg
RSolberg

Reputation: 26972

It appears that your HTML syntax is incorrect for the table cell. Before you try the other idea below, confirm if this works or not... You can also try adding this to your table itself: table-layout:fixed.. .

<td style="overflow: hidden; width: 280px; text-align: left; valign: top; whitespace: nowrap;">
   [content]
</td>

New HTML

<td>
   <div class="MyClass"">
       [content]
   </div>
</td>

CSS Class:

.MyClass{
   height: 280px; 
   width: 456px; 
   overflow: hidden;
   white-space: nowrap;
}

Upvotes: 35

Related Questions