HTML browser print no respect css style - html

I have a table where I fixed the columns and the font to the text. The table is optimized by bootstrap.
I try to print the table through the print menu of the browser and it does not respect the style from css the table that is generated on the sheet
Code for js:
function myFunction() {
var css = '#page { size: landscape;} }',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
style.media = 'print';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
window.print();
}
myFunction();
Code css:
#media print;
.table th:nth-child(1),
.table td:nth-child(1) {
width: 5px;
}
.table th:nth-child(2),
.table td:nth-child(2) {
width: 5px;
}
.table th:nth-child(3),
.table td:nth-child(3) {
width: 5px;
}
.table th:nth-child(4),
.table td:nth-child(4) {
width: 5px;
}
.table th:nth-child(5),
.table td:nth-child(5) {
width: 5px;
}
.table th:nth-child(6),
.table td:nth-child(6) {
width: 5px;
}
.table th:nth-child(7),
.table td:nth-child(7) {
width: 5px;
}
.table th:nth-child(8),
.table td:nth-child(8) {
width: 5px;
}
.table th:nth-child(9),
.table td:nth-child(9) {
width: 5px;
}
.table th:nth-child(10),
.table td:nth-child(10) {
width: 5px;
}
.table th:nth-child(11),
.table td:nth-child(11) {
width: 5px;
}
.table th:nth-child(12),
.table td:nth-child(12) {
font-size: 20px;
width: 120px;
}
.table th:nth-child(13),
.table td:nth-child(13) {
font-size: 10px;
width: 5px;
}
.table th:nth-child(14),
.table td:nth-child(14) {
font-size: 10px;
width: 5px;
}
.table th:nth-child(15),
.table td:nth-child(15) {
font-size: 10px;
width: 5px;
}
.table th:nth-child(16),
.table td:nth-child(16) {
font-size: 10px;
width: 5px;
}
I would like to respect the css style as in the html page and to generate the same thing on the sheet, thank you!

The browser (by default) never prints the background color. You would need to enable that option in your browser.
If you are using a print-specific stylesheet, make sure it includes media="screen, print":
<link rel="stylesheet" type="text/css" href="print.css" media="screen, print" />
Also, remember to enable the printing settings in your browser:
-webkit-print-color-adjust: exact;

Related

Can I get one table to display differently to others on a responsive view?

I'm building a website in wordpress, using the 'Rookie' theme, and have installed the sportpress plugin, as well as an accordion plugin.
I'd like to display a table in accordions, but also make it display properly when on a mobile device. This is the webpage > http://wnu.054.myftpupload.com/fixtures-and-results/
I've sorted this for the majority of tables (fixtures for the month) using the following CSS:
.table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
#media only screen and (max-width: 40em) {
thead th {
display: none;
}
td, th {
display: block;
}
td[data-th]:before {
content: attr(data-th);
}
}
table {
border-spacing: 0;
}
td,
th {
padding: 0.5em;
}
th {
font-weight: bold;
text-align: left;
}
thead th {
background-color: #999;
color: white;
}
td > div {
float: right;
}
#media screen and (max-width: 500px) {
thead th {
display: none;
}
thead th:first-child:after {
content: "'s";
}
td, th {
display: block;
width: 100% !important;
}
td[data-th]:before {
content: attr(data-th);
border-radius: 10px 0px 0px 10px;
font-weight: bold;
min-width: 10rem;
display: inline-block;
}
The problem I've got is that I don't have access to edit the tables in Sportspress so I can't edit the table data on the Player stats table so that it includes the 'data-th="heading"' - meaning the headers don't display.
Without the CSS above, the sportspress table displays OK (not perfect, but not bad either), I was wondering if I can set it so that the sportspress table ignores the CSS I've set up (on the responsive view) or if there is a better way to do this? If possible, the sportpress table would display the headers, but I think I'd have to upgrade from the free version?

Setting width for columns in a table when the number of columns is variable

