简体   繁体   English

打印或转换为PDF时不会显示CSS边框

[英]CSS border won't display when printing or converting to PDF

I would like to print this HTML table with the elements displayed with rounded borders, but as soon as I print the page the CSS class seems not to be displayed anymore. 我想打印此HTML表,并以圆角边框显示元素,但是一旦我打印页面,CSS类似乎就不再显示。 Can anyone help? 有人可以帮忙吗?

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
                .hashtag {
                    display: inline-block;
                    color: white;
                    font-size: 20px;
                    background-color: rgba(46, 200, 40, 0.5);
                    margin-left: 1px;
                    margin-right: 1px;
                    margin-top: 1px;
                    margin-bottom: 1px;
                    padding: 0.0em 0.5em;
                    border-radius: 1em;
                    text-indent: 0;
                    text-align: center;
                }
        </style>
    </head>
    <body>

    <tr>
        <td>
            <p class="hashtag" align="center">CSS</p>
            <p class="hashtag" align="center">WON'T</p>
            <p class="hashtag" align="center">PRINT</p>
        </td>
    </tr>
    </body>
    </html>

You are using white text on a dark background. 您在深色背景上使用白色文本。 But most browsers don't print background images or background colors by default, which will result in white text on no/white background in your case, ie it will remain invisible. 但是默认情况下,大多数浏览器都不打印背景图像或背景色,在您的情况下,这将导致白色文本显示为无/白色背景,即它将保持不可见。

It is possible to print background colors, but that's a browser preference/setting that can only be set/changed by the user, not via CSS or any other code in the website. 可以打印背景颜色,但是这是一个浏览器偏好/设置,只能设置/更改用户,而不是通过CSS或网站的任何其他代码。

You are trying to print white text on a colored background, but most browsers will not print backgrounds by default (or if they do, then it is because the user has changed a setting to enable it). 您正在尝试在彩色背景上打印白色文本,但是大多数浏览器默认不会打印背景(或者,如果这样做,则是因为用户已更改设置以启用背景)。

You can prepare for this using a @media print { ... } addition to your stylesheet, which changes the applied style so it will print more nicely. 您可以使用样式表中的@media print { ... }来为此做准备,该样式表会更改应用的样式,以便更好地打印。 For an example see the snippet below: 有关示例,请参见下面的代码段:

 .hashtag { display: inline-block; color: white; font-size: 20px; background-color: rgba(46, 200, 40, 0.5); margin-left: 1px; margin-right: 1px; margin-top: 1px; margin-bottom: 1px; padding: 0.0em 0.5em; border-radius: 1em; text-indent: 0; text-align: center; } @media print { .hashtag { color: black; background-color: white; border: 1px solid black; } #btnPrint { display: none } } 
 <p class="hashtag" align="center">CSS</p> <p class="hashtag" align="center">WILL</p> <p class="hashtag" align="center">PRINT (B/W)</p> <button id="btnPrint" onclick="window.print()">Print</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM