BlaM
BlaM

Reputation: 28858

Force HTML Tables To Not Exceed Their Containers' Size

This question has been asked several times, but none of the answers provided seem to help me:

See this in action here: http://jsfiddle.net/BlaM/bsQNj/2/

I have a "dynamic" (percentage based) layout with two columns.

.grid {
    width: 100%;
    box-sizing: border-box;
}
.grid > * {
    box-sizing: border-box;
    margin: 0;
}
.grid .col50 {
    padding: 0 1.5%;
    float: left;
    width: 50%;
}

In each of these columns I have a table that is supposed to use the full column width.

.data-table {
    width: 100%;
}
.data-table td {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

My problem is that some of the columns in that table have content that needs to be truncated to fit in the given width of the table. That does not happen, though. I get two tables that are overlaying each other.

Requirements:

What I tried:

Related Questions:

Upvotes: 50

Views: 77256

Answers (5)

LF-DevJourney
LF-DevJourney

Reputation: 28529

.div {
  width:300px;
  border:1px solid;
}
.breaked {
  word-break: break-all;
}
table{
  border:1px solid red;
}
td {
  border:1px solid green;
}
<div class="div">
<table>
  <tr>
    <td>aaaaaaa_________________________________________-sdasd-ad-f-asfas-df-a a-sd-fa-d-ad-fa-ds-asd-a-ads-fa-df-ads-fa-d-fad-f-ad-fad-ad-sa-fda-df-</td>
    <td>13</td>
  </tr>
  <tr>
    <td>ddddddddddddd</td>
    <td>aa</td>
  </tr>
 </table>
  <br /><hr/><br />
  <table class="breaked">
  <tr>
    <td>aaaaaaa_________________________________________-sdasd-ad-f-asfas-df-a a-sd-fa-d-ad-fa-ds-asd-a-ads-fa-df-ads-fa-d-fad-f-ad-fad-ad-sa-fda-df-</td>
    <td>13</td>
  </tr>
  <tr>
    <td>ddddddddddddd</td>
    <td>aa</td>
  </tr>
 </table>
</div>

Upvotes: 4

RAC
RAC

Reputation: 514

In your CSS:

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

That should cover all tables

Upvotes: -4

CyberAP
CyberAP

Reputation: 1233

Maybe you'll be interested in a max-width: 0; hack I've discovered.

It has some limits, we should use CSS tables instead of HTML, but it works:

.leftBlock
{
    width: 100%;
    max-width: 0;
    word-wrap: break-word;
    overflow: hidden;
}

.rightBlock
{
    width: 200px;
    max-width: 200px;
    min-width: 200px;
}

http://jsfiddle.net/CyberAP/NUHTk/103/

Upvotes: 9

eudemonics
eudemonics

Reputation: 991

you need to add the table-layout property:

table-layout: fixed;

also include width=100% in the table HTML tag, not just the style tag.

http://jsfiddle.net/reeK5/

Upvotes: 83

Mr Lister
Mr Lister

Reputation: 46549

Measurements on tables work differently. In general, width on a table cell is handled as min-width.

One solution, if you don't mind adding extra markup, is to put a div inside each table cell in which you put the content. Then give this div a width, or a max-width. So

<td>http://www.xxxxxx.xxxxxxx.com/xxx_xxxx/XXXXXXXX/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/XXXXXXXX_xXxxxx</td>

becomes

<td><div>http://www.xxxxxx.xxxxxxx.com/xxx_xxxx/XXXXXXXX/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/XXXXXXXX_xXxxxx</div></td>

and so on.

See updated fiddle: http://jsfiddle.net/bsQNj/4/

Edit: I see the fiddle needs some work - I forgot to put some divs in where they were necessary. But I hope you can work with this idea.

Upvotes: 2

Related Questions