text-overflow: ellipsis; in table cell - html

I have a page with a table, which is updated periodically. The table has a heading, followed by a number of rows. Each row has 3 fields: Player position, Player name (also a link to the player's profile), and the player's time. The table takes up the entire page width.
The html looks like this:
<table id="raceTable">
<tbody>
<tr><th colspan="3">3 Players</th></tr>
<tr><td>1st</td><td><a>Link to profile of player A</a></td><td>00:22:12</td></tr>
<tr><td>2st</td><td><a>Link to profile of player B</a></td><td>00:23:12</td></tr>
<tr><td>3st</td><td><a>Link to profile of player C</a></td><td>00:24:15</td></tr>
</tbody>
</table>
Now, I want to make the table scalable. If the user changes the width of the window, the table should be resized, the text in the middle column should be clipped when it doesn't fit.
This is the CSS I've tried:
body{
overflow: hidden;
}
table {
width:100%;
line-height: 50px;
border-collapse: collapse;
}
th {
height: 50px;
width: 100%;
background-color: magenta;
}
td {
height: 50px;
}
#raceTable {
width: 100%;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(1) {
padding: 10px;
background-color: red;
float: left;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(2) {
width: 100%;
background-color: gray;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(2) > a{
display: block;
background-color: white;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(3) {
padding: 10px;
background: green;
float: right;
}
And I have a JSFiddle of it all here: http://jsfiddle.net/Gikkman/5wpts61p/17/
My question is, how do I make text-overflow: ellipsis; to work for the link text in the middle column of the table? I cannot change how the HTML of the site is structured, I can only apply CSS. Is this possible?

In table, text-overflow: ellipsis; only works with fixed table layout, normally you can just apply:
#raceTable {
table-layout: fixed;
width: 100%;
}
But it looks like you also want to have dynamic width for the columns. In that case you can wrap the <a> with a <div> to create a CSS fixed table layout structure, then apply the text overflow on it.
#raceTable td:nth-child(2) div {
display: table;
table-layout: fixed;
width: 100%;
}
#raceTable td:nth-child(2) a {
display: table-cell;
background-color: white;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
#raceTable {
line-height: 50px;
border-collapse: collapse;
width: 100%;
}
#raceTable th {
height: 50px;
background-color: magenta;
}
#raceTable td {
height: 50px;
}
#raceTable td:nth-child(1) {
padding: 10px;
background-color: red;
/* float: left; */
}
#raceTable td:nth-child(2) {
width: 100%;
background-color: gray;
}
#raceTable td:nth-child(2) div {
display: table;
table-layout: fixed;
width: 100%;
}
#raceTable td:nth-child(2) a {
display: table-cell;
background-color: white;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
#raceTable td:nth-child(3) {
padding: 10px;
background: green;
/* float: right; */
}
<table id="raceTable">
<thead>
<tr>
<th colspan="3">3 Players</th>
</tr>
</thead>
<tbody>
<tr>
<td>1st</td>
<td><div><a>Link to profile of player A, Link to profile of player A, Link to profile of player A</a></div></td>
<td>00:22:12</td>
</tr>
<tr>
<td>2st</td>
<td><div><a>Link to profile of player B</a></div></td>
<td>00:23:12</td>
</tr>
<tr>
<td>3st</td>
<td><div><a>Link to profile of player C</a></div></td>
<td>00:24:15</td>
</tr>
</tbody>
</table>

