make overflowing text readable with css - html

i have a table with fixed-width content. which does not always fit.
i would like to truncate the text to a fixed width and append an ellipsis. the full text should be visible when hovering the text. this works fine, except that the now visible text covers text to its right. how can i prevent this?
this is what i've got so far.
.truncate {
max-width: 5em;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: nowrap;
}
.truncate:hover {
overflow: visible
}
jsFiddle

Here is how your approach could be improved so text will be readable when hovered.
Updated HTML:
<table>
<tr>
<td><span><a class="truncate">faksjfaklsjf asjf lajslfk jasklfj alkjsfkl jalskfjla</a></span>
</td>
<td>
<span>
<a class="truncate">
faskfjalskfj alkfj laksj flkajfl kajfl ajfkl ajsl
</a>
</span>
</td>
</tr>
</table>
See how <a> is now wrapped with <span>.
And here is updated CSS:
td {
width: 5em;
}
span {
height:1.2em;
position:relative;
display: inline-block;
}
.truncate {
max-width: 5em;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: nowrap;
}
.truncate:hover {
position:absolute;
max-width: none;
background:#fff;
z-index:100;
overflow:visible;
}
span is relatively positioned, so absolute item inside it will be shown in a new layer and will take virtually no space (will not shift other text when overflow:hidden removed).
a.truncate will now became absolutely positioned on hover and will loose max width restriction. background:#fff is required to
Demo

Add a title attribute to your tag and remove the hover CSS:
HTML
<table>
<tr>
<td>
<a class="truncate" title="faksjfaklsjf asjf lajslfk jasklfj alkjsfkl jalskfjla">
faksjfaklsjf asjf lajslfk jasklfj alkjsfkl jalskfjla
</a>
</td>
<td>
<a class="truncate" title="faskfjalskfj alkfj laksj flkajfl kajfl ajfkl ajsl">
faskfjalskfj alkfj laksj flkajfl kajfl ajfkl ajsl
</a>
</td>
</tr>
</table>
CSS
td {
width: 5em;
}
.truncate {
max-width: 5em;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: nowrap;
}
Fiddle here

Related

How to use ellipsis in a table cell?

