如何用HTML和CSS复刻带三角右边缘的Realme文件管理器导航栏?
实现Realme文件管理器导航栏的三角效果及同类模板
问题说明
我想要用HTML和CSS复刻Realme文件管理器中的导航栏(参考图:Realme文件管理器导航栏),已编写了如下HTML和CSS代码,但想要实现导航栏右侧的三角形状效果,请问该如何实现?同时求同类导航栏的模板。
原代码
CSS
.header { width: 100%; position: fixed; top: 0; left: 0; z-index: 2; opacity: 1; display: flex; flex-direction: row; } .nav-bar { background-color: var(--nav-color ); box-sizing: border-box; text-align: center; height: 43px; margin: 8px; overflow: hidden; border: 1.5px solid #000000; } .first { position: relative; z-index: 4; width: 106px; border-radius: 15px; } .second { width: 100px; position: relative; left: -29px; z-index: 3; border-radius: 0 15px 15px 0; } .third { width: 100px; position: relative; left: -60px; z-index: 2; border-radius: 0 15px 15px 0; }
HTML
<!--===== HEADER =====--> <header class="header"> <!--===== NAV-BAR =====--> <a href=#home class="nav-bar first"><span class="nav-bar-title">Home</span></a> <a href=#about class="nav-bar second"><span class="nav-bar-title">About</span></a> <a href=#more class="nav-bar third"><span class="nav-bar-title">More</span></a> </header>
实现三角效果的方法
可以通过CSS伪元素::after/::before绘制三角形,配合相对定位实现导航栏的三角衔接,同时优化布局避免手动偏移的问题:
修改后的完整代码
CSS
:root { --nav-color: #ffffff; /* 自定义导航栏背景色 */ } .header { width: 100%; position: fixed; top: 0; left: 0; z-index: 2; display: flex; align-items: center; padding: 0 10px; box-sizing: border-box; } .nav-bar { background-color: var(--nav-color); box-sizing: border-box; text-align: center; height: 43px; line-height: 43px; /* 文字垂直居中 */ position: relative; border: 1.5px solid #000; margin-left: -12px; /* 相邻导航栏重叠,替代手动left偏移 */ } /* 第一个导航栏:左圆角+右三角 */ .nav-bar.first { width: 106px; border-radius: 15px 0 0 15px; margin-left: 0; } .nav-bar.first::after { content: ''; position: absolute; top: 0; right: -12px; border-top: 21.5px solid transparent; border-bottom: 21.5px solid transparent; border-left: 12px solid #000; z-index: 1; } .nav-bar.first::after { border-left-color: var(--nav-color); } /* 中间导航栏:左右三角 */ .nav-bar.second { width: 100px; } .nav-bar.second::after { content: ''; position: absolute; top: 0; right: -12px; border-top: 21.5px solid transparent; border-bottom: 21.5px solid transparent; border-left: 12px solid #000; z-index: 1; } .nav-bar.second::before { content: ''; position: absolute; top: 0; left: -12px; border-top: 21.5px solid transparent; border-bottom: 21.5px solid transparent; border-right: 12px solid #000; z-index: 1; } .nav-bar.second::after { border-left-color: var(--nav-color); } .nav-bar.second::before { border-right-color: var(--nav-color); } /* 最后一个导航栏:右圆角+左三角 */ .nav-bar.third { width: 100px; border-radius: 0 15px 15px 0; } .nav-bar.third::before { content: ''; position: absolute; top: 0; left: -12px; border-top: 21.5px solid transparent; border-bottom: 21.5px solid transparent; border-right: 12px solid #000; z-index: 1; } .nav-bar.third::before { border-right-color: var(--nav-color); } /* 文字层级高于三角,避免被遮挡 */ .nav-bar-title { position: relative; z-index: 2; }
HTML
<!--===== HEADER =====--> <header class="header"> <!--===== NAV-BAR =====--> <a href="#home" class="nav-bar first"><span class="nav-bar-title">Home</span></a> <a href="#about" class="nav-bar second"><span class="nav-bar-title">About</span></a> <a href="#more" class="nav-bar third"><span class="nav-bar-title">More</span></a> </header>
关键说明
- 用
margin-left: -12px实现导航栏重叠,替代原代码手动left偏移的不稳定写法。 - 伪元素绘制三角:利用透明边框+单侧有色边框生成三角形状,精准衔接导航栏边缘。
- 单独设置伪元素的背景色和边框色,保证导航栏整体线条连贯。
- 文字设置更高层级,避免被三角伪元素遮挡。
同类导航栏模板
斜切式堆叠导航
:root { --primary-bg: #f5f5f5; --border-color: #ddd; } .stack-nav { display: flex; align-items: center; padding: 10px; } .stack-nav-item { background: var(--primary-bg); border: 1px solid var(--border-color); padding: 8px 20px; position: relative; margin-left: -10px; text-decoration: none; color: #333; font-size: 14px; } .stack-nav-item:first-child { border-radius: 8px 0 0 8px; margin-left: 0; } .stack-nav-item:last-child { border-radius: 0 8px 8px 0; } .stack-nav-item::before { content: ''; position: absolute; top: 0; left: -10px; width: 0; height: 0; border-top: 18px solid transparent; border-bottom: 18px solid transparent; border-right: 10px solid var(--primary-bg); } .stack-nav-item::after { content: ''; position: absolute; top: 0; left: -11px; width: 0; height: 0; border-top: 19px solid transparent; border-bottom: 19px solid transparent; border-right: 10px solid var(--border-color); z-index: -1; } .stack-nav-item:last-child::before, .stack-nav-item:last-child::after { display: none; } .stack-nav-item:first-child::before, .stack-nav-item:first-child::after { display: none; }
<div class="stack-nav"> <a href="#" class="stack-nav-item">全部文件</a> <a href="#" class="stack-nav-item">图片</a> <a href="#" class="stack-nav-item">视频</a> <a href="#" class="stack-nav-item">文档</a> </div>
内容的提问来源于stack exchange,提问作者Uday Shirbhate
相关产品推荐
相关产品推荐