after reconsidering my comment, I would still advise to style your table with table-layout:fixed; and reset default display on a as you did, but build it with a little more table elements.
https://www.w3.org/wiki/HTML/Elements/colgroup
https://www.w3.org/WAI/UA/TS/html401/cp1001/1001-COL-COLGROUP.html
table {
width:100%;
table-layout:fixed;
line-height:50px;
}
thead {background:green;}
td {width:100%;}
col {background:gray;}
col:first-child, col:last-child {
width:4em;
background:red;
}
col:last-child {
background:cyan;
}
a { display:block;
background-color: white;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
width:100%;}
<table id="raceTable">
<colgroup>
<col/>
<col/>
<col/>
</col>
<thead>
<tr>
<th colspan="3">3 Players</th>
</tr>
</thead>
<tbody>
<tr>
<td>1st</td>
<td><a>Link to profile of player A</a></td>
<td>00:22:12</td>
</tr>
<tr>
<td>2st</td>
<td><a>Link to profile of player B</a></td>
<td>00:23:12</td>
</tr>
<tr>
<td>3st</td>
<td><a>Link to profile of player C Link to profile of player C Link to profile of player C</a></td>
<td>00:24:15</td>
</tr>
</tbody>
</table>
https://jsfiddle.net/bn2trf2o
What happens here is the colspan attribute on th and table-layout:fixed; that shares evenly the columns.
To avoid this confusion, you can use the colgroup and col tags to init each width for each columns. Size first and last , the middle will use all space left.You can use them also to set a background defaut color for each columns. it will be hidden as soon as you set a bg on any other elements(like thead does in the snippet)
edit
from your structure , a display reset on trs could do :
table,
tr {
width: 100%;
table-layout: fixed;
line-height: 50px;
border-spacing: 0;
}
tr {
display: table;
}
th {
background: magenta
}
td {
background: gray;
}
td:first-child,
td:last-child {
width: 30px;
background: red;
}
td:last-child {
background: cyan;
width: 100px;
}
a {
display: block;
background-color: white;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
<table id="raceTable">
<tbody>
<tr>
<th colspan="3">3 Players</th>
</tr>
<tr>
<td>1st</td>
<td><a>Link to profile of player A</a></td>
<td>00:22:12</td>
</tr>
<tr>
<td>2st</td>
<td><a>Link to profile of player B</a></td>
<td>00:23:12</td>
</tr>
<tr>
<td>3st</td>
<td><a>Link to profile of player C Link to profile of player C Link to profile of player C</a></td>
<td>00:24:15</td>
</tr>
</tbody>
</table>
https://jsfiddle.net/bn2trf2o/1/

Related

Cannot change the HTML table's column sizes despite of giving width

I have a table which is set to ?
I need to change the size of the columns. These are the column names and I have set width to 30 and 70. but it doesn't change. It is now displaying as 50:50.
table {
width: 100%;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: left;
align-content: center;
width: 25px;
}
th {
background-color: #eeeeee;
}
td {
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
height: 38px;
align-content: center;
}
<table height="100">
<tr>
<th width="30">Control Name</th>
<th width="70">Image</th>
</tr>
</table>
You can make classes and set them on your cells. You can probably name them better then I did in my example, but that is just a hint on how you can do it.
table {
width: 100%;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: left;
align-content: center;
width: 25px;
}
th {
background-color: #eeeeee;
}
td {
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
height: 38px;
align-content: center;
}
.cell-width-30 {
width: 30%;
}
.cell-width-70 {
width: 70%;
}
<table height="100">
<tr>
<th class="cell-width-30">Control Name</th>
<th class="cell-width-70">Image</th>
</tr>
</table>

Table: Minimize column width (multiple colspans)

I have this:
and want this (second column using less space):
Is there a way without introducing absolute values or fixed columns? I don't mind any other hacks like wrappers or invisible columns as long as the content sizes dynamically.
HTML
table {
border-collapse: collapse;
height: 100%;
background-color: blue;
white-space: nowrap;
}
.wrapper {
width: 40%;
font-family: avenir;
height: 100vh;
}
td > div {
color: white;
background-color: darkblue;
padding: 5px;
display: inline-block;
}
td {
padding: 0px;
border: 1px dotted white;
}
.w1px {
width: 1px;
}
.h-100 {
height: 100%;
}
<div class="wrapper">
<table>
<tr>
<td class="w1px"><div>Item1</div></td>
<td><div>Item2</div></td>
<td><div>Item3</div></td>
</tr>
<tr>
<td colspan="3"><div>Item Spanning All Three Above</div></td>
</tr>
<tr>
<td colspan="2"><div>Item Spanning Two</div></td>
</tr>
<tr class="h-100">
</tr>
</table>
</div>

multiple tables with the same TD width

To format these 2 tables I have a css sheet. The top table is a filter/sort selection. The second is a scrollable data table.
div#scrollTableContainer {
width: auto;
margin: 20px; /* just for presentation purposes */
border: 1px solid black;
}
#tHeadContainer {
background: #CC3600;
color: black;
font-weight: bold;
}
#tBodyContainer {
height: 750px;
overflow-y: scroll;
}
td:first-child {
min-width: 5%; /* EDIT */
max-width: 5%;
border-left:0;
}
td:first-child + td {
min-width: 4%;
max-width: 4%;
}
td:first-child + td + td {
min-width: 4%;
max-width: 4%;
}
<div id="scrollTableContainer">
<div id="tHeadContainer">
<table border="1" align="center" width="100%" cellpadding="0" cellspacing="0">
<tr>
<th bgcolor="<%=bgc0%>">USED</th>
<th bgcolor="<%=bgc0%>">STATUS</th>
</tr>
</table>
</div>
<div id="tBodyContainer">
<table border="1" align="center" id="tBody" class="TableData">
<td> stuff </td>
<td> More stuff </td>
</table>
</div>
</div>
Neither of the 2 tables are aligning column wise so that the Title/Header columns do not match the tables columns below. It also shows almost the same in chrome/IE, but firefox is a complete shamble.
Im at a loss to get this right guys, any help will be appreciated.
div#scrollTableContainer {
width: auto;
margin: 20px;
/* just for presentation purposes */
}
#tHeadContainer {
background: #CC3600;
color: black;
font-weight: bold;
}
#tBodyContainer {
height: 750px;
}
th:first-child, td:first-child {
min-width: 15%;
max-width: 15%;
width: 15%;
}
td:first-child+td {
min-width: 4%;
max-width: 4%;
}
td:first-child+td+td {
min-width: 4%;
max-width: 4%;
}
<div id="scrollTableContainer">
<div id="tHeadContainer">
<table border="1" align="center" width="100%" cellpadding="0" cellspacing="0">
<tr>
<th bgcolor="<%=bgc0%>">USED</th>
<th bgcolor="<%=bgc0%>">STATUS</th>
</tr>
</table>
</div>
<div id="tBodyContainer">
<table border="1" align="center" width="100%" cellpadding="0" cellspacing="0">
<td> stuff </td>
<td> More stuff </td>
</table>
</div>
</div>
You want to align the columns of both table vertically right?
If that is so, I have a solution for you
I have changed your css and u missed tr in second table.
https://jsfiddle.net/9ccwkoav/
div#scrollTableContainer {
width: auto;
margin: 20px; /* just for presentation purposes */
border: 1px solid black;
}
#tHeadContainer {
background: #CC3600;
color: black;
font-weight: bold;
}
#tHeadContainer th{
width:50%;
}
#tBodyContainer {
height: 750px;
overflow-y: auto;
}
.TableData{
width:100%;
}
.TableData td{
width:50%;
}
This should do the trick. Notice that there will be a little margin because of the scroll bar of the second table, but you can easily change it by adding a tab in the first array with the width of the scrollbar. Hope it will help
#scrollTableContainer {
width : 96%;
margin : auto;
border : 1px solid #ccc;
}
#tHeadContainer {
background-color : green;
width : 100%;
}
#tBodyContainer {
width : 100%;
height : 750px;
overflow-y : scroll;
}
#tHeadContainer table, #tBodyContainer table {
border-collapse : collapse;
width : 100%;
}
td, th {
border : 1px solid black;
}
tr {
width : 100%;
}
th:first-child, td:first-child {
min-width : 15%;
max-width : 15%;
width : 15%; /* Tweak this value to change the first column width */
}
<div id="scrollTableContainer">
<div id="tHeadContainer">
<table>
<tr>
<th>USED</th>
<th>STATUS</th>
</tr>
</table>
</div>
<div id="tBodyContainer">
<table>
<tr>
<td>Stuff</td>
<td>Some other stuff</td>
</tr>
</table>
</div>
</div>

