对 table-layout 的错误解决

本文通过对比两种不同的CSS表布局实现方式,展示了如何使用table-layout属性解决布局偏差问题。当设定为fixed值时,即使单元格内容长度不一致,也能确保各列宽度均匀分布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

上图中运用了表布局,结果与预期效果(下图)出现了偏差。

这里写图片描述
首先看一下错误布局的代码。

  display: table;
  width: 860px;
  margin: 20px auto auto auto;
  padding-bottom: 30px;
  border-bottom: 2px solid #993399;
  border-spacing: 30px;

再来看一下正确布局的代码。

  display: table;
  width: 860px;
  margin: 20px auto auto auto;
  padding-bottom: 30px;
  border-bottom: 2px solid #993399;
  border-spacing: 30px;
  table-layout: fixed;

可以看出,两段代码唯一的差别是 table-layout 属性。在第一段代码中, table-layout 属性未进行设置,所以它的值为默认的 automatic ,也就是自动布局。在这种情况下,如果未对单元格设置宽度,单元格宽度会由单元格内容确定。由于右下角的单元格内文字更多,所以最后一列的宽度更宽,进而导致了中心一列单元格的偏移。

如果将 table-layout 设定为 fixed , 即固定布局。浏览器会在忽略单元格内容的情况下,根据表格宽度平均分配列宽,这样就改变了中心列偏移情况。

PS:以上讨论是在设定好整个表格宽度的基础上展开的。

