Twitter Bootstrap Prints In XS

2020-08-26 11:19发布

问题:

I am using Twitter Bootstrap 3 but when I go to print my webpage (via File > Print) it's printing the page in XS (Mobile view), how do I force it to use and print in MD (Desktop view).

It only works fine in IE versions 9 and below. Every other browser and IE 10+ have the issue.

回答1:

.col-xs-X is outside media queries that is why it prints. All columns are the same size: 100/12 = 8.333333% so .col-lg-6 and .col-sm-6 and .col-xs-6 and .col-md-6 are all 50% of the .container/.container-fluid.

To get your printed page to print the columns, you would add a print media query:

@media print { 
    .container {
        max-width: none!important;
        width: 100%!important;
        padding-left: 0;
        padding-right: 0;
    }
    .row {
        margin-left: -1%;
        margin-left: -1%;
    }
    [class*="col-"] {
        float: left;
        padding-left: 1%;
        padding-right: 1%;
    }
    .col-sm-12,
    .col-md-12,
    .col-lg-12 {
        width: 100%
    }
    .col-sm-11,
    .col-md-11,
    .col-lg-11 {
        width: 91.66666667%
    }
    .col-sm-10,
    .col-md-10,
    .col-lg-10 {
        width: 83.33333333%
    }
    .col-sm-9,
    .col-md-9,
    .col-lg-9 {
        width: 75%
    }
    .col-sm-8,
    .col-md-8,
    .col-lg-8 {
        width: 66.66666667%
    }
    .col-sm-7,
    .col-md-7,
    .col-lg-7 {
        width: 58.33333333%
    }
    .col-sm-6,
    .col-md-6,
    .col-lg-6 {
        width: 50%
    }
    .col-sm-5,
    .col-md-5,
    .col-lg-5 {
        width: 41.66666667%
    }
    .col-sm-4,
    .col-md-4,
    .col-lg-4 {
        width: 33.33333333%
    }
    .col-sm-3,
    .col-md-3,
    .col-lg-3 {
        width: 25%
    }
    .col-sm-2,
    .col-md-2,
    .col-lg-2 {
        width: 16.66666667%
    }
    .col-sm-1,
    .col-md-1,
    .col-lg-1 {
        width: 8.33333333%
    }
}


回答2:

To fix it I did add the following to my CSS

@media print {
  body {
    margin: 0;
    padding: 0 !important;
    min-width: 768px;
  }
  .container {
    width: auto;
    min-width: 750px;
  }
}

This seems to have worked great for me.



回答3:

Please read http://getbootstrap.com/getting-started/#support-printing

The gist of it is that browsers handle media queries incorrectly/weirdly when printing. They don't use the same CSS viewport width as the physical paper width.

Workaround options include:

  • Embrace the extra-small grid and make sure your page looks acceptable under it.
  • Customize the values of the @screen-*Less variables so that your printer paper is considered larger than extra-small.
  • Add custom media queries to change the grid size breakpoints for print media only.


回答4:

Printing is tricky with most browsers - the issue is that depending on browser, default paper size and it's orientation (portrait or landscape) and the current tide in Sydney a page will print in 72ppi, 96ppi or 144ppi.

In Bootstrap the media query establishes a pixel width band... e.g. "xs" is between 544px-767px, so depending on the (printed) page width your site might revert to the "xs" or the "sm" width variants when printing.

The solutions listed above using @media print {} will do the trick in most cases - but the fact remains that the site needs to be coded with printing in consideration.

I have an example at vinorodrigues/print-ready that tries to account for some of these issues and considerations.