推荐用 clip-path: polygon() 配合 lh 单位实现响应式丝带角标,1lh = 当前行高,比固定像素更可靠;混用 1lh、calc(100% - var(--r)) 和百分比可自适应折叠角度与留白,须加 overflow: hidden 防旧版 Safari 渲染异常。
用clip-pathborderclip-path: polygon()lhlh1lhcalc(100% - var(--r))overflow: hiddenclip-path.ribbon {
--r: 16px;
line-height: 1.8;
padding-inline: 1.2lh calc(var(--r) + .2lh);
background: #ff6b6b;
clip-path: polygon(
1lh 0,
100% 0,
calc(100% - var(--r)) 50%,
100% 100%,
1lh 100%,
1lh calc(100% + var(--r)),
0 calc(100% + var(--r)),
0 100%
);
}:before:afterv5.17.0transform: rotate(45deg)position: relativetop/rightrotate(45deg)transform-originbackface-visibility: hidden-webkit-font-smoothing: antialiased.ribbon-wrapper {
position: relative;
}
.ribbon {
position: absolute;
top: -8px;
right: -8px;
transform: rotate(45deg);
transform-origin: center;
padding: 4px 12px;
background: #1677ff;
color: white;
font-size: 12px;
}.ant-ribbon.ant-ribbon-corner
),且硬编码了
inset-inline-end
、
filter: brightness(75%)
等现代属性。直接拷代码到普通 HTML 会漏样式、缺定位。
想轻量复刻它的视觉逻辑,只需关注三点:
主色块用
position: absolute
+
top/right
定位
右下角小三角用
:after
+
border
构造,
border-inline-end-color: transparent
控制方向
加
box-shadow
模拟微立体感,别依赖
filter
(低版本 Chrome 不支持)
移动端适配时
em
和
rem
比像素更安全
用户缩放页面或系统字体调大时,写死
16px
的丝带会突然变小或压扁。用
em
(相对于父字体大小)或
rem
(相对于根字体)能保持比例。
实操建议:
把
--r
、
padding
、
line-height
全部换成
em
,例如
--r: 1em
避免在
clip-path
里混用
px
和
%
,统一用相对单位
真机测试时重点看 iOS Safari —— 它对
clip-path
的渲染有延迟,有时首屏闪一下才切好
真正难的不是画出丝带,而是让它在文字换行、字号切换、横竖屏切换时都不崩形。优先选
clip-path
+
lh
方案,但得接受 IE 完全不支持;要保底就用旋转法,再加一层媒体查询兜底。