<el-tab-pane v-for="item in tabType" v-model="applyType" :label="item.dictLabel" :name="item.dictValue" :key="item.dictValue"> <div class="biographical_table"> <el-table :data="tableData" border style="width: 100%;" height="69vh" v-loading="tableLoading" > <el-table-column type="index" width="50" align="center" label="序号"></el-table-column> <el-table-column prop="postName" label="标题" min-width="200" align="center"> </el-table-column> <el-table-column prop="postTypeValueName" label="角色" width="80" align="center"> </el-table-column> <el-table-column prop="driverName" label="姓名" width="80" align="center"> </el-table-column> <el-table-column prop="phone" label="联系方式" align="center" width="120"> </el-table-column> <el-table-column prop="wantPay" label="期望薪资" align="center" width="120"> </el-table-column> <el-table-column prop="carryTypeValueName" label="承运类型" align="center" width="80"> </el-table-column> <el-table-column prop="driverTypeValueName" label="司机类型" align="center" width="80"> </el-table-column> <el-table-column prop="idCard" label="身份证号" align="center" width="180"> </el-table-column> <el-table-column prop="driverAge" label="驾龄" width="120" align="center"> </el-table-column> <el-table-column prop="licenseLevel" label="驾证级别" width="120" align="center"> </el-table-column> <el-table-column label="操作" align="center"> <template #default="scope"> <el-button type="success" size="mini" @click="handleEntry(scope.row)">签约</el-button> <el-button type="danger" size="mini" @click="handleRefuse(scope.row)">拒绝</el-button> <el-button type="primary" size="mini" @click="handleDetail(scope.row)">详情</el-button> </template> </el-table-column> </el-table> </div> <div class="biographical_pagination"> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 15, 20, 25]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" > </el-pagination> </div> </el-tab-pane> </el-tabs>默认选择第一个
最新发布
08-22
<!-- 表格处理(增强版) --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%" border-collapse="collapse"> <xsl:for-each select="w:tblGrid/w:gridCol"> <fo:table-column column-width="{@w:w div 20}pt"/> </xsl:for-each> <fo:table-body> <xsl:apply-templates select="w:tr"/> </fo:table-body> </fo:table> </xsl:template> <!-- 表格行(过滤被合并单元格) --> <xsl:template match="w:tr"> <fo:table-row> <!-- 精确过滤被合并单元格:跳过所有标记为continue的单元格 --> <xsl:apply-templates select="w:tc[not(w:tcPr/w:vMerge/@w:val='continue')]"/> </fo:table-row> </xsl:template> <!-- 单元格模板(完整版) --> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center"> <!-- 处理列合并 --> <xsl:if test="w:tcPr/w:gridSpan"> <xsl:attribute name="number-columns-spanned"> <xsl:value-of select="w:tcPr/w:gridSpan/@w:val"/> </xsl:attribute> </xsl:if> <!-- 处理行合并 --> <xsl:if test="w:tcPr/w:vMerge[@w:val='restart']"> <!-- 计算实际跨行数 = 下方连续标记为continue的单元格数 + 1 --> <xsl:variable name="rowSpan"> <xsl:value-of select="count(following::w:tr/w:tc[1][w:tcPr/w:vMerge/@w:val='continue']) + 1"/> </xsl:variable> <xsl:attribute name="number-rows-spanned"> <xsl:value-of select="$rowSpan"/> </xsl:attribute> </xsl:if> <fo:block linefeed-treatment="ignore" white-space-collapse="true"> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template>我这个提示The column-number or number of cells in the row overflows the number of fo:table-columns specified for the table. (See position 1:9340),请直接给出修改后的整体代码
03-08
PS C:\Users\xpeng\Desktop\aurora\xp-ai-admin-aurora-fe> npm install @umijs/plugin-md-enhance --save npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: @ant-design/pro-form@1.74.7 npm WARN Found: rc-field-form@1.38.2 npm WARN node_modules/@ant-design/pro-form/node_modules/rc-field-form npm WARN rc-field-form@"~1.38.2" from antd@4.24.16 npm WARN node_modules/@ant-design/pro-form/node_modules/antd npm WARN antd@"^4.20.0 " from @ant-design/pro-form@1.74.7 npm WARN node_modules/@ant-design/pro-form npm WARN npm WARN Could not resolve dependency: npm WARN peer rc-field-form@"^1.22.0" from @ant-design/pro-form@1.74.7 npm WARN node_modules/@ant-design/pro-form npm WARN @ant-design/pro-form@"1.74.7" from @ant-design/pro-table@2.80.8 npm WARN node_modules/@ant-design/pro-table npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @ant-design/pro-layout@6.32.8 npm ERR! Found: antd@4.24.99-rc1 npm ERR! node_modules/antd npm ERR! antd@"4.24.99-rc1" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer antd@"4.x" from @ant-design/pro-layout@6.32.8 npm ERR! node_modules/@ant-design/pro-layout npm ERR! peer @ant-design/pro-layout@"^6.0.0" from @umijs/plugin-layout@0.18.1 npm ERR! node_modules/@umijs/plugin-layout npm ERR! @umijs/plugin-layout@"0.18.1" from @umijs/preset-react@1.8.32 npm ERR! node_modules/@umijs/preset-react npm ERR! dev @umijs/preset-react@"1.x" from the root project npm ERR! @ant-design/pro-layout@"6.32.8" from the root project npm ERR! npm ERR! Conflicting peer dependency: antd@4.24.16 npm ERR! node_modules/antd npm ERR! peer antd@"4.x" from @ant-design/pro-layout@6.32.8 npm ERR! node_modules/@ant-design/pro-layout npm ERR! peer @ant-design/pro-layout@"^6.0.0" from @umijs/plugin-layout@0.18.1 npm ERR! node_modules/@umijs/plugin-layout npm ERR! @umijs/plugin-layout@"0.18.1" from @umijs/preset-react@1.8.32 npm ERR! node_modules/@umijs/preset-react npm ERR! dev @umijs/preset-react@"1.x" from the root project npm ERR! @ant-design/pro-layout@"6.32.8" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\xpeng\AppData\Local\npm-cache\_logs\2025-06-11T09_03_17_716Z-eresolve-report.txt npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\xpeng\AppData\Local\npm-cache\_logs\2025-06-11T09_03_17_716Z-eresolve-report.txt npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! Fix the upstream dependency conflict, or retry npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! Fix the upstream dependency conflict, or retry npm ERR! Fix the upstream dependency conflict, or retry npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps PS C:\Users\xpeng\Desktop\aurora\xp-ai-admin-aurora-fe> npm install react-markdown highlight.js @umijs/plugin-mdx --save npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: @ant-design/pro-form@1.74.7 npm WARN Found: rc-field-form@1.38.2 npm WARN node_modules/@ant-design/pro-form/node_modules/rc-field-form npm WARN rc-field-form@"~1.38.2" from antd@4.24.16 npm WARN node_modules/@ant-design/pro-form/node_modules/antd npm WARN antd@"^4.20.0 " from @ant-design/pro-form@1.74.7 npm WARN node_modules/@ant-design/pro-form npm WARN npm WARN Could not resolve dependency: npm WARN peer rc-field-form@"^1.22.0" from @ant-design/pro-form@1.74.7 npm WARN node_modules/@ant-design/pro-form npm WARN @ant-design/pro-form@"1.74.7" from @ant-design/pro-table@2.80.8 npm WARN node_modules/@ant-design/pro-table npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @ant-design/pro-layout@6.32.8 npm ERR! Found: antd@4.24.99-rc1 npm ERR! node_modules/antd npm ERR! antd@"4.24.99-rc1" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer antd@"4.x" from @ant-design/pro-layout@6.32.8 npm ERR! node_modules/@ant-design/pro-layout npm ERR! peer @ant-design/pro-layout@"^6.0.0" from @umijs/plugin-layout@0.18.1 npm ERR! node_modules/@umijs/plugin-layout npm ERR! @umijs/plugin-layout@"0.18.1" from @umijs/preset-react@1.8.32 npm ERR! node_modules/@umijs/preset-react npm ERR! dev @umijs/preset-react@"1.x" from the root project npm ERR! @ant-design/pro-layout@"6.32.8" from the root project npm ERR! npm ERR! Conflicting peer dependency: antd@4.24.16 npm ERR! node_modules/antd npm ERR! peer antd@"4.x" from @ant-design/pro-layout@6.32.8 npm ERR! node_modules/@ant-design/pro-layout npm ERR! peer @ant-design/pro-layout@"^6.0.0" from @umijs/plugin-layout@0.18.1 npm ERR! node_modules/@umijs/plugin-layout npm ERR! @umijs/plugin-layout@"0.18.1" from @umijs/preset-react@1.8.32 npm ERR! node_modules/@umijs/preset-react npm ERR! dev @umijs/preset-react@"1.x" from the root project npm ERR! @ant-design/pro-layout@"6.32.8" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\xpeng\AppData\Local\npm-cache\_logs\2025-06-11T09_09_48_235Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: C:\Users\xpeng\AppData\Local\npm-cache\_logs\2025-06-11T09_09_48_235Z-debug-0.log
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值