How to transform only one table column into responsive - html-table

I have a table with 3 columns (used to display an album songs). Number, Track Title and Artist. I wanted to keep the column of Number and Artist small, and allow Title to place most of the space of the column. And I wanted to make it responsive for mobile.
I managed to make it right with white-space: nowrap, overflow: auto, and adding a max-width in the table td. This looks nice in a Desktop with a large display, but it seems that in mobile screens the width:100% used in the table does not apply, because the table overflows the limit of the screen.
How can I fix it and display the full table in small screens? Is it only possible with #media?
The work done is here:
table {
width: 100%;
}
table th {
background: #E4E4E4;
line-height: 23px;
padding-top: 6px;
color: #626262;
letter-spacing: 0.05em;
text-transform: uppercase;
font-size: 11px;
border: 1px solid #DBDBDB;
}
table td {
font-size: 13px;
line-height: 22px;
background: #F7F7F7;
border-top: 1px solid #F3F3F3;
white-space: nowrap;
overflow: auto;
max-width: 300px;
}
table td,
table th {
text-align: left;
padding: 3px 20px;
}
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th>Nº</th>
<th class="largura-min">Title</th>
<th>Artist</th>
</tr>
<tr>
<td>1</td>
<td>I need this column placing most of this space / Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String</td>
<td>ARTIST</td>
</tr>
<tr class="even">
<td>2</td>
<td>TITLE 2</td>
<td>I NEED A SMALLER ARTIST SPACE / SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST</td>
</tr>
</tbody>
</table>

When you say that width:100% is ignored, do you mean that the table's width becomes larger than the screen width and a horizontal scrollbar appears?
Give each column a different max-width in px units, in the same proportion of space you want them to take up. I removed a bunch of stuff from your example just to make experimenting easier. Resize the output frame on https://jsfiddle.net/dgrogan/ye2wm4yp/2/
But note that Firefox doesn't seem to support overflow:auto on table cells. If you need to support FF you'll have to figure that out. Didn't test Edge.
Grid might support this use case better than tables.
let reducer = (sum, current) => sum += current;
let cells = Array.from(document.querySelectorAll("tr:first-child td"));
let abs_widths = cells.map(cell => cell.offsetWidth);
let total_width = abs_widths.reduce(reducer);
let pct_widths = abs_widths.map(w => Math.round(1000 * w / total_width) / 1000);
let specified_widths_px = cells.map(cell => parseInt(getComputedStyle(cell).maxWidth));
let total_spec_widths = specified_widths_px.reduce(reducer);
let specified_widths_pct = specified_widths_px.map(px => Math.round(1000 * px / total_spec_widths) / 1000);
log.innerHTML = `${abs_widths}
<br>${pct_widths} <-- current column ratio
<br>${specified_widths_pct} <-- specified column ratio
`;
table {
width: 100%;
}
table td {
font-size: 13px;
line-height: 22px;
white-space: nowrap;
overflow: auto;
padding-right: 0px;
outline: 1px solid lime;
}
td:nth-child(1) {
max-width: 10px;
}
td:nth-child(2) {
max-width: 50px;
}
td:nth-child(3) {
max-width: 35px;
}
<table cellpadding="0" cellspacing="0">
<tr>
<td>Nº</td>
<td class="largura-min">Title</td>
<td>Artist</td>
</tr>
<tr>
<td>1</td>
<td>I need this column placing most of this space / Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String Superlong String</td>
<td>ARTIST</td>
</tr>
<tr class="even">
<td>2</td>
<td>TITLE 2</td>
<td>I NEED A SMALLER ARTIST SPACE / SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST SUPERLONG ARTIST</td>
</tr>
</table>
<div id=log></div>

Related

How to fixed header of the table to the top of the page if table must be scrolable vertically?

