CSS for floating divs with ellipsis -- widths are unknown - html

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/

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.

Three div in row and text of div break line when text exceeds div limits

I am using white-space: nowrap; in parent div which is sticking it's child div's in a row.
In the child div's I use word-wrap: break-word; which is not working inside of white-space:nowrap;.
My expected result is that all divs in a row even they reach screen limit but when div text exceed div width limit text line break to next line.
Fiddle Link
<div overflow-x:auto; width="100%" style="white-space: nowrap">
<div style="display: inline-block; width: 200px; border-right: solid #778899">
<div style="padding: 0 10px; background-color: white; height: 130px; overflow-x: hidden;">
div1
<p class="text-center" style="word-wrap: break-word;">
datasssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
</p>
</div>
</div>
<div style="width:200px; display: inline-block; border-right: solid #778899;">
<div class="pd-5" style="background-color: white; height: 130px; overflow-x: hidden;">
div2
<p class="text-center" style="word-wrap: break-word;">
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss ssssssss
</p>
</div>
</div>
<div style="width: 200px; display: inline-block; border-right: solid #778899;">
<div class="pd-5" style="background-color: white; height: 130px; overflow-x: hidden;">
div3
<p class="text-center" style="word-wrap: break-word;">
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
</p>
</div>
</div>
</div>
You can use white-space:pre-wrap;.
Here's an updated fiddle https://jsfiddle.net/tdc300up/1/
Here's the CSS code I used to solve it:
div > div > div {
white-space: pre-wrap; /* CSS3 */
white-space: -moz-pre-wrap; /* Firefox */
white-space: -pre-wrap; /* Opera <7 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* IE */
}
Alternatively, you could also use
div {
white-space: normal;
}
as show in this fiddle here: https://jsfiddle.net/tdc300up/3/
(take note that you don't need all of the additional pre-fixes here thanks to better support - see https://css-tricks.com/almanac/properties/w/whitespace/#browser-support)
Edit
As mentioned by WorkWe in the comments, another fix that you could try is removing the white-space:nowrap; from the main parent container (if it is not required for something else).
You can see an example fiddle here: https://jsfiddle.net/tdc300up/4/
Updated fiddle link. as said by shuvo, add white-space:normal to childern div
`https://jsfiddle.net/gopal280377/tdc300up/2/`
You should try white-space to be normal in the child.As you want to break the line..
<p class="text-center" style="word-wrap: break-word;white-space: normal;">
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
</p>
Remove wrong code of div parent:
<div overflow-x:auto; width="100%" style="white-space: nowrap">
// Some thing
</div>
To:
<div width="100%" style="white-space: nowrap">
// Some thing
</div>
Note: You can remove width="100%", because div block tag, width default is 100%.
And reset white-space for text-center class is:
.text-center {
white-space: normal;
}

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>

Aligning text in div

How can I get three divs to look as they are shown below with only HTML and CSS? Is it possible to align them in a way that the bottom text will stay under the text which is the longest?
<html>
<div id=1>
Short text
Bottom text?
</div>
<div id=2>
Long text
Bottom text?
</div>
<div id=3>
Not so long text
Bottom text?
</div>
</html>
As #Ruddy pointed outThanks for that, I used Flexbox approach for this, with CSS Positioning, so am using display: flex; for the parent element, and wrapping the bottom text in a span, and than am positioning the span to bottom using position: absolute; also, you don't have to assign fixed height to the containers, as we are using display: flex;
Flex Demo
div.parent {
display: flex;
}
div.parent > div {
border: 4px solid #000;
width: 33%;
float: left;
position: relative;
padding-bottom: 30px; /* Make sure you tweak this, to the
highest height of your bottom content*/
}
div.parent > div > span {
position: absolute;
bottom: 0;left: 0;
}
Well, obviously you can use position: absolute; with bottom: 0; with padding-bottom: 30px;(approx) and wrap the bottom text in span and use position: relative; on the container element but again, you won't be able to stretch the other two containers which doesn't have height and thus it will fail.
So you need to use display: table-cell; with vertical-align: bottom;, it will keep pushing the text to the bottom which has content, also, vertical-align: bottom; will see to it that even the other containers text stick to the bottom
Demo
div.parent {
display: table;
}
div.parent > div {
border: 4px solid #000;
width: 33%;
display: table-cell;
vertical-align: bottom;
}
Yes, this is possible. But first: give the three a wrapper. Beside that, you can't use numbers as ID's.
Here you go: http://jsfiddle.net/SP68r/
<div class="wrapper">
<div id="first">
Short text
Bottom text?
</div>
<div id="second">
Long text
Long text
Long text
Long text
Long text
<div class="bottom">bottom text</div>
</div>
<div id="third">
Not so long text
</div>
</div>
The CSS
.wrapper { width: 350px; }
#first, #second, #third { float: left; width: 100px; }
#first { margin-right: 10px; }
#second { padding-bottom: 40px; margin-right: 40px; }
div.bottom { position: absolute; bottom: 0; }
<div id="1" style="border: 2px black solid; width: 120px;
height: 160px; position: relative;">
<div style="text-align: left;" id="1-1">
Not so long text
</div>
<div style="left: 17%;position: absolute; bottom:0px;" id="1-2">
Bottom text?
</div>
</div>
You can use position:absolute
DEMO
Simple Solution : Use a table.
<table border="1" width="100">
<tr valign="top">
<td>DIV 1</td>
<td>DIV 2<BR/>THIS IS A BIG, BIG, LONG PARAGRAPH, WHICH WILL GO DOWN THAN THE OTHER TWO CELLS.</td>
<td>DIV 3</td>
</tr>
<tr>
<td>BOTTOM TEXT</td>
<td>BOTTOM TEXT</td>
<td>BOTTOM TEXT</td>
</tr>
</table>
fiddle here.

make overflowing text readable with css

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