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
andbackground-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
Removing Space at The Top Left and Right of Div
Why Are Bootstrap's Form Elements Rendered Terribly with Struts2-Boostrap-Plugin
Why Would The Height Increase with a Smaller Font Size
Detecting Real Time Window Size Changes in Angular 4
Pure CSS Rotate Animation Broken While in Infinite Loop
Allow Access-Control-Allow-Origin Header Using HTML5 Fetch API
How Is The Meta Viewport Tag Used, and What Does It Do
Convert Svg Path Data to 0-1 Range to Use as Clippath with Objectboundingbox
Configuring Android Web Applications
How to Set Background-Color on 50% of Area CSS
How to Display a Range Input Slider Vertically
Items That Span All Columns/Rows Using CSS Grid Layout
Center a Div Horizontally and Vertically and Keep Centered When Resizing The Parent
How to Link to Google Maps with a Particular Longitude and Latitude
CSS Hide Scroll Bar If Not Needed