Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing html ignores CSS stylesheets

Tags:

html

css

printing

I have a very simple html page with a table element.

The document is styled with css. When I print the page it looks like there are no stylesheets at all. Wrong font, everything is wrong.

What's the problem here? Do I have to create a special css only for printing? In my case it would be the same.. Is that really necessary?

like image 779
Elias Avatar asked Oct 18 '10 09:10

Elias


People also ask

Why my CSS file is not working in HTML?

Make sure that you add the rel attribute to the link tag When you add an external CSS file to your HTML document, you need to add the rel="stylesheet" attribute to the <link> tag to make it work. If you omit the rel attribute from the <link> tag then the style won't be applied to the page.

Can we apply CSS to page for printing?

You can use CSS to change the appearance of your web page when it's printed on a paper. You can specify one font for the screen version and another for the print version.

How do you print using CSS?

Developer Tools. The DevTools ( F12 or Cmd/Ctrl + Shift + I ) can emulate print styles, although page breaks won't be shown. In Chrome, open the Developer Tools and select More Tools, then Rendering from the three-dot icon menu at the top right. Change the Emulate CSS Media to print at the bottom of that panel.

Can I force a page break in HTML printing?

We can add a page break tag with style "page-break-after: always" at the point where we want to introduce the pagebreak in the html page.


3 Answers

Without the link to the page, I can only offer little advice.

  • Make sure the link to the stylesheet in the header is correct. And it should look something like this:

    <link rel="stylesheet" type="text/css" href="/link/to/.css" media="all">

I just noticed that you mentioned print. The all media type will work across web and print. However, if you wanted to explicitly state another stylesheet for print than replace the all with print.

  • Check to see if the style are not being overridden by something else. As CSS is Cascading Style Sheets
  • Get Firebug.
like image 163
Russell Dias Avatar answered Oct 19 '22 08:10

Russell Dias


If your stylesheet link has media="screen", then it will be ignored by print. In that case you can create a separate print stylesheet with media="print" or just change your existing style sheet to media="all".

like image 32
Magnar Avatar answered Oct 19 '22 07:10

Magnar


The media attribute is used to determine the stylesheet purpose. You need to make sure that it is set to all (for all purposes) or print (only used when printing).

like image 1
jwueller Avatar answered Oct 19 '22 07:10

jwueller