How can I have different CSS when I print or print preview?
This can be achieved with a separate print stylesheet. The media
attribute is the key:
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
How to display print preview as same as the webpage using css?
Short answer is you cannot trigger Print Preview
because it's the local OS window, what you can do is before your user prints you can create a separate HTML document and use print specific stylesheet(which you are already using) for that file like this
<link rel="stylesheet" href="#" type="text/css" media="print" />
And if you want to control page break yourself use this
@media print {
whatever {
page-break-after:always
}
}
CSS style not showing on print preview
This is what you need. Some sites have their own media print so you need to add -webkit-print-color-adjust: exact; to your media print and !important it to make sure it overrides any other media print.
http://codepen.io/anon/pen/JRXEko
<div id="draw">
<button id="click">Click me</button>
</div>
<hr />
<button onclick="window.print();">Print</button>
.highlited {
color: red;
}
@media print {
.highlited {
color: red !important;
-webkit-print-color-adjust: exact;
}
}
$('#draw').on('click', 'p', function () {
if($(this).hasClass("highlited")) {
$(this).removeClass("highlited");
} else {
$(this).addClass("highlited");
}
});
$('#click').click(function () {
var html = "";
html += '<p>I want to be red...</p>';
html += '<p>Me too...</p>';
html += '<p>Ok... Me too...</p>';
$('#draw').html(html);
});
Page and print preview do not match up visually
Use css @media
and create different styles for when you want something to look/render differently when it's printed
.my__class {
font-size: 20px;
}
@media print {
.my__class {
font-size: 80px; // when you print .my__class will be 80px, but 20px on screen
}
}
Related Topics
Make Scrollbars Only Visible When a Div Is Hovered Over
Why Use an HTML5 Semantic Tag Instead of Div
Apply CSS Style to Child Elements
How to Indicate Long Text into a Smaller Fixed Column with CSS
In Outlook HTML Email, Float Does Not Work
How to Override Global CSS in a CSS Module File
Keep Background Image Fixed During Scroll Using CSS
Is There a Functional Difference Between > *:First-Child and > :First-Child
How to Make a <Svg> Element Expand or Contract to Its Parent Container
How to Add a Margin to a Table Row <Tr>
How to Give One CSS Class Priority Over Another
How to Rotate Text Left 90 Degree and Cell Size Is Adjusted According to Text in HTML
How to Set the Height of a Div Based on a Percentage-Based Width
Css: Left, Center, & Right Align Text on Same Line
How to Efficiently Load Google Fonts in Nuxt
Issue When Centering Vertically with Flexbox When Heights Are Unknown