CSS Table with one column taking remaining space - html

I have tried now several things (and looked around here) and nothing worked so far. So I am going to ask.
What I want:
I have the following simple HTML:
<table>
<tr>
<td class="small">First column with text</td>
<td class="extend">This column should fill the remaining space but should be truncated if the text is too long</td>
<td class="small">Small column</td>
</tr>
</table>
The table itself should be 100% width of the parent container.
I wish the first and last column (.small) to be as large as they need to be, so the content can fit into it without a line break (so pretty much what white-space: nowrap does). The middle column (.extend) should take the rest of the space (so the table will stay within 100% width of its parent container) and the text within .extend should be ellipsised before it needs to break to a seconds line.
I've prepared a fiddle for this at http://jsfiddle.net/3bumk/
With these background colors I would expect a result like:
Is there any solution for this?
What I get:
My problem is, if I can make the text to stay in one row (having no line breaks), the table will always overflow its parent container width (and cause it to be scrollable), before having the idea to ellipsis the text in the middle column.
What is no solution (I often found):
It's no solution to set the first and third column to a 'fixed' with (percentage or pixel), because the content will have different length from time to time. It is possible to add as many div or span as needed (or get rid of the table all together - what I tried first, with display and table but I didn't find a working solution that way either).
PS: It would be very nice if you could edit the fiddle to a working example, if you know one :-)
EDIT I am free to use divs instead of a table too!

Here is an answer using divs instead of a table: DEMO
HTML
<div class="container">
<div class="fnl first">First Baby</div>
<div class="fnl last">Last Guy</div>
<div class="adjust">I will adjust between both of you guys</div>
</div>
CSS
.container{
width: 300px;
}
.first{
float:left;
background: red;
}
.last{
float:right;
background: orange;
}
.adjust{
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}

Something like this? http://jsfiddle.net/NhGsf/
By using: display: table; width: 100%; table-layout: fixed; position: absolute; top: 0;
And setting first and last child to fixed width the middle section will have the rest off the space

min-width in combination with width:100% seems to work in firefox and chrome:
tr {
td:first-of-type {
width: 100%;
}
td:last-of-type {
min-width: 200px;
}
}