I have table which is very long vertically, then it must use overflow: auto hidden.
I have to prepare header of this table as fixed to the top of the page after scroll (prevent to hide header of the table after scrolling).
The problem is when I trying to use position: fixed; top: 100px on the thead or thead tr, this header is losing conection to the table and tbody and width of this is not as tbody (th aren't connected to appropriate td in body).
Also tbody is losing overflow: auto hidden and now has typical width 100% of the page.
SOME CONTENT BEFORE
<div style="overflow: auto;" class="table-responsive">
<table class="products-table">
<thead>
<tr>
<th>First</th>
<th>Second</th>
<th>Third</th>
<th>Fourth</th>
<th>Fifth</th>
<th>Sixth</th>
<th>...</th>
<th>...</th>
<th>...</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>First</td>
<td>Second</td>
<td>Third</td>
<td>Fourth</td>
<td>Fifth</td>
<td>Sixth</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
</div>
SOME CONTENT AFTER
Styling:
.products-table
border-collapse: separate
border-spacing: 0 12px
min-width: 100%
font-size: 15px
border-bottom: 4px solid grey
td,th
padding: 0 10px
max-width: 300px
&:first-of-type
border-radius: 9px 0 0 9px
padding-left: 14px
&:last-of-type
border-radius: 0 9px 9px 0
padding-right: 14px
thead
background-color: grey
color: white
font-weight: 700
th
height: 26px
white-space: nowrap
a
color: white
tbody
tr
padding: 0 14px
td
word-break: break-word
background-color: white
max-width: 300px
&:nth-of-type(odd) td
background-color: #f6f6f6
Help, please...
I have tried yet many options based on similar questions, like for example position sticky.
You may add the following codes into ur css.
thead tr th {
/*Fixed Scroll*/
position: sticky;
top: 0;
z-index: 9999;
}
table {
position: relative;
}

How do you make a long string of characters make a line break to start a second line in a td tag? [duplicate]

This question already has answers here:
Wrap text in <td> tag
(15 answers)
Closed 4 years ago.
A long string of characters contained in a td tag and that string will continue on one line and won't make a line break and I have to scroll towards the right to see the y's on the right side.
How do I make it so that the line containing x will make a line break after filling up 50% of the window so I can see all the x's and y's without scrolling?
<html>
<style>
.table
{
border-width: 1px;
border-style: solid;
border-color: black;
text-align: center;
width: 100%;
overflow-wrap: break-word;
word-wrap: break-word;
}
</style>
<body>
<table border= '1'class = 'table'>
<tr>
<td class = 'left'>Left</td>
<td class = 'right'>Right</td>
</tr>
<tr>
<td>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
</td>
<td>
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
</td>
</tr>
</table>
</body>
Use the CSS property:
word-wrap: break-word;
You can add it as a style attribute inside your TD tag, or create a CSS rule for TD in your stylesheet.
All you have to do is add word-break: break-all; to your <td> tags.

How to reduce table border thickness?

I am drawing one table with border=1 but it looks quite prominent as I am enclosing one image inside it so I want to make it thinner for better look and feel.
<table border=1 cellpadding=50 cellspacing=0><tr><td></td></tr></table>
How can I reduce border thickness?
You can style it like this:
td, th {
border: .1px solid black;
}
JSFIDDLE DEMO
This sample code may help you,
CSS+HTML
table{
border:5px solid blue;
}
caption{
color:wheat;
}
th{
border:2px solid yellow;
background-color:grey;
}
td{
border:3px solid red;
background-color:green;
}
<table border=1 cellpadding=50 cellspacing=0>
<caption>TABLE</caption>
<thead>
<tr><th>H1</th><th>H2</th></tr>
</thead>
<tbody>
<tr><td>1</td> <td>1</td></tr>
<tr><td>1</td> <td>1</td></tr>
</tbody>
<tfoot>
<tr><td colspan="2" >FOOTER</td></tr>
</tfoot>
</table>

pisa html2pdf convert table dont apply height to td

I use pisa xhtml2pdf to convert an html code to pdf.
I have a problem! pisa did not apply some style to my table such as height and vertical-align
<table style='overflow: hidden;' border='1' cellpadding='0' cellspacing='0'
width="300px">
<tr style="background-color : yellow; height:100px;">
<td style="color: red; height:100px; vertical-align: middle;">
A lot of data
</td></tr></table>
but after converting this html to pdf, height of table tds and vertical-align did not correct!
Please help me.
You need to specify a height (height:100px;) to the table too example for you :
<table style='overflow: hidden; height:100px;' border='1' cellpadding='0' cellspacing='0' width="300px">
<tr style="background-color : yellow;height:100px;">
<td style="color: red; height:100px; vertical-align: middle;">
<!-- A lot of data -->
</td>
</tr>
</table>
I managed to get the height of the cell with this hack:
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%" style="border: 1px solid #000"><!-- content goes here --></td>
<td width="0"><br><br><br><br><br></td><!-- special cell to keep the height of the table -->
</tr>
</table>
The second cell is completely invisible and the code above draws a box of 100% in width and the height is defined by amount of <br>s in the second cell or amount of the text in the first cell (whichever comes longer)

Scrollable table with fixed header

I searched for some solutions in PHP/HTML/CSS for this, but nothing worked so far, maybe because in most of those examples were sooo much of code so I got lost in it. Could someone explain to me what I need to do or put some simple sample code here?
Table fixed header using CSS
The simplest would be to position: sticky; your th elements:
.tableFix { /* Scrollable parent element */
position: relative;
overflow: auto;
height: 100px;
}
.tableFix table{
width: 100%;
border-collapse: collapse;
}
.tableFix th,
.tableFix td{
padding: 8px;
text-align: left;
}
.tableFix thead th {
position: sticky; /* Edge, Chrome, FF */
top: 0px;
background: #fff; /* Some background is needed */
}
<div class="tableFix">
<table>
<thead>
<tr><th>H1</th><th>Header 2</th><th>Header 3</th><th>4</th><th>5th Header</th></tr>
</thead>
<tbody>
<tr><td>R1C2</td><td>R1C2</td><td>R1C3</td><td>R1C4</td><td>R1C5</td></tr>
<tr><td>R2C1</td><td>R2C2</td><td>R2C3</td><td>R2C4</td><td>R2C5</td></tr>
<tr><td>R3C1</td><td>R3C2</td><td>R3C3</td><td>R3C4</td><td>R3C5</td></tr>
<tr><td>R4C1</td><td>R4C2</td><td>R4C3</td><td>R4C4</td><td>R4C5</td></tr>
<tr><td>R5C1</td><td>R5C2</td><td>R5C3</td><td>R5C4</td><td>R5C5</td></tr>
<tr><td>R6C1</td><td>R6C2</td><td>R6C3</td><td>R6C4</td><td>R6C5</td></tr>
</tbody>
</table>
</div>
Table fixed header for older browsers
If the browsers you need to support do not encompass the position's sticky value, you can take a look at Fix table head using a bit of Javascript
This code (taken from the link in your comment) is the basic code you need. Next time you need to figure that kind of thing out, just remove segments of code to see what breaks, and leave out everything that doesn't break something that you need.
<html>
<head>
<style>
div.tableContainer {
clear: both;
border: 1px solid #963;
height: 285px; /* html>body tbody.scrollContent height plus 23px for the header */
overflow: auto;
width: 756px /* Remember to leave 16px for the scrollbar! */
}
html>body tbody.scrollContent {
display: block;
height: 262px;
overflow: auto;
width: 100%
}
html>body thead.fixedHeader tr {
display: block
}
html>body thead.fixedHeader th { /* TH 1 */
width: 200px
}
html>body thead.fixedHeader th + th { /* TH 2 */
width: 240px
}
html>body thead.fixedHeader th + th + th { /* TH 3 +16px for scrollbar */
width: 316px
}
html>body tbody.scrollContent td { /* TD 1 */
width: 200px
}
html>body thead.scrollContent td + td { /* TD 2 */
width: 240px
}
html>body thead.scrollContent td + td + td { /* TD 3 +16px for scrollbar */
width: 316px
}
</style>
</head>
<body>
<div id="tableContainer" class="tableContainer">
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="scrollTable">
<thead class="fixedHeader">
<tr class="alternateRow">
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody class="scrollContent">
<tr class="normalRow">
<td>Cell Content 1</td>
<td>Cell Content 2</td>
<td>Cell Content 3</td>
</tr>
<tr class="alternateRow">
<td>More Cell Content 1</td>
<td>More Cell Content 2</td>
<td>More Cell Content 3</td>
</tr>
.........
</tbody>
</table>
</div>
</body>
</html>