Table Overflowing Outside of Div

Table overflowing outside of div

Turn it around by doing:

<style type="text/css">
#middlecol {
border-right: 2px solid red;
width: 45%;
}

#middlecol table {
max-width: 400px;
width: 100% !important;
}
</style>

Also I would advise you to:

  • Not use the center tag (it's deprecated)
  • Don't use width, bgcolor attributes, set them by CSS (width and background-color)

(assuming that you can control the table that was rendered)

100% width table overflowing div container

From a purely "make it fit in the div" perspective, add the following to your table class (jsfiddle):

table-layout: fixed;
width: 100%;

Set your column widths as desired; otherwise, the fixed layout algorithm will distribute the table width evenly across your columns.

For quick reference, here are the table layout algorithms, emphasis mine:

  • Fixed (source)
    With this (fast) algorithm, the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table's width, the width of the columns, and borders or cell spacing.
  • Automatic (source)
    In this algorithm (which generally requires no more than two passes), the table's width is given by the width of its columns [, as determined by content] (and intervening borders).

    [...] This algorithm may be inefficient since it requires the user agent to have access to all the content in the table before determining the final layout and may demand more than one pass.

Click through to the source documentation to see the specifics for each algorithm.

Prevent HTML table from overflowing the containing div

There is many better ways than using table to do this but i will fit to your method.

This is the whole html code that will get the wanted result (Just read the comments to see the changes):

<html>

<head>
<title>Table</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
table {
border-collapse: collapse;
}

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

table.nowrap {
white-space: nowrap;
}
</style>
</head>

<body>

<!-- Added position:relative to the div -->
<div style="width:300px;position: relative;height:300px;background-color: cyan;">

<!-- Change the table position to absolute and make its width and height 100% of the parent's -->
<table style="position: absolute;width: 100%;height: 100%;">
<tbody>
<tr style="height:40px;background-color: lightblue;">
<td>This is the static header</td>
</tr>
<tr>
<!-- Added position:relative to the cell so the div will fit to it -->
<td style="position: relative;">
<!-- Make the div position absolute and position it to the top of the cell -->
<div style="position: absolute;top: 0;width:100%;height:100%;overflow:auto;">
<!--Set width and height to 100% so it will always fill the div-->
<table class="nowrap" style="width:100%;height:100%">
<tbody>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
<tr>
<td>An item in the table</td>
<td>An item in the table</td>
<td>An item in the table</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

Table overflowing containing div in HTML

Add this to your #content css if you want the table to push out the containing div.

display: table-cell;

Table info making table overflow outside div

This issue came beacuse you have added a long text without any space. Add the following CSS. Hopefully it will resolve the issue.

 #content table td
{
word-wrap:break-all;
}

I have reproduced(similar to your image) the issue in your fiddle. Check it out HERE

After that I have added my above solution to resolve this. Check the Updated Solution HERE.



Related Topics



Leave a reply



Submit