I was facing the same challenge and I found the following solution using tables.
The HTML needs to use a DIV in the long column.
The CSS defines your small and extend classes. The hard part being the definition of the extend class.
This gives you the behavior you describe.
HTML
<table>
<tr>
<td class="small">First column with text</td>
<td class="extend">
<div class="small">This column should fill the remaining space but should be truncated if the text is too long.</div>
</td>
<td class="small">Small column</td>
</tr>
</table>
CSS
table {
margin-top: 50px;
}
table td {
white-space: nowrap;
}
table td:nth-child(1) {
background-color: red;
}
table td:nth-child(2) {
background-color: green;
}
table td:nth-child(3) {
background-color: orange;
}
.extend {
display: table;
table-layout: fixed;
width: 100%
}
.small {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

You can re-arrange the columns by moving the longest one at the end then use nested tables.
CSS
.extend
{
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
-o-text-overflow:ellipsis;
}
td
{
white-space:nowrap;
}
.box
{
width:1000px;
border:blue solid thick;
overflow:hidden;
}
HTML
<div class="box">
<table width="100%" border="1">
<tr>
<td class="small">First column with text</td>
<td class="small">Small column</td>
<td>
<table>
<tr>
<td class="extend" >This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long. This column should fill the remaining space but should be truncated if the text is too long.</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
Except for the ellipsis it is working well. See result

See this fiddle (or, alternatively), you need to set the max-width for each table cell:
body{
width:100%;
padding:0;
margin:0;
}
table {
margin-top: 50px;
width:100%;
max-width:100%;
}
table td {
white-space: nowrap;
}
table td:nth-child(1) {
background-color:red;
max-width:100px;
}
table td:nth-child(2) {
background-color: green;
overflow:hidden;
max-width:100px;
text-overflow: ellipsis;
white-space: nowrap;
}
table td:nth-child(3) {
background-color: orange;
max-width:100px;
}

Related

Text wrap in table with fill-remaining-width middle column

I am trying to get a horizontal line to stretch between the first and last columns in a table but I need the first and last columns to wrap if the text is long. The only way I have found to get the desired effect is to use width:100%; on the middle column, and white-space:nowrap; on the first and last, but I need to find another way as I need the text to wrap when there isn't enough space. Is there a way to achieve this effect in plain CSS?
https://jsfiddle.net/macu/8axk5qv5/4/
table {
width: 100%;
}
td {
vertical-align: middle;
white-space: nowrap;
}
td:nth-child(2) {
width: 100%;
}
.line {
border-top: thin solid blue;
}
<table>
<tr>
<td>Title cell with a long title that should wrap</td>
<td><div class="line"></div></td>
<td>Another cell, should wrap</td>
</tr>
</table>
If the text is long enough there should be no line, and the text should wrap normally:
You can put a span or div in each cell, and make them to use white background, then set the line on the table row to create such layout visually.
Check out the fiddle demos below, so you can easily resize and see the wrapping text.
jsFiddle
.table {
width: 100%;
border-collapse: collapse;
}
.table tr {
background: linear-gradient(blue, blue) center/99.99% 1px no-repeat;
}
.table div {
background: white;
display: inline-block;
}
.middle div {
min-width: 100px; /*remove or adjust value as need*/
}
.last {
text-align: right;
}
<table class="table">
<tr>
<td class="first">
<div>Title cell with a long title that should wrap</div>
</td>
<td class="middle">
<div><!-- This td can be removed if no min-width needed --></div>
</td>
<td class="last">
<div>Another cell, should wrap</div>
</td>
</tr>
</table>
But using flexbox can make it much easier, if you don't have to use table.
jsFiddle
.container {
display: flex;
}
.line {
background: linear-gradient(blue, blue) center/1px 1px repeat-x;
flex: 1;
min-width: 100px; /*remove or adjust value as need*/
}
<div class="container">
<div>Title cell with a long title that should wrap</div>
<div class="line"></div>
<div>Another cell, should wrap</div>
</div>
try by removing the white-space: nowrap; on the TD tag, then target the first and the third TD with
td {
vertical-align: middle;
//white-space: nowrap;
}
td:nth-child(1),td:nth-child(3) {
//add whatever min-width AND max-width so it could be something like this
min-width:150px;
max-width:300px;
}
see if that helps.

text overflow ellipsis not working for div inside table cell with flexible width [duplicate]

This question already has answers here:
How can I truncate table cells, but fit as much as content possible?
(19 answers)
Closed 7 years ago.
I searched stack overflow and looked at some solutions but nothing worked so far. What I have is a table with flexible column widths and one of the table cell has divs with some text that I want to have ellipses for overflow.
JS fiddle example here:
<table>
<td class= 'name'>Name</td>
<td class='desc'>
<div>
<div class='desc-1'>descript1:</div>
<div class='desc-2'>really long description that I want to have ellipses for ajhdfjhdf ajhdf akjdhf asdjfh askdjfh askdjfh asdkjfh askjdfh askdjfh askjdfhaksjdhf askjfdh ajkdsfh
</div>
</div>
</td>
</table>
.name {
width: 50%;
}
.desc {
width: 50%;
max-width: 100%;
}
td {
border: 1px solid black;
}
td div {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
http://jsfiddle.net/tgowurLu/
If I set the width for the .desc class to a fixed value, it works fine, like here:
.desc {
width: 50%;
max-width: 300px;
}
http://jsfiddle.net/Lx71r05b/
How to get it to work for flexible grid column width (percentage value)?
Edit: The solution here helped solve my problem:
CSS: Truncate table cells, but fit as much as possible
please use this code
/*Please use this code in CSS*/
.responsive {width:100%;overflow:hidden;}
.table-data {width:100%;max-width:100%}
/*you can give fix width as below*/
.desc {
width: 50%;
max-width: 300px;
}
<!--please use this code in html:-->
<div class="responsive"><table class="table-data">
<td class= 'name'>Name</td>
<td class='desc'>
<div>
<div class='desc-1'>descript1:</div>
<div class='desc-2'>really long description that I want to have ellipses for ajhdfjhdf ajhdf akjdhf asdjfh askdjfh askdjfh asdkjfh askjdfh askdjfh askjdfhaksjdhf askjfdh ajkdsfh
</div>
</div>
</td>
</table></div>

Why isn't my TD element wrapping around text content?

I have a table, where the left td takes the majority of the space, and the right td have very little space which forces the words to break.
I am trying to make so that both TD are the same width of the text content inside. I have done some research online but nothing I found fixed the problem.
Any help would be really appreciated! Below is my table code and css
CSS:
#order_info_container {
width: 620px;
margin-left: auto;
margin-right: auto;
margin-bottom: 40px;
}
#customer_service {
padding-top: 10px;
vertical-align: top;
text-align : right;
}
#order_information { padding-top: 20px; }
#totals_o {
padding-top: 20px;
text-align: right;
}
HTML:
<table id='order_info_container'>
<!--This left td is taking most of the space -->
<tr>
<td><img id='o_summary_logo' src='o_summary_logo.png'></td>
<!--This right td is being squished -->
<td id='customer_service'><b>Customer Service # (646)-397-5751</b><br>
Thank you for your business </td>
<!--This left td is taking most of the space -->
<tr id='totalswrapper_o'>
<td id='order_information'>Order Placed - 07/01/14 12:18AM<br>
<span id='order_number'>Order # - 775</span><br>
<span class='conf_num'>Confirmation # - 81<br>
</span></td>
<!--This right td is being squished -->
<td id='totals_o'>Sub Total = $28.95<br>
Tax = $2.57<br>
Grand Total = $33.52<br></td>
</tr>
</table>
You can use the table-layout: fixed attribute on your table so that all cells have the same width. Example on jsfiddle here:
HTML:
<table>
<tr>
<td>column1 Lorem Ipsum dolor sit amet</td>
<td>column2 consectetur…</td>
</tr>
</table>
CSS:
table {
table-layout: fixed;
width: 200px;
border-collapse: collapse;
}
td {
border: 1px solid red;
}
Also, the fixed layout will automatically give the same proportion of the length to any number of cells you have, i.e. if you had 2 cells, each one will be 50% of the table, 3 would be 33% each and so on… You can play with the fiddle to try this.
Defining a width of 50% for your <td> in CSS should solve this problem. Having said that, 50% will work if you have only 2 <td>'s.
table td{
width: 50%;
}
FIDDLE. I have added a border in fiddle to show this effect clearly.
A better way would be to use the table-layout: fixed; property on table like #arielnmz recommends in his answer. It works best even when you have dynamically generated columns. This way, you won't have to calculate and define how much space a column takes. They will always be equally divided. So, you should probably go with this approach instead.
table{
width: 100%;
table-layout: fixed;
}
FIDDLE

