当前位置:首页 > CSS

css制作时间轴

2026-04-01 00:31:21CSS

时间轴的基本结构

使用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;
  }
}

动画效果增强

为时间轴节点添加入场动画,创建渐进显示效果。

css制作时间轴

.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 }

标签: 时间css
分享给朋友:

相关文章

css如何制作圆

css如何制作圆

使用 border-radius 属性制作圆 通过设置 border-radius 属性为元素添加圆角效果。当 border-radius 的值等于元素宽度和高度的一半时,元素会呈现圆形。 .cir…

如何制作css

如何制作css

创建CSS文件 新建一个纯文本文件,将文件扩展名改为.css,例如style.css。确保文件名不含空格或特殊字符,使用连字符或下划线分隔单词。 编写CSS语法 CSS规则由选择器和声明块组成。选择…

如何制作css导航菜单

如何制作css导航菜单

创建基本HTML结构 导航菜单的基础是HTML结构。使用<nav>标签包裹无序列表<ul>,每个菜单项用<li>标签表示,链接用<a>标签。 <…

css背景图片制作

css背景图片制作

使用CSS设置背景图片 在CSS中,可以通过background-image属性为元素添加背景图片。以下是一个基本示例: .element { background-image: url('pa…

页脚制作 css

页脚制作 css

页脚基础样式 使用CSS为页脚设置基础样式,通常包括背景色、文字颜色、内边距等属性。以下是一个常见示例: footer { background-color: #333; color: wh…

css制作箭头

css制作箭头

使用边框制作箭头 通过设置元素的 border 属性,利用透明边框和实色边框的组合生成箭头。例如,创建一个向右的箭头: .arrow-right { width: 0; height:…