css如何制作未读图标

未读图标实现方法
通过CSS可以创建多种未读图标的样式,以下是几种常见的实现方式:
使用伪元素创建小红点
.unread-badge {
position: relative;
display: inline-block;
}
.unread-badge::after {
content: '';
position: absolute;
top: -5px;
right: -5px;
width: 10px;
height: 10px;
background-color: red;
border-radius: 50%;
}
带数字的未读标记
.unread-count {
position: relative;
}
.unread-count::after {
content: attr(data-count);
position: absolute;
top: -10px;
right: -10px;
min-width: 20px;
height: 20px;
background-color: red;
color: white;
border-radius: 10px;
font-size: 12px;
text-align: center;
line-height: 20px;
padding: 0 4px;
}
动画效果的未读提示
.pulsing-badge {
position: relative;
}
.pulsing-badge::before {
content: '';
position: absolute;
top: -5px;
right: -5px;
width: 10px;
height: 10px;
background-color: red;
border-radius: 50%;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.5); opacity: 0.5; }
100% { transform: scale(1); opacity: 1; }
}
使用CSS变量自定义样式
:root {
--badge-size: 8px;
--badge-color: #ff4757;
}
.custom-badge {
--badge-offset: calc(var(--badge-size) / 2);
position: relative;
}
.custom-badge::after {
content: '';
position: absolute;
top: calc(-1 * var(--badge-offset));
right: calc(-1 * var(--badge-offset));
width: var(--badge-size);
height: var(--badge-size);
background-color: var(--badge-color);
border-radius: 50%;
}
这些方法可以根据具体需求进行调整,通过修改尺寸、颜色和位置来适应不同的设计场景。伪元素方式不会影响原有DOM结构,是实现未读图标的推荐做法。