I am working on an angular 8 project at work. I have a page which has a View Only mode and an Edit mode. This page has a table which displays a certain type of records. I am supposed to add a column of checkboxes as the first column in the table when Edit mode is enabled. As per the current code the width of each column was set like below
.data-table tr th:nth-child(1) {
width: 20%;
}
.data-table tr th:nth-child(2) {
width: 10%;
}
.data-table tr th:nth-child(3) {
width: 16%;
}
.data-table tr th:nth-child(4) {
width: 18%;
}
.data-table tr th:nth-child(5) {
width: 18%;
}
.data-table tr th:nth-child(6) {
width: 15%;
}
.data-table tr th:nth-child(7) {
width: 5%;
}
So when my checkboxes appear in Edit mode, the formatting gets messed up, because now each existing column is pushed ahead by a place and gets assigned wrong width. Other than adding id for each th tag and then specifying the css for each, I can't think of anything else. Please suggest any other possible way to do this.
From My Comment
Add the EDIT column so it always exists but then hide that column with CSS display:none; when not on the edit page.
View Page:
<table class='without-edit'>
<tr>
<td>Edit</td>
<td>Hosehead</td>
<td>Trapdoor</td>
<td>Tartarse</td>
<td>Flippant</td>
<td>Crappery</td>
<td>Scottish</td>
<td>Dorkeley</td>
</tr>
</table>
Edit Page:
<table class='with-edit'>
<tr>
<td>Edit</td>
<td>Hosehead</td>
<td>Trapdoor</td>
<td>Tartarse</td>
<td>Flippant</td>
<td>Crappery</td>
<td>Scottish</td>
<td>Dorkeley</td>
</tr>
</table>
CSS:
.without-edit tr td:first-of-type {
display:none;
}
Instead of setting width from scss file use attribute binding like below
<th [style.width]="percentage + '%'"></th>
here percentage may be a number depending on your edit condition
or else try like this
add a class to your table by using ngClass directive like
<table [ngClass]="{ 'with-edit': isEdit , 'without-edit': !isEdit }"></table>
.data-table .with-edit tr th:nth-child(1) {
width: 20%;
}
.data-table .with-edit tr th:nth-child(2) {
width: 10%;
}
.data-table .with-edit tr th:nth-child(3) {
width: 16%;
}
.data-table .with-edit tr th:nth-child(4) {
width: 18%;
}
.data-table .with-edit tr th:nth-child(5) {
width: 18%;
}
.data-table .with-edit tr th:nth-child(6) {
width: 15%;
}
.data-table .with-edit tr th:nth-child(7) {
width: 5%;
}
.data-table .without-edit tr th:nth-child(1) {
width: 20%;
}
.data-table .without-edit tr th:nth-child(2) {
width: 10%;
}
.data-table .without-edit tr th:nth-child(3) {
width: 16%;
}
.data-table .without-edit tr th:nth-child(4) {
width: 18%;
}
.data-table .without-edit tr th:nth-child(5) {
width: 18%;
}
.data-table .without-edit tr th:nth-child(6) {
width: 15%;
}
.data-table .without-edit tr th:nth-child(7) {
width: 5%;
}
You could add the Edit column header as td instead of th then skip td by changing your css to nth-of-type:
<td>Edit</td><th>Col 1</th>
css:
.data-table tr th:nth-of-type(1) {
width: 20%;
}
.data-table tr th:nth-of-type(2) {
width: 10%;
}
Then amend your css to add header styles to that td also if needed.

fixing Header of table and making table body scrollale

I am working in Angular Project but stuck in a design Issue
I am want to scroll the body of the table making body header fixed
I tried to do the at but not able to scroll just table body
My code and design is ready I have put it in below link -
https://stackblitz.com/edit/angular-x8c89j?file=src%2Fapp%2Fapp.component.html
Please update your table style as below
table {
width: 100%;
}
table, td {
border-collapse: collapse;
border: 1px solid #000;
}
thead {
display: table;
width: calc(100% - 17px);
}
tbody {
display: block;
max-height: 200px;
overflow-y: scroll;
}
th, td {
padding: 5px;
word-break: break-all;
}
tr {
display: table;
width: 100%;
box-sizing: border-box;
}
td {
text-align: center;
border-bottom: none;
border-left: none;
}
tr th:first-child,
tr td:first-child {
width: 100px;
}

Tfoot width misaligned when fixed to bottom in a responsive table

