2017-09-23 167 views
0

我想加载它时打印页面,我一直在尝试打印@media print页面CSS,但我不能让它工作,无论我做什么。HTML - 打印页面不检索CSS

我唯一需要做的就是在<th>标签中应用颜色。

HTML代码:

<body onload="window.print();"> 

<div> 
    <h2 class="text-center"><span class="label label-default">Order Form Details</span></h2><br> 
    <table class="table table-hover table-responsive"> 
    <thead> 
     <tr> 
     <th>Name</th> 
     <th>Surname</th> 
     <th>Telephone</th> 
     <th>Postal Code</th> 
     <th>Priority</th> 
     </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>1</td> 
     <td>1</td> 
     <td>1</td> 
     <td>1</td> 
     <td>1</td> 
     </tr> 
    </tbody> 
    </table> 
</div> 

</body> 

CSS代码:

th { 
    background-color: #C8C8C8; 
} 

的jsfiddle here

回答

0

你可以使用@media打印在CSS

.table th { 
    background-color: #C8C8C8; 
    -webkit-print-color-adjust: exact; 
} 

@media print { 
    .table th { background-color: #C8C8C8 !important; } 
} 
+0

是啊,已经试过这个,在'@media print'中加入'!important',但没有,没有解决它 –

+0

加上它的'@media print',而不是'@media print()'我认为 –

+0

打印背景颜色可能会受到浏览器设置的影响。请查看[https://stackoverflow.com/questions/3893986/css-media-print-issues-with-background-color](https://stackoverflow.com/questions/3893986/css-media-print-issues- with-background-color) – jfeferman

0

这取决于浏览器的设置,但大多数浏览器默认不会打印背景颜色(你可以将它做的,但只有在浏览器本身,即谁打开页面的用户),和你不能强制他们默认这样做,因为它是网站代码没有影响的浏​​览器设置。

如果您绝对需要它,您必须将其编码为您放在DIV后面的img标签。