css制作时间
CSS 制作时间轴的方法
水平时间轴
使用 flex 布局和伪元素创建水平时间轴:
.timeline {
display: flex;
padding: 20px 0;
position: relative;
}
.timeline::before {
content: '';
position: absolute;
top: 50%;
left: 0;
right: 0;
height: 2px;
background: #ddd;
}
.timeline-item {
flex: 1;
text-align: center;
position: relative;
}
.timeline-item::after {
content: '';
width: 12px;
height: 12px;
background: #3498db;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
垂直时间轴
通过 position 和边框实现垂直时间轴:
.timeline-vertical {
position: relative;
padding-left: 30px;
}
.timeline-vertical::before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 15px;
width: 2px;
background: #ddd;
}
.timeline-item-vertical {
position: relative;
margin-bottom: 20px;
}
.timeline-item-vertical::before {
content: '';
width: 12px;
height: 12px;
background: #e74c3c;
border-radius: 50%;
position: absolute;
left: -33px;
top: 5px;
}
响应式处理
添加媒体查询适应移动设备:
@media (max-width: 768px) {
.timeline {
flex-direction: column;
}
.timeline::before {
top: 0;
bottom: 0;
left: 50%;
width: 2px;
height: auto;
}
.timeline-item::after {
left: 50%;
top: 30px;
}
}
动画效果
为时间轴节点添加悬停动画:
.timeline-item:hover::after {
transform: translate(-50%, -50%) scale(1.5);
transition: all 0.3s ease;
}
.timeline-item-vertical:hover::before {
transform: scale(1.5);
transition: all 0.3s ease;
}
进阶样式
使用 CSS 变量实现主题色控制:
:root {
--timeline-color: #2ecc71;
--timeline-width: 2px;
}
.timeline-advanced::before {
background: var(--timeline-color);
width: var(--timeline-width);
}
.timeline-item-advanced::before {
background: var(--timeline-color);
box-shadow: 0 0 0 3px rgba(46, 204, 113, 0.2);
}
这些方法提供了从基础到进阶的时间轴实现方案,可根据实际需求组合使用或调整样式参数。







