I'm trying to make a two column CSS table (tried HTML table too) with one row where the first column is one line of text and it expands to fit the content. The second column needs to be a single line of text that is right justified and expands left until it hits the first column and then becomes an ellipsis.
This first column is working with white-space: nowrap;. The second column is the issue:
When the content is longer than the second column max width, it will wrap. Once I add white-space: nowrap; it overflows and ignores the width, max-width and overflow: hidden;
Here is a JSFiddle showing what I'm getting vs. what I'm trying to get. Thanks!
https://jsfiddle.net/esodell1/oq59jkr3/10/
UPDATE
Thanks for the responses, but I found a way to accomplish this using flex box!
https://jsfiddle.net/esodell1/jdykzv5m/13/
So this is how the text-overflow technique works. There needs to be a defined width of it's parent or a closest parent. You'll need to defined a width for your cell such as:
&:last-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 85%;
text-align: right;
}
But you'll also have to limit your table since table naturally tries to expand as much as the content in its child requires by using table-layout:
.table {
display: table;
padding: 0.25rem;
table-layout: fixed;
width: 100%;
}
This will make the right column create the ellipsis when the cell reaches 85% of its available width. The problem you'll be faced with is dealing with the left column because you also have no-wrap set on it so that column might actually exceed 15% width and overlap with the right. The issue is both your column is trying not to wrap and fighting for table space, you'll need to apply the same text-overflow technique on the left column. See the the previous and last example here:
https://jsfiddle.net/bhL6sx0g/
To be honest if you're dealing with many columns with dynamic content like this that you don't want wrapping you might want to add a layer of JS on top to do some of your sizing for you else the best option is delegate the actual widths such as a 15/85 split and stick to it.
Edit:
Give width units in vw(viewport width). Notice changes in scss of table(width), first-child( width, min-width) and last-child(width, max-width).
This vw will behaved strangely in jsfiddle, try in .html only.
.table {
display: table;
padding: 0.25rem;
width:90vw;
.row {
display: table-row;
.cell {
display: table-cell;
border: 1px solid gray;
padding: 0.25rem;
&:first-child {
white-space: nowrap;
width:20vw;
min-width:20vw;
}
&:last-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 70vw;
max-width: 70vw;
text-align: right;
}
}
}
}
Previous
By max-width:100% it can stretch to take full size of parent. You need limit it to some percentage or pixels to achieve desired effect.
Removed width:100% as it was useless and changed max-width:200px
&:last-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
/* width: 100%; */
max-width: 200px;
text-align: right;
}
Related
I have a table that displays text fetched from an API using JS and because of that a lot of my cells have different length. I've been trying to create a table with a vertical and horizontal scroll, which I managed to do but I can't seem to figure out how to adjust table row length so that entire text is shown, instead of being cut out. I would like for a cell to have a maximum length and if that length is exceeded the text is shown on the next line. This might not be doable with css only but any solution is welcomed.
Here is a quick look on my issue
Here is part of my CSS for the Match History Table
* {
box-sizing: border-box;
}
.table2 {
position: fixed;
top: 450px;
left: 400px;
border-collapse: collapse;
width: 1001px;
overflow-x: scroll;
display: block;
}
.table2 tbody {
overflow-y: scroll;
overflow-x: hidden;
height: 300px;
}
.table2 td, th {
min-width: 100px;
height: 25px;
border: dashed 1px lightblue;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100px;
}
1st part of your issue is some cells needing longer lengths than others, so you could set the colspan for those longer than the others.
2nd part is by setting a max-width on those columns you can program how they behave when text is too long for the cells width. If you already know which column's cells will need more room, try adding a larger max width for those cells which will affect all cells in that column.
td:nth-child(1) {
width: 100px
}
td:nth-child(2) {
max-width: 400px; /* Comment this line to see the width stretch based on cell's
content */
}
I have included a working example of how you should set colspan and a modified version of your CSS here: https://codepen.io/TxsAdamWest/pen/NWbOwRW
I managed to fix my issue by adding a class with a min-width attribute to the headers that needed their length adjusted and as for the body, since it is generated dynamically using JS I did the same thing using cell.style.minWidth to a specific row.
I am having trouble producing an ellipsis effect when a series of span children overflows its parent container. I have set up the parent container to have all the necessary attributes for an ellipsis (nowrap, display, hidden overflow, and of course text-overflow as ellipsis) but with my current setup, my spans seem to not want to ellipse on an overflow.
...The elements are structured like this
<div class="outer">
<span class="genre">Adventure</span>
<span class="operator">OR</span>
<span class="genre"> Comedy</span>
</div>
...And the corresponding CSS:
.outer {
max-width: 90px;
overflow: hidden;
white-space: nowrap;
display: inline-block;
text-overflow: ellipsis;
}
.operator {
width: 20px;
height: 22px;
float: left;
}
.genre {
float: left;
}
While the desired effect is an ellipsis, what I'm seeing is the overflowed elements wrapping around instead. I was under the impression that any sort of wrapping would be prevented through my display as inline-block, as well as nowrap, but that doesn't seem to be the case here. This seems to be due to floating the elements, but what I've been looking for is a solution that includes these floats in this case. Here is a jsfiddle of my current situation: https://jsfiddle.net/k91wzsq3/2/ - And The screenshot below is the effect I'm looking for.
Any help would be greatly appreciated, thank you in advance!
You don't need to use the float property in this case, it will just mess up your element. You only needed to tell the .outer element that it is going to be inline, just like you did but then by adding the floats it broke everything. You only need this on you CSS
.outer {
max-width: 90px;
overflow: hidden;
white-space: nowrap;
display: inline-block;
text-overflow: ellipsis;
}
Fiddle here https://jsfiddle.net/zgranda/0Ls6fw4j/14/
I have an HTML table, with headings in the first column and data in the later columns. It has to fit into a certain width on page, but the all cells are of variable length. Is there any way I can set the first column to shorten itself:
text-overflow: ellipsis;
white-space: nowrap;
while leaving the other columns at their full width to show all the data? I have created a demonstration here: http://jsfiddle.net/Xsanda/uBrV7/
You can do this, but only if you are willing to set a max-width of the cell. The width can't be completely dynamic and still truncated.
http://jsfiddle.net/uBrV7/2/
td:first-child {
max-width: 30px;
overflow: hidden;
text-overflow: ellipsis;
}
You can use max-width: Fiddle
td:first-child {
text-overflow: ellipsis;
overflow: hidden;
max-width: 100px; /* Change this to fit your needs */
}
What I'm trying to do is to have a line with two parts, and the last part should be always visible while using text-overflow: ellipsis for the first one. Like this :
[[foo][bar] ]
[[lorem i...][bar]]
So I used display: table for the row and display: table-cell for the parts. And it works fine I add table-layout: fixed to the row, but that leaves empty space in the second cell.
Here is the fiddle containing what I tried: http://jsfiddle.net/ybouhjira/2vWKm/1/
It depend on #wrapper width
go change wrapper width to 400px
and add this code
#cell1 {
width: 20%;
}
#cell2 {
width: 99%;
}
http://jsfiddle.net/2vWKm/2/
I would like to setup a horizontal container that holds multiple (smaller) columns within it. I have the following setup:
http://jsfiddle.net/f464W/1/
As you can see, when you resize the window, the .column containers just stack vertically when the width of the window is too small to contain them all.
Shouldn't
overflow-x: hidden
Stop the .column class from being displayed when they run off the side of .container?
Add white-space: no-wrap the .container
http://jsfiddle.net/feitla/f464W/17/
.container {
max-height: 600px;
width: 100%;
margin-top: 100px;
background: red;
padding: 0;
overflow-y: hidden;
overflow-x: hidden;
white-space: nowrap;
}
With overflow-x: hidden will hide what exceeds on the right of your div. But the natural behaviour of the divs are to wrap to the line below when they're out of space, therefore nothing exceeds.
You need to make the divs not wrap. if you add white-space: nowrap to your container CSS they will only stack horizontally.
You can add float: left; to .column and that should give you the desired effect. You'll then have to play with margin if you want to maintain the spacing you had between the columns.