CSS: Constrain a table with long cell contents to page width?
To achieve this, you have to wrap the long text into two divs. The outer one gets position: relative
and contains only the inner one. The inner one gets position: absolute
, overflow: hidden
, and width: 100%
(this constrains it to the size of the table cell).
The table-layout algorithm now considers those table cells empty (because absolutely-positioned elements are excluded from layout calculations). Therefore we have to tell it to grow that column regardless. Interestingly, setting width: 100%
on the relevant <col>
element works, in the sense that it doesn’t actually fill 100% of the width, but 100% minus the width of all the other columns (which are automatically-sized). It also needs a vertical-align: top
as otherwise the (empty) outer div is aligned middle, so the absolutely-positioned element will be off by half a line.
Here is a full example with a three-column, two-row table showing this at work:
CSS:
div.outer {
position: relative;
}
div.inner {
overflow: hidden;
white-space: nowrap;
position: absolute;
width: 100%;
}
table {
width: 100%;
border-collapse: collapse;
}
td {
border: 1px dotted black;
vertical-align: top;
}
col.fill-the-rest { width: 100%; }
HTML:
<table>
<col><col><col class="fill-the-rest">
<tr>
<td>foo</td>
<td>fooofooooofooooo</td>
<td>
<div class='outer'>
<div class='inner'>
Long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text
</div>
</div>
</td>
</tr>
<tr>
<td>barbarbarbarbar</td>
<td>bar</td>
<td>
<div class='outer'>
<div class='inner'>
Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text Some more long text
</div>
</div>
</td>
</tr>
</table>
In this example, the first two columns will have the minimum possible size (i.e. spaces will make them wrap a lot unless you also add white-space: nowrap
to them).
jsFiddle: http://jsfiddle.net/jLX4q/
How can I constrain the contents of a table cell which contains wide HTML to an overall particular width?
with your explanation and without examples we don't help you so much, but i think i can help you.
with this CSS properties:
my-cell {
box-sizing: border-box;
width: 100%;
border: solid #5B6DCD 10px;
padding: 5px;
}
you can visit the MDN link: box-sizing Mozilla developer's
or you can try with this css property inside your div container or cell overflow: hidden;
hope this help you.
Set the table column width constant regardless of the amount of text in its cells?
I played with it for a bit because I had trouble figuring it out.
You need to set the cell width (either th
or td
worked, I set both) AND set the table-layout
to fixed
. For some reason, the cell width seems to only stay fixed if the table width is set, too (I think that's silly but whatev).
Also, it is useful to set the overflow
property to hidden
to prevent any extra text from coming out of the table.
You should make sure to leave all of the bordering and sizing for CSS, too.
Ok so here's what I have:
table { border: 1px solid black; table-layout: fixed; width: 200px;}
th,td { border: 1px solid black; width: 100px; overflow: hidden;}
<table> <tr> <th>header 1</th> <th>header 234567895678657</th> </tr> <tr> <td>data asdfasdfasdfasdfasdf</td> <td>data 2</td> </tr></table>
CSS text-overflow in a table cell?
To clip text with an ellipsis when it overflows a table cell, you will need to set the max-width
CSS property on each td
class for the overflow to work. No extra layout div
elements are required:
td
{
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
For responsive layouts; use the max-width
CSS property to specify the effective minimum width of the column, or just use max-width: 0;
for unlimited flexibility. Also, the containing table will need a specific width, typically width: 100%;
, and the columns will typically have their width set as percentage of the total width
table {width: 100%;}
td
{
max-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
td.column_a {width: 30%;}
td.column_b {width: 70%;}
Historical: For IE 9 (or less) you need to have this in your HTML, to fix an IE-specific rendering issue
<!--[if IE]>
<style>
table {table-layout: fixed; width: 100px;}
</style>
<![endif]-->
Table should be only as wide as necessary
Do not set width on the table at all. This will make the table only as wide as needed by its content. If this looks too wide, look at the table content to see what causes width requirements in cells. For example, if you have long text in a cell, it will expand the cell to be as wide as needed, within the available width. If you wish to limit this, you need to set width
or max-width
on the content of a cell, or the cell itself, or the table.
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)
Related Topics
How to Customize the HTML5 Input Range Type Looks Using CSS
Differencebetween Default, User and Author Style Sheets
How to Match 3D Perspective of Real Photo and Object in CSS3 3D Transforms
Django Templates: Group Items in Threes
Change Parent Div on Input[Type=Checkbox]:Checked with CSS
Fancy Media Queries with Some Less Magic
How to Rotate Text Left 90 Degree and Cell Size Is Adjusted According to Text in HTML
CSS Flexbox: Difference Between Align-Items and Align-Content
Single VS Multiple Stylesheets in Responsive Web Design
Stopping a CSS Animation But Letting Its Current Iteration Finish
Combining Border-Top,Border-Right,Border-Left,Border-Bottom in CSS
Using Modulus in CSS Calc Function