css制作角标
使用伪元素创建角标
通过 ::before 或 ::after 伪元素生成角标,结合绝对定位调整位置:
.element {
position: relative;
display: inline-block;
}
.element::after {
content: "NEW";
position: absolute;
top: -5px;
right: -10px;
background: red;
color: white;
font-size: 12px;
padding: 2px 5px;
border-radius: 3px;
}
利用 transform 实现偏移
通过旋转和位移创造斜角效果:

.badge {
display: inline-block;
background: #ff4757;
color: white;
padding: 2px 8px;
transform: rotate(15deg) translate(5px, -5px);
}
圆形数字角标
适合未读消息等数字提示:

.notification {
position: relative;
}
.notification::after {
content: "5";
position: absolute;
top: -8px;
right: -8px;
width: 18px;
height: 18px;
background: #3498db;
border-radius: 50%;
color: white;
text-align: center;
line-height: 18px;
font-size: 12px;
}
边框角标样式
结合边框和伪元素创建贴边角标:
.ribbon {
position: relative;
overflow: hidden;
}
.ribbon::before {
content: "HOT";
position: absolute;
top: 10px;
right: -25px;
width: 100px;
padding: 3px 0;
background: #ff6b6b;
color: white;
text-align: center;
transform: rotate(45deg);
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
使用 CSS 变量动态控制
通过变量实现可配置化角标:
:root {
--badge-color: #e74c3c;
--badge-size: 20px;
}
.dynamic-badge {
--badge-content: "!";
position: relative;
}
.dynamic-badge::after {
content: var(--badge-content);
position: absolute;
top: calc(var(--badge-size) / -2);
right: calc(var(--badge-size) / -2);
width: var(--badge-size);
height: var(--badge-size);
background: var(--badge-color);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: calc(var(--badge-size) * 0.6);
}






