HTML CSS实现Chevron/Arrow导航栏:选中态边框显示异常求助
Chevron导航栏选中项边框被裁剪的解决方案
问题背景
尝试实现箭头样式的步骤导航栏时,选中项的黄色边框出现顶部被裁剪、底部完全不可见的问题,需要让边框完整显示在导航栏高度范围内。
原实现代码
HTML代码
<div id="wrapper"> <ul class="chevrons"> <li><a href="#" class="previous">Step 1</a></li> <li><a href="#" class="selected">Step 2</a></li> <li><a href="#">Step 3</a></li> <li><a href="#">Step 4</a></li> </ul> </div>
原CSS代码
html { font-family: sans-serif; font-size: 1em; -ms-text-size-adjust: 1em; -webkit-text-size-adjust: 1em; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } body { margin: 0; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 1.42857143; color: #333333; background-color: #ffffff; } #wrapper { width: 600px; margin: 50px auto; } ul.chevrons { padding: 0; margin: 0; height: 2.5em; font-size: 1.125em; background-color: #e3edf9; overflow: hidden; width: 100%; } ul.chevrons li { list-style: none; text-align: center; line-height: 2.5em; float: left; } ul.chevrons li { width: 25%; /* 项数变化时调整:3项设33%,2项设50% */ } ul.chevrons li a { width: 100%; color: #212121; text-decoration: none; display: inline-block; position: relative; text-indent: 0.75em; } ul.chevrons li a.selected { border: 1px solid yellow; background-color: #013e75; color: #fff; } ul.chevrons li a.previous { color: #6c6c6c; } ul.chevrons li a:before, ul.chevrons li a:after { content: " "; display: block; width: 0; height: 0; border-top: 1.5em solid transparent; border-bottom: 1.5em solid transparent; position: absolute; top: 50%; margin-top: -1.5em; left: 100%; } ul.chevrons li a:before { border-left: 0.75em solid yellow; margin-left: -0px; z-index: 1; } ul.chevrons li a:after { border-left: 0.75em solid #e3edf9; margin-left: -2px; z-index: 2; } ul.chevrons li a.selected:after { border-left: 0.75em solid #013e75; } ul.chevrons li:last-child a:before, ul.chevrons li:last-child a:after { display: none; }
问题原因
ul.chevrons设置了overflow:hidden,同时伪元素总高度(1.5em*2=3em)超过了ul的2.5em高度,导致溢出部分被裁剪。- 选中项的
a元素添加了边框,但未设置box-sizing:border-box,边框会额外增加元素高度,加剧溢出问题。
修改后的CSS代码
html { font-family: sans-serif; font-size: 1em; -ms-text-size-adjust: 1em; -webkit-text-size-adjust: 1em; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } body { margin: 0; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 1.42857143; color: #333333; background-color: #ffffff; } #wrapper { width: 600px; margin: 50px auto; } ul.chevrons { padding: 0; margin: 0; height: 2.5em; font-size: 1.125em; background-color: #e3edf9; width: 100%; } ul.chevrons li { list-style: none; text-align: center; line-height: 2.5em; float: left; height: 100%; } ul.chevrons li { width: 25%; } ul.chevrons li a { width: 100%; color: #212121; text-decoration: none; display: inline-block; position: relative; text-indent: 0.75em; box-sizing: border-box; height: 100%; } ul.chevrons li a.selected { border: 1px solid yellow; background-color: #013e75; color: #fff; } ul.chevrons li a.previous { color: #6c6c6c; } ul.chevrons li a:before, ul.chevrons li a:after { content: " "; display: block; width: 0; height: 0; border-top: 1.25em solid transparent; border-bottom: 1.25em solid transparent; position: absolute; top: 0; left: 100%; } ul.chevrons li a:before { border-left: 0.75em solid yellow; margin-left: -1px; z-index: 1; } ul.chevrons li a:after { border-left: 0.75em solid #e3edf9; margin-left: -2px; z-index: 2; } ul.chevrons li a.selected:after { border-left: 0.75em solid #013e75; } ul.chevrons li:last-child a:before, ul.chevrons li:last-child a:after { display: none; }
关键修改点
- 移除
ul.chevrons的overflow:hidden,避免裁剪边框和伪元素。 - 给
a元素添加box-sizing:border-box,让边框包含在元素高度内,不额外增加高度。 - 调整伪元素的
border-top和border-bottom为1.25em,总高度匹配ul的2.5em,避免溢出。 - 伪元素设置
top:0,移除margin-top,让箭头和导航栏高度完全对齐。 - 微调
a:before的margin-left为-1px,让黄色边框和箭头完美衔接。
内容的提问来源于stack exchange,提问作者Fahadhastocode
相关产品推荐
相关产品推荐

