css制作时间轴
时间轴的基本结构
使用HTML创建一个无序列表<ul>,每个列表项<li>代表时间轴的一个节点。通过CSS为列表项添加样式,包括线条、圆点标记和内容区域。
<ul class="timeline">
<li>
<div class="content">
<h3>2023年</h3>
<p>事件描述内容...</p>
</div>
</li>
<li>
<div class="content">
<h3>2022年</h3>
<p>事件描述内容...</p>
</div>
</li>
</ul>
基础样式设置
为时间轴容器添加相对定位,并隐藏默认列表样式。通过伪元素在左侧创建时间轴的线条。

.timeline {
position: relative;
list-style: none;
padding-left: 1.5rem;
}
.timeline::before {
content: '';
position: absolute;
left: 10px;
top: 0;
height: 100%;
width: 2px;
background: #ddd;
}
节点标记设计
为每个列表项添加绝对定位的圆点标记。通过nth-child伪类可交替改变标记颜色。
.timeline li {
position: relative;
margin-bottom: 2rem;
}
.timeline li::before {
content: '';
position: absolute;
left: -20px;
top: 5px;
width: 12px;
height: 12px;
border-radius: 50%;
background: #3498db;
}
.timeline li:nth-child(odd)::before {
background: #e74c3c;
}
内容区域样式
为内容添加卡片效果,包括阴影、内边距和圆角。通过调整位置使内容与时间轴对齐。

.timeline .content {
padding: 1rem;
background: white;
border-radius: 8px;
box-shadow: 0 3px 10px rgba(0,0,0,0.1);
margin-left: 1rem;
}
响应式调整
在小屏幕设备上调整时间轴布局,将线条和标记移动到内容区域下方。
@media (max-width: 768px) {
.timeline::before {
left: 0;
top: auto;
bottom: 0;
width: 100%;
height: 2px;
}
.timeline li::before {
left: 50%;
top: auto;
bottom: -6px;
transform: translateX(-50%);
}
.timeline .content {
margin-left: 0;
margin-bottom: 2rem;
}
}
动画效果增强
为时间轴节点添加入场动画,创建渐进显示效果。
.timeline li {
opacity: 0;
animation: fadeIn 0.5s forwards;
}
@keyframes fadeIn {
to {
opacity: 1;
}
}
.timeline li:nth-child(1) { animation-delay: 0.1s }
.timeline li:nth-child(2) { animation-delay: 0.3s }
.timeline li:nth-child(3) { animation-delay: 0.5s }