I want to fix the tfoot to the bottom of the page with it being as responsive as the rows of the table.
My code: https://codepen.io/adu8/pen/MWWXqGY
The problem I face now is once I add the class="fixed-bottom" to the tr of tfoot, it goes to bottom and gets misaligned.
I tried fixing it with having a workaround by adding this css:
tfoot td {
display: inline-block;
width: calc((100%/8) - 10px);
}
As in this codepen: https://codepen.io/adu8/pen/dyyKqOv. However, it's still not able to be as responsive.
I would appreciate if someone helps fixing this.
Please use this CSS
body {
background-color: midnightblue;
color: white;
}
table {
max-width: 100%;
font-size: 2vw;
}
thead, tr, tfoot {
text-align: center;
background-color: midnightblue;
}
.table-striped>tbody>tr:nth-child(odd)>td,
.table-striped>tbody>tr:nth-child(odd)>th {
background-color: darkslateblue;
}
.table-hover tbody tr:hover > th {
/*background-color: #06765d;*/
background-color: #009999;
}
.table td {
padding: 0.05rem;
margin: 0px;
}
.fixed-bottom {
width: 100%;
}
table tr {
display: flex; display: -webkit-flex;
width: 100%;
}
tr th,
tr td {
width: calc(100% / 8);
}
tr th:first-child,
tr td:first-child {
width: 22%;
flex-shrink: 0;
-webkit-flex-shrink: 0;
}

Problems with the styles print unbreakable elements

I have this file with my styles for print a pdf:
#media print {
::-webkit-scrollbar {
display: none;
}
body {
background-color: white !important;
overflow-y: hidden !important;
}
.main.container {
background-color: #ffffff !important;
> .menu {
display: none;
}
> .container {
overflow-y: visible !important;
}
> .content {
overflow: visible !important;
}
}
}
.unbreakable {
display:inline;
width: 100%;
background-color: lightgreen;
}
.unbreakable:after {
content: '';
display:block;
height:0;
visibility: hidden;
}
.board-print {
background-color: #FFFFFF !important;
padding: 15px;
overflow: visible !important;
font-family: #font-family;
.board-header {
font-weight: normal;
}
.board-body {
height: auto !important;
background-color: #FFFFFF;
width: 100%;
.table-wrapper {
background-color: lightcyan;
width: 100%;
}
.widget-print {
height: auto !important;
margin-bottom: 30px !important;
}
.widget-header {
font-family: #font-family;
margin-bottom: 5px;
font-weight: 300;
font-size: 1.28rem;
color: #4D4D4D;
}
.reactive-table {
page-break-after: auto !important;
overflow: visible !important;
height: auto !important;
table {
th, td{
font-weight: normal;
font-family: #font-family;
}
.table-up-icon, .table-down-icon, .filter.padded.icon {
display: none;
}
}
}
.note-widget .mce-container{
border: none !important;
}
.ruler {
display: none;
}
.chart {
width: 95%;
height: 450px;
}
}
}
the .unbreakable I use it for print only charts and works fine but when I added .table-wrapper for print tables all is broken.
this is my jade:
template(name="widgetsShowPrint")
.widget-print
+if isConfigured
+if isChart
.unbreakable
h3.widget-header #{title}
+UI.dynamic template=type data=widgetData
+else
table.table-wrapper
tbody
tr
td
h3.widget-header #{title}
+UI.dynamic template=type data=widgetData
there I have a conditional for uses .unbreakable or the table wrapper.
I tried to print a PDF the size of the page is A4 but it doesn't work. Someone could help me, I am not expert in CSS.
I attached a image with the result.
This should work :
#media print {
.unbreakable {
page-break-inside: avoid;
}
}
Check the browser compatibility
the solution was:
template(name="widgetsShowPrint")
.widget-print
+if isConfigured
table.table-wrapper
tbody: tr: td(class="{{type}}-container")
h3.widget-header #{title}
+UI.dynamic template=type data=widgetData
I added a type td(class="{{type}}-container")
and in less file I added the size for any td:
.board-body {
height: auto !important;
background-color: #FFFFFF;
width: 100%;
.table-wrapper {
width: 100%;
td.chart-container {
height: #mid-page-size;
}
}
and I solve my problem.