How can I make css of an overflowing div in a table cell important?

I am a newbie to css.
I have two td in a row of which first has overflow visible. The contents in the first div are overlapping with the second td as in the example below. I wish to make the div css properties like cursor: pointer over the second td. Thus, the div css properties should be on top of the second td.
Please help me with this.
https://jsfiddle.net/jt00uk3e/
Thanks.
table {
width: 0px;
table-layout: fixed;
}
td {
width:50px;
height:50px;
overflow: visible;
}
#td1 {
background-color: red;
}
#td2 {
background-color: green;
}
#div2 {
width: 200px;
height: 20px;
background-color: blue;
cursor: pointer;
}
<table>
<tr>
<td id="td2"><div id="div2"></div></td>
<td id="td1"></td>
</tr>
</table>
Add z-index to div
#div2 {
width: 200px;
height: 20px;
background-color: blue !important;
cursor: pointer;
position:relative;
z-index:
https://jsfiddle.net/jt00uk3e/6/
try this:
table {
width: 0px;
table-layout: fixed;
}
td {
width:50px;
height:50px;
overflow: visible;
}
#td1 {
background-color: red;
}
#td2 {
background-color: green;
}
#div2 {
position:relative;
z-index:1;
width: 200px;
height: 20px;
background-color: blue;
cursor: pointer;
}
<table>
<tr>
<td id="td2"><div id="div2"></div></td>
<td id="td1"></td>
</tr>
</table>

How to correct an accidental "table overflow"?

I am currently coding an accessibility page, but I am visually impaired. I've been advised that the table on this page:
http://www.accessibilityagent.com/legal/
is overlapping the adjacent content. Below is the CSS code being used. Could someone please help adjust this code to not overlap the page in the link above:
<div>
<style type="text/css">
body {
color: purple;
background-color: #d8da3d
}
table.center {
margin-left:auto;
margin-right:auto;
}
body {
text-align:center;
}
table {
width: 50%;
}
table, th, td {
border: 20px solid black;
margin: auto;
padding: 5px;
}
td.wrappable,
table.data_table td.wrappable {
white-space: normal;
}
</style>
Try setting this CSS for the table:
table-layout: fixed;
width: 100%;
word-wrap: break-word;
you need to fit the table, so set table-layout:fixed , now you need to have it fit as large as possible so change width to 100% in table, then you need to fit the links in the cell, so break the words, using word-wrap:break-word in .center a
body {
background-color: #d8da3d;
color: purple;
}
table.center {
margin-left: auto;
margin-right: auto;
table-layout: fixed;
width: 100%;
}
table,
th,
td {
border: 20px solid black;
padding: 5px;
}
.center a {
display: block;
word-wrap: break-word;
}
<table class="center">
<thead>
<tr>
<th scope="col">Year</th>
<th scope="col">Plaintiff</th>
<th scope="col">Defendant</th>
<th scope="col">Link to More Info</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row">2015</td>
<td>US DOJ</td>
<td>YAKIMA COUNTY</td>
<td>http://www.ada.gov/yakima…
</td>
</tr>
</tbody>
</table>