Materialize可滑动Tabs自定义指示器遮挡Tab文字的层级问题咨询
遮挡问题核心原因
Materialize 内置的.tabs .indicator为绝对定位元素,DOM渲染顺序排在所有tab导航项之后,默认层叠规则下后渲染的元素会覆盖在先渲染元素之上,因此自定义的黄色指示器会遮挡tab上的文字。
零HTML改动的调整方案
仅通过CSS调整元素层叠关系即可解决问题,核心是为涉及层叠的元素设置有效的定位属性和明确的z-index层级,保证文字浮在指示器上层:
- 为tab导航项、tab内文字链接添加非static的定位属性,让z-index规则生效
- 给tab内文字链接设置更高的z-index值
- 给指示器设置更低的z-index值,让它作为背景层存在
修正后的完整CSS代码如下:
.tabs { position: relative; overflow-x: auto; overflow-y: hidden; height: 40px; width: 80%; background-color: #262626; border-radius:50px; margin: 0 auto; white-space: nowrap; } .tabs .tab { display:inline-block; text-align: center; line-height: 40px; height: 40px; width:calc(80%/2); padding: 0px; margin: 0; text-transform: uppercase; position: relative; /* 新增:支撑z-index生效 */ } .tabs .tab a { color: #fff; display: block; width: 100%; text-decoration:none; height: 100%; padding: 0 20px; font-size: 14px; text-overflow: ellipsis; overflow: hidden; font-family:Enviro; -webkit-transition: color .28s ease, background-color .28s ease; transition: color .28s ease, background-color .28s ease; position: relative; /* 新增:支撑z-index生效 */ z-index: 2; /* 新增:置于上层 */ } .tabs .indicator { position:absolute; height: 32px; border-radius:50px; background-color: #f5d247; will-change: left, right; z-index: 1; /* 新增:置于下层作为背景 */ }
可选可读性优化
可单独给选中态的tab链接设置深色文字(比如黑色),提升文字在黄色指示器背景上的对比度,代码示例:
.tabs .tab a.active { color: #000; }
内容的提问来源于stack exchange,提问作者Ankit Kumar
相关产品推荐
相关产品推荐