Table with no wrap, one column with overflow and as big as possible

I have a fixed-width table of 400px with 3 columns.
<table>
<tbody>
<tr>
<td class="wide">This is really really long and as much as possible should show but should eventually be cut off.</td><td class="narrow">Small1</td><td class="narrow">Small2</td></tr>
</tbody>
</table>
Here is the CSS.
table
{
table-layout: fixed;
width: 400px;
}
td
{
border: 1px solid #000;
white-space: nowrap;
}
td.wide
{
overflow: hidden;
text-overflow: ellipsis;
}
td.narrow
{
}
Here is the JSFiddle.
Currently, each of the 3 columns takes up 1/3 of the space. What I want is for the 2nd and 3rd columns to be as small as possible (without anything hidden or text-wrapped) and have the 1st column take up the remainder of the space (with any text that doesn't fit being hidden).
Depending on the data displayed, the 2nd and 3rd columns may need to be wider or narrower to fit their content, so I don't want to define a fixed size for any column.
Is this possible?
Here is the only solution i found. It's pretty ugly but it does the trick :
http://jsfiddle.net/XA9kY/
The thing is to wrap the string to be overflowing into a .... table
Notice the table into the td.wide
<div style="width:400px; border: 1px solid red;">
<table>
<tbody>
<tr>
<td class="wide">
<table>
<tr>
<td>This is really really long and as much as possible should show but should eventually be cut off.
</td>
</tr>
</table>
</td>
<td class="narrow">Small1</td>
<td class="narrow">Small2</td>
</tr>
</tbody>
</table>
</div>
And here is the magic
td.wide table
{
width: 100%;
table-layout: fixed;
}
td.wide table td
{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Just wrapping the string into a table with table-layout: fixed; property does the trick.
Here's my try at this: (Example)
<table>
<tbody>
<tr>
<td class="wide">This is really really long and as much as possible should show but should eventually be cut off.</td>
<td class="rest">Small1</td>
<td class="rest">Small2</td>
</tr>
</tbody>
</table>
CSS
table {
width: 400px;
}
table td {
border: 1px solid #000;
}
td.rest {
width:1px;
}
The only thing is that it doesn't like:
overflow: hidden;
text-overflow: ellipsis;
If that isn't an issue then this should work.
EDIT
A possible solution to hide the text in the wider cell by just setting the height.
Added also the line-height for all the cells so you can change both based on the settings you're after.
Here the (Example)
table {
width: 400px;
}
table td {
border: 1px solid #000;
line-height:26px;
}
td.rest {
width:1px;
white-space: nowrap;
}
td.wide {
overflow:hidden;
height:26px;
display:block;
}

Rounding width aka spread divs evenly

I'm struggling spreading fives divs evenly inside a resizeable div. I have looked at How to spread elements evenly (horizonatly)? but cant get it to work :-(
The problem seems to be that even thoug my div width is set to 20% the last element is wrapped onto the next line if the surrounding divs width is not dividable by 5.
My css is this
#exposureSummaryContainer > div { width: 20%;display: inline-block;}
and my html:
<div id="exposureSummaryContainer">
<div>1.000.000</div>
<div>1.000.000</div>
<div>1.000.000</div>
<div>1.000.000</div>
<div>1.000.000</div>
</div>
How do I ensure that all five elements stay on the same line?
I made this http://jsfiddle.net/GTwFb/ to illustrate my problem.
Add float:left;
#exposureSummaryContainer > div { width: 20%;display: inline-block; float:left; background:red}
DEMO
There is a whitespace you don't see, if you removed it,
<div id="exposureSummaryContainer"><div>1.000.000</div><div>1.000.000</div><div>1.000.000</div><div>1.000.000</div><div>1.000.000</div></div>
it shall work.
This is maybe not the best looking way.. but its working
#exposureSummaryContainer { width: 100%; display: table; }
#exposureSummaryContainer > div { width: 20%;display: table-cell;}
You can edit the width to a specific size
the problem is the physical space on the markup amongs div elements when their display is inline-block, so a workaround is the following:
#exposureSummaryContainer {
white-space: nowrap;
word-spacing: -3px;
letter-spacing: -3px;
}
#exposureSummaryContainer > div {
width: 20%;
display: inline-block;
white-space: normal;
word-spacing: normal;
letter-spacing: normal;
}
I'd use the float: left; property
This works for me:
http://jsfiddle.net/GTwFb/2/
Well I would you the trick of table:
<table style="width:whateveryouwant">
<tr>
<td style="width:20%"><div>1.000.000</div></td>
<td style="width:20%"><div>1.000.000</div></td>
<td style="width:20%"><div>1.000.000</div></td>
<td style="width:20%"><div>1.000.000</div></td>
<td style="width:20%"><div>1.000.000</div></td>
</tr>
</table>
This works for me all the time ^_^