I have done quite a bit or searching on this but could not find a solution for my specific case. I am a beginner to CSS and the solution is probably really simple, I just cannot find it. So I am trying to create a table that has some columns with long text. I am able to show ellipsis to cut off too long text, but I would like to have the column resize and the ellipsis be updated if I reduce the size of the window, that also reduces the size of the table itself. Now the cell width remains the same and a horizontal scroll bar is shown, which I want to prevent. I know using fixed width for the column is probably the problem, but it is the only way I am able to show the ellipsis in the first place, without fixed width no ellipsis is shown. Here is a very simplified piece of code and the css that easily shows the problem:
<div class="tableFixHead">
<table>
<thead>
<tr>
<th>one</th>
<th>two</th>
</tr>
</thead>
<tbody>
<tr>
<td>
first column
</td>
<td>
<div class="overflowTableText">
the entire text that is way too long to show all at once
</div>
</td>
</tr>
</tbody>
</table>
</div>
.tableFixHead {
overflow: scroll;
height: 200px;
max-width: 50%;
}
.overflowTableText {
width: 300px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
-o-text-overflow: ellipsis;
}
Any pointers are appreciated
One way to do this is to use #media query. Do something like this
.tableFixHead {
overflow: scroll;
height: 200px;
max-width: 50%;
}
.overflowTableText {
max-width: 300px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* add as many break points you want */
#media screen (max-width: 768px) {
.overflowTableText {
max-width: 200px;
}
}
#media screen (max-width: 576px) {
.overflowTableText {
max-width: 150px;
}
}
<div class="tableFixHead">
<table>
<thead>
<tr>
<th>one</th>
<th>two</th>
</tr>
</thead>
<tbody>
<tr>
<td>first column</td>
<td>
<div class="overflowTableText">
the entire text that is way too long to show all at once
</div>
</td>
</tr>
</tbody>
</table>
</div>
After fiddling around a bit I found a solution that works with multiple columns with ellipsis:
<div class="tableFixHead">
<table width="100%">
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
</thead>
<tbody>
<tr>
<td width="100px">
first column
</td>
<td class="overflowTableText"><span>
the entire text that is way too long to show all at once the entire text that is way too long to show all at once</span>
</td>
<td class="overflowTableText"><span>
the entire text that is way too long to show all at once the entire text that is way too long to show all at once</span>
</td>
</tr>
</tbody>
</table>
</div>
.tableFixHead {
overflow: scroll;
height: 200px;
border-collapse: separate;
width: 90%;
padding-right: 8px;
}
.overflowTableText {
max-width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
-o-text-overflow: ellipsis;
}
Good day, Do you know bootstap? you can easily do that with bootsrap
https://getbootstrap.com/docs/4.1/content/tables/#responsive-tables
Related
I'm trying to implement ellipsis and it's partially working - I am having one problem, when I hover on the very long work, it goes on top of the other words. I can't explain it very well, so here's a picture to show what exactly is happening:
Before hover:
After hover:
Here's my code:
.attachment td {
max-width: 300px;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: nowrap;
}
.attachment td:hover {
overflow: visible;
white-space: normal;
}
<body>
<h1>The text-overflow Property</h1>
<p>Hover over the div below, to see the entire text.</p>
<div class="a">This is some long text that will not fit in the box.</div>
<table class="table table-striped table-hover">
<tbody>
<tr class="attachment hover-parent">
<td>
bhdvbcjkdsbjdhskbcsajdkggfbjaksbfisjkadzhfnisfjdkzvbcds,zhvmcneajsdkzvbdjszvbsdjzkvbdsjzkxvbdscjkvbdcxzjhvbdfshzjkxvbd
</td>
<td>
download
</td>
<td>
delete
</td>
</tr>
</tbody>
</table>
I feel like I'm missing out on one thing, I just can't pin point it. Any help?
Edit:
Here's my desired effect:
Edit:
What I needed was overflow-wrap: breakword;, thanks Raina
overflow-y is better
https://jsfiddle.net/2g1jham6/
<head>
<style>
.attachment td {
max-width: 300px;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: nowrap;
}
.attachment td:hover {
overflow-x: hidden;
overflow-y: scroll;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h1>The text-overflow Property</h1>
<p>Hover over the div below, to see the entire text.</p>
<div class="a">This is some long text that will not fit in the box.</div>
<table class="table table-striped table-hover">
<tbody>
<tr class="attachment hover-parent">
<td>
bhdvbcjkdsbjdhskbcsajdkggfbjaksbfisjkadzhfnisfjdkzvbcds,zhvmcneajsdkzvbdjszvbsdjzkvbdsjzkxvbdscjkvbdcxzjhvbdfshzjkxvbd
</td>
<td>
download
</td>
<td>
delete
</td>
</tr>
</tbody>
</table>
I've updated your code. The reason for the overlapping is because you have download and delete in their own td elements in the same table.
See below
.attachment {
max-width: 300px;
overflow: hidden;
display: inline-block;
text-overflow: ellipsis;
white-space: no-wrap;
width: auto;
}
.attachment:hover {
overflow: visible;
white-space: normal;
}
.option {
display: inline-block;
text-overflow: none;
}
<body>
<h1>The text-overflow Property</h1>
<p>Hover over the div below, to see the entire text.</p>
<div class="a">This is some long text that will not fit in the box.</div>
<table class="table table-striped table-hover">
<tbody>
<tr>
<td class="attachment"> bhdvbcjkdsbjdhskbcsajdkggfbjaksbfisjkadzhfnisfjdkzvbcds,zhvmcneajsdkzvbdjszvbsdjzkvbdsjzkxvbdscjkvbdcxzjhvbdfshzjkxvbd
</td>
<tr>
<td class="option">
download
</td>
<td class="option">
delete
</td>
</tr>
</tr>
</tbody>
</table>
<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
I have a table where I'm trying to set the width of the first column to 20%, and wrap the content of its cells in ellipsis. My goal is to keep the 20% width fixed regardless of screen size and length of cell content. However, when the content of the cell is long, the cell streches beyond 20%. I did try table-layout: fixed, but for some reason it caused the browser to completely ignore my column width instructions. I'm working with Chrome, here's a jsfiddle:
http://jsfiddle.net/07sktof2/7/
Thanks.
You need to set a td max-width:
table {
font-size: 14px;
width: 100%;
border: 1px solid;
border-collapse: collapse;
}
td {
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
border: 1px solid black;
}
.first {
width: 20%;
padding-right: 20px;
}
.first .ellipsis {
width: 90%;
display:inline-block;
overflow: hidden;
text-overflow: ellipsis;
word-wrap: nowrap;
}
<table>
<thead>
<tr>
<th>header 1</th>
<th>header 234567895678657</th>
</tr>
</thead>
<tbody>
<tr>
<td class="first">
<span class="ellipsis">
data long long long long long long long long long long long long cell
</span>
</td>
<td class="second">data 2</td>
</tr>
<tr>
<td class="first">short</td>
<td class="second">data 2</td>
</tr>
</tbody>
</table>
Try it Online!
I have a trouble with x-scroll table, when I try to resize window to small size. I have the same table (with same css) which doesn't contain row with images and it works great.
HTML:
<div class="price_wrapper">
<table class="price">
<thead>
<tr class="head">
<th>Название</th>
<th>Вольерная 1</th>
<th>Вольерная 2</th>
<th>Вольерная 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Схема</td>
<td><img src="http://king-bitovki.ru/static/pub/img/products/budki/volyer1.jpg" alt=""></td>
<td><img src="http://king-bitovki.ru/static/pub/img/products/budki/volyer2.jpg" alt=""></td>
<td><img src="http://king-bitovki.ru/static/pub/img/products/budki/volyer3.jpg" alt=""></td>
</tr>
<tr>
<td>Ширина</td>
<td>90</td>
<td>120</td>
<td>150</td>
</tr>
<tr>
<td>Высота</td>
<td>60</td>
<td>70</td>
<td>80</td>
</tr>
<tr>
<td>Глубина</td>
<td>60</td>
<td>80</td>
<td>90</td>
</tr>
<tr>
<td>Лаз</td>
<td>30x35</td>
<td>39x42</td>
<td>42x53</td>
</tr>
<tr>
<td>Стоимость, руб.</td>
<td>7 000</td>
<td>9 000</td>
<td>11 000</td>
</tr>
</tbody>
</table>
</div>
CSS:
.price_wrapper {
overflow-x: scroll;
overflow-y: auto;
min-width: 400px;
width: 100%;
}
table.price {
border-collapse: collapse;
background-color: #FFF;
}
table.price th,
table.price td {
border: solid 1px #999;
padding: 5px;
}
table.price img {
max-width: 180px;
width: 100%;
height: auto;
}
JSFiddle: https://jsfiddle.net/vasromand/gue888vp/
Why is it?
You must use width for the scroll to
work.price_wrapper {
overflow-x: autol;
overflow-y: auto;
max-width: 400px;
width: 100%;
}
And dont use scroll because it will not be working in ie so instead you can use auto for it and set max-width or width to enable the auto scroll
Your content is not overflowing, you're using the width 90% of its container.. there never will be an overflow to scroll..
800px Content
table.price {
width: 800px;
margin: 10px 5%;
border-collapse: collapse;
background-color: #FFF;
}
400px container
.price_wrapper {
overflow-x: scroll;
overflow-y: visible;
width: 400px;
}
This will force the content to be wider than the wrapper.. which has the overflow-x
Also as correctly suggested by the other answer, use overflow:auto
Here is an updated version which will FORCE overflow to show you an example..
JSFiddle
As you can see i have forced the child to be wider than its parent.. Therefore offering scroll.
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;
}