对于 Bootstrap 4 sass,这里有一些我在多个项目中应用的代码片段。这些代码片段可以进行以下调整:
- 网格(遵循 LG 断点)
- 间距(重写所有 LG 以下的边距/填充)
- 按钮(由于背景颜色在打印预览中无法使用,将填充按钮切换为轮廓)
- 显示(重写所有 LG 以下的显示)
- 文本对齐方式(重写所有 LG 以下的断点)
@media print {
$grid-breakpoints-print: (lg: 992px); // keep breakpoint that you would like to apply for print
/* Rewrite margins, padding, display & alignment to keep the LG and not the mobile ones */
@each $breakpoint in map-keys($grid-breakpoints-print) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints-print);
// rewrite all displays for your print breakpoint
@each $value in $displays {
.d
display: $value !important;
@each $v in $displays {
&.d-
display: $value !important;
&.d-print-none,
&.table__sort {
display: none !important;
}
}
}
&.d-print-none {
display: none !important;
}
}
}
// rewrite all spacings for your print breakpoint
@each $prop, $abbrev in (margin: m, padding: p) {
@each $size, $length in $spacers {
.
@each $s, $l in $spacers {
&.
&.
}
}
}
.
.
@each $s, $l in $spacers {
&.
&.
&.
&.
}
}
}
.
.
@each $s, $l in $spacers {
&.
&.
&.
&.
}
}
}
.
.
@each $s, $l in $spacers {
&.
&.
&.
&.
}
}
}
.
.
@each $s, $l in $spacers {
&.
&.
&.
&.
}
}
}
}
}
// rewrite all text alignments for your print breakpoint
.text
text-align: left !important;
&.text-left,
&.text-right,
&.text-center {
text-align: left !important;
}
}
.text
text-align: right !important;
&.text-left,
&.text-right,
&.text-center {
text-align: right !important;
}
}
.text
text-align: center !important;
&.text-left,
&.text-right,
&.text-center {
text-align: center !important;
}
}
}
/* Rewrite grid to keep the LG and discard the mobile */
@for $i from 1 through 12 {
.col-lg-#{$i} {
flex: 0 0 #{percentage(round($i*8.33)/100)} !important;
max-width:
@for $k from 1 through 12 {
&.col-xs-
&.col-sm-
&.col-md-
&.col-
flex: 0 0
max-width:
}
}
}
}
/* Since the print will not fill background-colors you need to transform filled buttons into outline */
@each $color, $value in $theme-colors {
.btn-
color: $value !important;
background-color: $white !important;
}
}
}
这里是一个可工作的Fiddle。请注意,只有@media print { ... } 在fiddle示例中起作用。为了使fiddle正常工作,从开头复制了变量。