css如何制作未读图标
使用伪元素和计数器
在CSS中,可以利用伪元素和计数器创建未读图标。通过::after或::before伪元素添加数字标记,结合counter属性动态显示未读数量。
.unread-icon {
position: relative;
display: inline-block;
width: 24px;
height: 24px;
background-color: #ccc;
border-radius: 50%;
}
.unread-icon::after {
content: attr(data-count);
position: absolute;
top: -5px;
right: -5px;
background-color: red;
color: white;
border-radius: 50%;
width: 18px;
height: 18px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
}
使用绝对定位和动画
通过绝对定位将未读标记固定在右上角,并添加动画效果增强视觉提示。可以使用transform和@keyframes实现脉冲动画。

.unread-badge {
position: relative;
display: inline-block;
}
.unread-badge::after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 8px;
height: 8px;
background-color: red;
border-radius: 50%;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.3); opacity: 0.7; }
100% { transform: scale(1); opacity: 1; }
}
使用Flex布局和动态内容
结合Flex布局和动态内容属性attr(),可以灵活控制未读标记的位置和样式。适用于需要动态更新未读数量的场景。

.notification {
display: flex;
align-items: center;
gap: 8px;
}
.notification-count {
min-width: 20px;
padding: 2px 6px;
background-color: red;
color: white;
border-radius: 10px;
font-size: 12px;
text-align: center;
}
使用CSS变量和自定义属性
通过CSS变量实现未读标记的颜色和大小动态调整,增强可维护性。适用于主题切换或响应式设计。
:root {
--unread-color: red;
--unread-size: 16px;
}
.unread-indicator {
position: relative;
}
.unread-indicator::before {
content: "";
position: absolute;
top: -4px;
right: -4px;
width: var(--unread-size);
height: var(--unread-size);
background-color: var(--unread-color);
border-radius: 50%;
}
响应式设计适配
针对不同屏幕尺寸调整未读标记的大小和位置,使用媒体查询确保在不同设备上显示效果一致。
.unread-marker {
position: relative;
width: 32px;
height: 32px;
}
.unread-marker::after {
content: attr(data-count);
position: absolute;
top: -8px;
right: -8px;
width: 20px;
height: 20px;
font-size: 12px;
}
@media (max-width: 600px) {
.unread-marker::after {
width: 16px;
height: 16px;
font-size: 10px;
}
}






