Table单元格td的position:relative的兼容性

本文探讨了在IE和FF浏览器中,表格单元格内使用position属性进行元素定位时的兼容性问题。指出在FF中position:relative需要与display:block/inline-block同时使用才有效,而在IE中则可以与多种display属性配合使用。

( From:  http://www.itref.cn/a/css/2010/0706/70.html)

问题描述:

默认情况下,table的单元格td的display为table-cell,在IE给td设置position:relative,然后给它包含的一个容器使用position:absolute进行定位是有效的,但在FF下却不可以。但是在IE下,position:absolute的容器的z-index总是比td的z-index低,td层总是在position:absolute的容器的上面

发生条件:

1. IE6、IE7、IE8和FF浏览器
2. 使用td默认样式,设置td的position:relateve,再给td内的容器设置position:absolute定位

原因分析:

1. 在FF中position:relative要与display:block/inline-block才能生效,display:block/inline-block可以是默认块元素,或是被定义的元素。
2. 而在IE中position:relative除了与display:block/inline-block可以生效外,与display:table-cell、table等都可以

实例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>Table单元格内容器定位的兼容性</title>
<style type="text/css">
*{margin:0;padding:0;}
.tb{display:block;width:304px;margin:0 auto;border-collapse:collapse;position:relative;}
.tb td{width:150px;border:1px solid #DDD;position:relative;}
.sub{background-color:#FF0000;width:150px;padding:3px;border:1px solid #FF6600;position:absolute;z-index:9999;left:152px;top:0;}
</style>
</head>
<body>
<h1>可以在IE和FF下看到区别</h1>
<table class="tb">
    <tbody>
            <tr>
                    <td rowspan="2"> <wbr></td>
                        <td> <wbr></td>
                </tr>
                <tr>                  
                        <td> <wbr></td>
                </tr>
                <tr>                  
                        <td class="cell"> <wbr><div class="sub">第二行第一个单元格内的容器</div></td>
                        <td> <wbr></td>
                </tr>
        </tbody>
</table>
默认情况下,IE中td的position:relative是有效的;<br />
在FF中td的position:relative是无效的<br />
要在FF中使table的position:relative有效,需要同时设置display:block,而IE不用同时设置display:block
</body>
</html>

 

新浪网友2011-04-28 11:01:50[回复] [删除] [举报]

我在机器上试了 你这个说的不对,w3c没有对table元素的position:relative进行定义,不管table-cell内部绝对定位的元素是block/ inline-block /inline,都不起作用的。
<template> <div class="accordion-container"> <table class="accordion-table"> <!-- 表头 --> <thead> <tr> <th class="text w-55" fixed>序号</th> <th class="text w-100">变更时间</th> <th class="text max-w-150">变更单</th> <th class="text w-250">111</th> <th class="text w-55">实施人</th> <th class="text w-90">子系统名称</th> <th class="text w-105">计划时间与实际时间偏差</th> <th class="text w-105">计划时长与实际时长偏差</th> <th class="w-100">状态</th> <th class="text w-55" fixed="right">操作</th> </tr> </thead> <!-- 表主体 --> <tbody v-for="i in 3" style="height: 38px;overflow: hidden;"> <tr> <td colspan="10"> <div class="tbodyTitle"><span>应用变更</span></div> </td> </tr> <tr v-for="item in 10"> <td class="text" fixed>1</td> <td class="text">09-21:09:00</td> <td class="text">09-21:09:0009-21:09:0009-21:09:0009-21:09:0009-21:09:00</td> <td class="text"></td> <td class="text">张某某</td> <td class="text">111</td> <td class="text">13分钟</td> <td class="text">13分钟</td> <td class="text">待实施</td> <td class="text" fixed="right">验证</td> </tr> </tbody> </table> </div> </template> <script> export default { } </script> <style lang="less" scoped> .accordion-container { width: 100%; overflow-x: auto; white-space: nowrap; position: relative; } /* ==== 滚动条样式(WebKit 内核:Chrome / Edge / Safari)==== */ .accordion-container::-webkit-scrollbar { height: 4px; /* 横向滚动条高度 */ } .accordion-container::-webkit-scrollbar-track { background: transparent; border-radius: 0px; } .accordion-container::-webkit-scrollbar-thumb { background: #c0c0c0; border-radius: 0px; } .accordion-container::-webkit-scrollbar-thumb:hover { background: #a0a0a0; } .accordion-table { border-collapse: collapse; /* 关键:合并边框,去除单元格间隙 */ border-spacing: 0; /* 兼容性兜底 */ thead {} tbody {} tbody tr { background-color: #001b32; } tr { height: 38px; } th { height: 100%; padding: 0 8px; background-color: rgba(23, 100, 187, 0.15); } td { padding: 0 8px; height: 100%; } th.text,td.text { color: #9caabf; font-family: 'Alibaba_PuHuiTi_55'; font-size: 14px; font-weight: 400; line-height: normal; font-style: normal; } /* 固定第一列 */ td[fixed], th[fixed]{ position: sticky; left: -0.5px; background-color: #071C33; /* 背景色防止滚动时透明 */ z-index: 10; /* 确保覆盖其他单元格 */ } td[fixed='right'],th[fixed='right'] { position: sticky; right: -0.5px; background-color: #071C33; /* 背景色防止滚动时透明 */ z-index: 10; /* 确保覆盖其他单元格 */ } .w-55 { width: 55px } .w-90 { width: 90px } .w-100 { width: 100px } .w-105 { width: 205px } .w-250 { width: 250px } .max-w-150 { width: 250px } .noPading { padding: 0; } .tbodyTitle { width: 874px; height: 38px; box-sizing: border-box; border-bottom: 1px solid rgba(0, 66, 110, 0); background: linear-gradient(90deg, rgba(0, 44, 81, 0) 0%, #002c51 50%, rgba(0, 44, 81, 0) 100%); cursor: pointer; position: relative; span { position: sticky; left: 397px; top: 46px; color: #fff; font-family: 'HYRunYuan'; font-size: 20px; line-height: 24px; background: linear-gradient(0deg, #6fcfff 32.77%, #fff 79.52%); background-clip: text; -webkit-background-clip: text; -webkit-text-fill-color: transparent; } } } </style> 如何将以上代码改为 div + css 表格布局
09-23
<template> <div class="accordion-container"> <table class="accordion-table"> <!-- 表头 --> <thead> <tr> <th class="text w-55" fixed>序号</th> <th class="text w-100">变更时间</th> <th class="text max-w-150">变更单</th> <th class="text w-250">111</th> <th class="text w-55">实施人</th> <th class="text w-90">子系统名称</th> <th class="text w-105">计划时间与实际时间偏差</th> <th class="text w-105">计划时长与实际时长偏差</th> <th class="w-100">状态</th> <th class="text w-55" fixed="right">操作</th> </tr> </thead> <!-- 表主体 --> <tbody v-for="i in 3" style="height: 38px;overflow: hidden;"> <tr> <td colspan="10"> <div class="tbodyTitle"><span>应用变更</span></div> </td> </tr> <tr v-for="item in 10"> <td class="text" fixed>1</td> <td class="text">09-21:09:00</td> <td class="text">09-21:09:0009-21:09:0009-21:09:0009-21:09:0009-21:09:00</td> <td class="text"></td> <td class="text">张某某</td> <td class="text">111</td> <td class="text">13分钟</td> <td class="text">13分钟</td> <td class="text">待实施</td> <td class="text" fixed="right">验证</td> </tr> </tbody> </table> </div> </template> <script> export default { } </script> <style lang="less" scoped> .accordion-container { width: 100%; overflow-x: auto; white-space: nowrap; position: relative; } /* ==== 滚动条样式(WebKit 内核:Chrome / Edge / Safari)==== */ .accordion-container::-webkit-scrollbar { height: 4px; /* 横向滚动条高度 */ } .accordion-container::-webkit-scrollbar-track { background: transparent; border-radius: 0px; } .accordion-container::-webkit-scrollbar-thumb { background: #c0c0c0; border-radius: 0px; } .accordion-container::-webkit-scrollbar-thumb:hover { background: #a0a0a0; } .accordion-table { border-collapse: collapse; /* 关键:合并边框,去除单元格间隙 */ border-spacing: 0; /* 兼容性兜底 */ thead {} tbody {} tbody tr { background-color: #001b32; } tr { height: 38px; } th { height: 100%; padding: 0 8px; background-color: rgba(23, 100, 187, 0.15); } td { padding: 0 8px; height: 100%; } th.text,td.text { color: #9caabf; font-family: 'Alibaba_PuHuiTi_55'; font-size: 14px; font-weight: 400; line-height: normal; font-style: normal; } /* 固定第一列 */ td[fixed], th[fixed]{ position: sticky; left: -0.5px; background-color: #071C33; /* 背景色防止滚动时透明 */ z-index: 10; /* 确保覆盖其他单元格 */ } td[fixed='right'],th[fixed='right'] { position: sticky; right: -0.5px; background-color: #071C33; /* 背景色防止滚动时透明 */ z-index: 10; /* 确保覆盖其他单元格 */ } .w-55 { width: 55px } .w-90 { width: 90px } .w-100 { width: 100px } .w-105 { width: 205px } .w-250 { width: 250px } .max-w-150 { width: 250px } .noPading { padding: 0; } .tbodyTitle { width: 874px; height: 38px; box-sizing: border-box; border-bottom: 1px solid rgba(0, 66, 110, 0); background: linear-gradient(90deg, rgba(0, 44, 81, 0) 0%, #002c51 50%, rgba(0, 44, 81, 0) 100%); cursor: pointer; position: relative; span { position: sticky; left: 397px; top: 46px; color: #fff; font-family: 'HYRunYuan'; font-size: 20px; line-height: 24px; background: linear-gradient(0deg, #6fcfff 32.77%, #fff 79.52%); background-clip: text; -webkit-background-clip: text; -webkit-text-fill-color: transparent; } } } </style> 以上使用 grid布局来实现
最新发布
09-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值