1、使用表格默认布局 + 合并边框
table-layout: auto(默认)模式下,内容可能撑开列宽。
代码
.detail {
border-collapse: collapse;
&-row,
.bold-row {
height: 30px;
line-height: 30px;
&-col01 {
width: 40px;
text-align: center;
}
&-col02 {
width: 100px;
}
&-col03 {
width: 180px;
}
&-col04 {
width: 40px;
text-align: center;
}
&-col05 {
width: 40px;
text-align: center;
}
&-col06 {
width: 80px;
text-align: center;
}
&-col07 {
width: 80px;
text-align: center;
}
&-col08 {
width: 158px;
}
}
}
// 表格单元格样式
.detail td {
// 边框 1px
border: 1px solid #999;
// 左右内边距 4px
padding: 0 4px;
}
用途设置了width: 158px;
这个158是不包含边框、不包含左右内边距的
但是页面上查看实际是 139.854,说明在默认模式(table-layout: auto)下,列宽会变化,与代码设置的不一样,这样在默认模式(table-layout: auto)下就是没有办法指定固定列宽。

打印效果:

2、 使用表格固定布局 + 合并边框
table-layout: flex 固定布局,严格按设置的宽度分配
代码
.detail {
// 合并边框,合并边框腾出的空间为:边框宽度 * (列数 - 1)
border-collapse: collapse;
// 固定列宽布局
table-layout: fixed;
&-row,
.bold-row {
height: 30px;
line-height: 30px;
&-col01 {
// width: 40px;
width: 30px; // 实际宽度为 30px + 4px(左内边距)+ 4px(右内边距)+ 1px(左边框)+ 1px(右边框)= 40px
text-align: center;
}
&-col02 {
// width: 100px;
width: 90px;
}
&-col03 {
// width: 180px;
// 这个表格有8列,合并边框腾出的空间为:边框宽度 * (列数 - 1) = 1 * (8 - 1) = 7px,把这个7px加到这列的宽度中 170 + 7 = 177px
width: 177px;
}
&-col04 {
// width: 40px;
width: 30px;
text-align: center;
}
&-col05 {
// width: 40px;
width: 30px;
text-align: center;
}
&-col06 {
// width: 80px;
width: 70px;
text-align: center;
}
&-col07 {
// width: 80px;
width: 70px;
text-align: center;
}
&-col08 {
// width: 158px;
width: 148px;
}
}
}
// 表格单元格样式
.detail td {
// 边框 1px
border: 1px solid #999;
// 左右内边距 4px
padding: 0 4px;
}
代码中,用途设置了 width: 148px;
这个148是不包含边框、不包含左右内边距的
页面上查看实际也是 148,但是屏幕上显示的出来宽度是 158,因为单元格设置了边框和内边距,148 + 4px(左内边距)+ 4px(右内边距)+ 1px(左边框)+ 1px(右边框)= 158,说明在固定布局(table-layout: flex)下,列宽不会变化,与代码设置的一样,这样在固定布局(table-layout: flex)下就可以指定固定列宽。

但是最终的打印效果又有问题,也屏幕显示的效果不一样,没有铺满页面的打印区域:

解决这个问题,需要重新设置打印样式:
重新设定列宽,不用考虑边框和内边距
// 设置打印样式
@media print {
.detail {
&-row,
.bold-row {
&-col01 {
width: 40px;
// width: 30px; // 实际宽度为 30px + 4px(左内边距)+ 4px(右内边距)+ 1px(左边框)+ 1px(右边框)= 40px
text-align: center;
}
&-col02 {
width: 100px;
// width: 90px;
}
&-col03 {
width: 180px;
// 这个表格有8列,合并边框腾出的空间为:边框宽度 * (列数 - 1) = 1 * (8 - 1) = 7px,把这个7px加到这列的宽度中 170 + 7 = 177px
// width: 177px;
}
&-col04 {
width: 40px;
// width: 30px;
text-align: center;
}
&-col05 {
width: 40px;
// width: 30px;
text-align: center;
}
&-col06 {
width: 80px;
// width: 70px;
text-align: center;
}
&-col07 {
width: 80px;
// width: 70px;
text-align: center;
}
&-col08 {
width: 158px;
// width: 148px;
}
}
}
}
打印效果:

其他无效的尝试方法:
1、 设置打印样式中,重新设置表格单元格的内边距
// 设置打印样式
@media print {
.detail td {
border: 1px solid #000;
// 左右内边距 4px
padding: 0 4px;
}
}
说明在多媒体打印样式中,设置表格单元格内边距样式无效。
4449




被折叠的 条评论
为什么被折叠?



