Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table overflows parent div when td content is too wide

I prepared a JSFiddle to explain/show you my problem: http://jsfiddle.net/nz96C/

It looks alright at first, but when I add some text to #firsttd the whole table overflows the parent div once the tds whole width is used. I know how to solve this problem with CSS (#firstdiv {width:90px;overflow:hidden;}) but I don't know the exact number of pixels (percentage doesn't work). Also I don't want the text in the first td to wrap.

I hope you get my problem, I even have trouble explaining it in my first language.

EDIT: My goal is to have a table in which it doesn't matter how long the text in the first td is, the table should never overflow the parent div - without the text being wrapped. I want the text which overflows the first td just not to be shown.

like image 932
Timon Avatar asked Jul 25 '14 19:07

Timon


People also ask

Why content overflowing out of div?

The content overflows the box and falls into the paragraph below. This box has a height and a width. This means that if there is too much content to be displayed within the assigned height, there will be an overflow situation. If overflow is set to hidden then any overflow will not be visible.

How to fix overflowing div?

Add a padding top on the #scrollable div and make the #menu div absolute. Add a z-index to the #menu div so it stays on top. Also, make sure to add box-sizing: border-box on all elements to prevent exceeding the assigned sizes. Save this answer.

How to prevent text overflow in div?

To fix this problem, avoid using vw for your max-width and use a max-width of 100% instead. This will transfer the width of the parent container to the width of the child container.

How do you stop a table from overflowing in HTML?

The trick is to use the CSS property table-layout. It can take three values: auto , fixed , and inherit . The normal (initial) value is auto , which means that the table width is given by its columns and any borders. In other words, it expands if necessary.


1 Answers

Same answer as another very recent topic:table-layout:fixed; + width. DEMO

table {
    table-layout:fixed;
    width:100%;
}
td {
    border: 1px solid black;
    overflow:hidden;/* optionnal*/
}
#firsttd {
    white-space: nowrap;
}

edit : about comment below :

@MatthieuRiegler

table-layout:fixed; is bad if you want cell with different width.

here is a demo where table-layout and cell's width can be different:

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

table,
td,
th {
  border: 1px solid;
}

thead tr th:nth-child(1) {
  width: 5em;
}

thead tr th:nth-child(2) {
  width: 20%;
}

thead tr th:nth-child(3) {
  width: 7.5em;
}
<table>
  <thead>
    <tr>
      <th>Head Cell</th>
      <th>Head Cell</th>
      <th>Head Cell</th>
      <th>Head Cell</th>
      <th>Head-Cell-Text</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
  </tbody>
</table>

width + table-layout:fixed might just be a bad idea if no responsiveness is thought aside.

like image 120
G-Cyrillus Avatar answered Oct 12 '22 07:10

G-Cyrillus