<div className="container">
<div className="left-area">
<div className="container2">
<table>
<tbody>
<tr>
<td>
48148581581581858158158iffjafjadjfjafdjafdjfjadfjdjafdjafdjajdfjadfjdafjdajfajfdjaf
</td>
<td>1/1/0001</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
I want to truncate that very long text.
.container {
margin-top: 20px;
width: 90%;
display: flex;
.left-area {
flex: 1 1 20%;
.container2 {
flex: 1 1 20%;
table {
width: 100%;
}
}
}
}
I tried to use this css on the td cell
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
The missing key is table-layout: fixed.
table {
width: 100%;
table-layout: fixed;
}
td {
width: 50%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<table>
<tbody>
<tr>
<td>
48148581581581858158158iffjafjadjfjafdjafdjfjadfjdjafdjafdjajdfjadfjdafjdajfajfdjaf
</td>
<td>1/1/0001</td>
</tr>
</tbody>
</table>
With table-layout: auto (the default setting), the browser uses an automatic layout algorithm that checks the content size to set the width of the cells (and, therefore, columns).
The width and overflow properties are ignored in this scenario, and ellipsis can't work.
With table-layout: fixed, you can define the width of the cells on the first row (and, therefore, set the column widths for the table).
The width and overflow properties are respected in this case, allowing the ellipsis function to work.
https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout

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.

CSS for floating divs with ellipsis -- widths are unknown

How can two divs float side by side where left will show ellipsis and the right side will not wrap?
This is the desired result:
The thing is, I need to do this without specifying a width for the left and right divs.
Here's a jsFiddle: http://jsfiddle.net/k8hhhjc0/1/
When the width is specified, it works. Without the widths, the right div is pushed down:
Is it possible to not specify a width for the left and right divs? The right div is always small.
Note: In the jsFiddle, I only have control over the div tags, the surrounding table can have a variable width based on user preferences.
Approach 1: Do it with CSS table, requires to have fixed width of second column.
http://jsfiddle.net/pbw2jdc8/ (adjust the output frame narrower and see)
.outer {
display: table;
table-layout: fixed;
width: 100%;
}
.row {
display: table-row;
}
.topLeft, .botLeft {
display: table-cell;
text-overflow: ellipsis;
overflow: hidden;
word-wrap: break-word;
white-space: nowrap;
}
.topRight, .botRight {
display: table-cell;
width: 60px;
text-align: right;
}
<table style="width:270px;border:1px solid grey;">
<tr>
<td>
<div class="outer">
<div class="row">
<div class="topLeft">Firstl line, this is long line and needs to show eclipise</div>
<div class="topRight">12:30pm</div>
</div>
<div class="row">
<div class="botLeft">Second line also long and needs to show eclipise</div>
<div class="botRight"><img height='16' width='16' src='http://s12.postimg.org/jbo7bw449/test.png'/></div>
</div>
</div>
</td>
</tr>
</table>
Approach 2: Using CSS flexbox, no fixed width is required.
http://jsfiddle.net/ktmwqqzk/ (adjust the output frame narrower and see)
.row {
display: flex;
}
.topLeft, .botLeft {
flex: 1 1 0;
text-overflow: ellipsis;
overflow: hidden;
word-wrap: break-word;
white-space: nowrap;
}
.topRight, .botRight {
text-align: right;
}
<table style="width:270px;border:1px solid grey;table-layout:fixed;">
<tr>
<td>
<div class="row">
<div class="topLeft">Firstl line, this is long line and needs to show eclipise</div>
<div class="topRight">12:30pm</div>
</div>
<div class="row">
<div class="botLeft">Second line also long and needs to show eclipise</div>
<div class="botRight"><img height='16' width='16' src='http://s12.postimg.org/jbo7bw449/test.png'/></div>
</div>
</td>
</tr>
</table>
Approach 3: The float first trick, again table-layout:fixed is required to ensure both text boxes to ellipsis correctly.
http://jsfiddle.net/rmLsa5aw/ (adjust the output frame narrower and see)
.topLeft, .botLeft {
text-overflow: ellipsis;
overflow: hidden;
word-wrap: break-word;
white-space: nowrap;
}
.topRight, .botRight {
float: right;
}
<table style="width:270px;border:1px solid grey;table-layout:fixed;">
<tr>
<td>
<div class="topRight">12:30pm</div>
<div class="topLeft">Firstl line, this is long line and needs to show eclipise</div>
<div class="botRight"><img height='16' width='16' src='http://s12.postimg.org/jbo7bw449/test.png'/></div>
<div class="botLeft">Second line also long and needs to show eclipise</div>
</td>
</tr>
</table>
remove the float in your css
and place your div top right first
<div class="outer">
<div class="topRight">12:30pm</div>
<div class="topLeft">Firstl line, this is long line and needs to show eclipise</div>
Then your css woule be
.topLeft {
xxxwidth:215px;
text-overflow:ellipsis;
overflow:hidden;
word-wrap:break-word;
white-space:nowrap;
}
http://jsfiddle.net/rfmny0a4/

Align span of text vertically in table cell

How can I align vertical text, that is generated by two spans, inside a div inside a table cell. I've tried many combinations of text-align,display but nothing worked. I have this html segment
<table>
<tr>
<td>
<div class="container">
<span>This is span-sentence-1</span>
<span>This is span-sentence-2</span>
</div>
</td>
</tr>
</table>
and the output is
This is span-sentence-1 This is span-sentence-2
while I want to be rendered like this
This is span-sentence-1
This is span-sentence-2
fiddle:http://jsfiddle.net/hjuxdd1b/1/
You can use following:
.container {
width: 100%;
line-height: 50px;
border: 1px solid black;
/*height: 50px; Remove height*/
}
.container span{
display: block;/*Set display to block*/
}
fiddle
Give display: block to .container span
.container span {display: block;}
Remove the height and line-height in the .container
Fiddle: http://jsfiddle.net/praveenscience/hjuxdd1b/7/
Add br tag after the first span or replace span with div.
You may not need that div:
Set those spans to display: block so they are each given their own line.
Give vertical-align: middle to the td so that its content will stay vertically centred.
Have a fiddle
By default a span is display: inline so they will line up next to each other. You could read more about the span element over on the MDN.
CSS
td {
vertical-align: middle;
height: 100px;
border: 1px solid black;
}
td span {
display: block;
}
HTML
<table>
<tr>
<td>
<span>This is span-sentence-1</span>
<span>This is span-sentence-2</span>
</td>
</tr>
</table>

Manage text overflow with a span inside a div inside a table cell

I've a long text inside a span element and I want to truncate it using ellipsis.
The span element is inside a div element which is inside a table cell.
How can I truncate the long text at the size of the cell.
Here the HTML code snippet:
<table>
<tr>
<td>
<div>
<span>
this is a long sentence to test the text overflow
</span>
</div>
</td>
</tr>
And the CSS code:
table {
width: 50px;
table-layout: fixed;
}
td {
border: 1px solid red;
}
span {
white-space: nowrap;
text-overflow: ellipsis;
}
Here my case in JSFiddle:
http://jsfiddle.net/Fedy2/wG3CF/
Test this example my friends!
span {
white-space: nowrap;
text-overflow: ellipsis;
overflow:hidden;
width:100%;
display:block;
}
Here the updated JSFiddle snippet:
http://jsfiddle.net/Fedy2/wG3CF/3/
Is it necessary to use <span>?
Its working in <p> tag.
http://jsfiddle.net/aashi/wG3CF/2/