Bootstrap grid for printing

前端 未结 11 2165
长情又很酷
长情又很酷 2020-12-23 11:25

I would like to design a report page with a different layout for printing to mobile. I am using bootstrap v3. It seems the grid can\'t differentiate between the two as the b

相关标签:
11条回答
  • 2020-12-23 12:04

    If you only have 2 columns, you can try it. I fixed it with the code below.

    <div class="row">
        <div class="w-50 p-3 float-left">            
        </div>
        <div class="w-50 p-3 float-right">
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-23 12:05

    I had a similar problem, for me the easiest solution was to manually modify the width for elements I wanted to appear differently when printed (and I added a specific class -in my case: title-container, details-container for those, along the col-xs-6 etc.).

    For example:

    <div class="jumbotron">
        <div class="container">
            <div class="row">
                <div class="col-xs-12 col-ms-3 col-sm-6 col-md-6 title-container">
                Some stuff
                </div>
    
                <div class="col-xs-12 col-ms-9 col-sm-6 col-md-6 details-container">
                Some more stuff
                </div>
            </div>
        </div>
    </div>
    
    @media print {
        .title-container {
            width: 360px;
            float: left;
        }
    
        .details-container {
            width: 300px;
            float: right;
        }
    }
    

    In my case I needed a column to be floated on the right, one to the left, thus the floats... You could set in your custom css the width also for .col-xs-6 etc. just a quick and dirty solution, but did the job for a page where I needed this...

    0 讨论(0)
  • 2020-12-23 12:06

    If you want the Bootstrap's grid do not print with col-xs (mobile settings) , and want to use col-sm-?? instead , Based on Fredy31 answer and you don't even need to define col-print-??. simply rewrite all col-md-?? css class definitions inside a: @media print { /* copy and paste from bootstrap.css*/ } like this:

    @media print {
       .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
            float: left;
       }
       .col-sm-12 {
            width: 100%;
       }
       .col-sm-11 {
            width: 91.66666667%;
       }
       .col-sm-10 {
            width: 83.33333333%;
       }
       .col-sm-9 {
            width: 75%;
       }
       .col-sm-8 {
            width: 66.66666667%;
       }
       .col-sm-7 {
            width: 58.33333333%;
       }
       .col-sm-6 {
            width: 50%;
       }
       .col-sm-5 {
            width: 41.66666667%;
       }
       .col-sm-4 {
            width: 33.33333333%;
       }
       .col-sm-3 {
            width: 25%;
       }
       .col-sm-2 {
            width: 16.66666667%;
       }
       .col-sm-1 {
            width: 8.33333333%;
       }
    }
    
    0 讨论(0)
  • 2020-12-23 12:06

    The Sass version of Fredy31 solution:

    @for $i from 1 through 12 {
        .col-print-#{$i} {
            width: #{percentage(round($i*8.33)/100)};
            float: left;
        }
    }
    
    0 讨论(0)
  • 2020-12-23 12:16

    Instead of recreating with new column names like .col-print-1 , .col-print-2 , write a media query which will be enable while printing the document.

      @media print {
      .col-md-1,.col-md-2,.col-md-3,.col-md-4,
      .col-md-5,.col-md-6,.col-md-7,.col-md-8, 
      .col-md-9,.col-md-10,.col-md-11,.col-md-12 {
        float: left;
      }
    
      .col-md-1 {
        width: 8%;
      }
      .col-md-2 {
        width: 16%;
      }
      .col-md-3 {
        width: 25%;
      }
      .col-md-4 {
        width: 33%;
      }
      .col-md-5 {
        width: 42%;
      }
      .col-md-6 {
        width: 50%;
      }
      .col-md-7 {
        width: 58%;
      }
      .col-md-8 {
        width: 66%;
      }
      .col-md-9 {
        width: 75%;
      }
      .col-md-10 {
        width: 83%;
      }
      .col-md-11 {
        width: 92%;
      }
      .col-md-12 {
        width: 100%;
      }
    }
    

    So by this way we can be able to apply print css styles directly without changing the column names.

    0 讨论(0)
提交回复
热议问题