当前位置:首页 > CSS

用css制作火车图标

2026-02-12 16:39:42CSS

使用CSS制作火车图标

通过纯CSS可以绘制简单的火车图标,主要利用::before::after伪元素和边框属性构建车厢、车轮等部件。以下是实现方案:

基础结构

HTML部分只需一个容器元素:

用css制作火车图标

<div class="train"></div>

CSS样式设计

.train {
  position: relative;
  width: 120px;
  height: 60px;
  background: #FF5733;
  border-radius: 10px;
  margin: 50px auto;
}

/* 火车头部 */
.train::before {
  content: '';
  position: absolute;
  left: -20px;
  top: 10px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 20px 20px 20px 0;
  border-color: transparent #FF5733 transparent transparent;
}

/* 车窗 */
.train::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 20px;
  background: #85C1E9;
  border-radius: 5px;
  left: 15px;
  top: 10px;
}

/* 车轮 */
.wheel {
  position: absolute;
  width: 20px;
  height: 20px;
  background: #333;
  border-radius: 50%;
  bottom: -10px;
}

.wheel.left { left: 20px; }
.wheel.right { right: 20px; }

完整实现代码

<!DOCTYPE html>
<html>
<head>
<style>
.train {
  position: relative;
  width: 120px;
  height: 60px;
  background: #FF5733;
  border-radius: 10px;
  margin: 50px auto;
}

.train::before {
  content: '';
  position: absolute;
  left: -20px;
  top: 10px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 20px 20px 20px 0;
  border-color: transparent #FF5733 transparent transparent;
}

.train::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 20px;
  background: #85C1E9;
  border-radius: 5px;
  left: 15px;
  top: 10px;
}

.wheel {
  position: absolute;
  width: 20px;
  height: 20px;
  background: #333;
  border-radius: 50%;
  bottom: -10px;
}

.wheel.left { left: 20px; }
.wheel.right { right: 20px; }
</style>
</head>
<body>
<div class="train">
  <div class="wheel left"></div>
  <div class="wheel right"></div>
</div>
</body>
</html>

进阶优化

增加烟囱和连接器:

用css制作火车图标

/* 烟囱 */
.chimney {
  position: absolute;
  width: 10px;
  height: 25px;
  background: #333;
  top: -25px;
  left: 80px;
}

/* 车厢连接器 */
.connector {
  position: absolute;
  width: 10px;
  height: 5px;
  background: #333;
  right: -10px;
  top: 25px;
}

多节车厢版本

通过复制HTML结构并调整位置即可实现多节车厢:

<div class="train-container">
  <div class="train engine">
    <div class="wheel left"></div>
    <div class="wheel right"></div>
    <div class="chimney"></div>
    <div class="connector"></div>
  </div>
  <div class="train carriage">
    <div class="wheel left"></div>
    <div class="wheel right"></div>
  </div>
</div>

对应的CSS调整:

.train-container {
  display: flex;
  align-items: center;
}

.carriage {
  margin-left: 30px;
  background: #3498DB;
}

这种方法通过组合基本的CSS形状属性,无需图片即可创建矢量风格的火车图标,适合需要轻量级图标的场景。

标签: 图标火车
分享给朋友:

相关文章

css图标制作

css图标制作

CSS 图标制作方法 使用 Unicode 或图标字体 通过引入字体库(如 Font Awesome)或直接使用 Unicode 符号生成图标。例如,Font Awesome 的图标可通过类名调用:…

制作css 图标

制作css 图标

使用 CSS 制作图标的方法 纯 CSS 绘制基础形状 通过 CSS 的 border、transform 和伪元素可以绘制简单图标,例如三角形、圆形或箭头: .triangle { width…

uniapp滑动图标

uniapp滑动图标

实现滑动图标的方法 在UniApp中实现滑动图标功能,可以通过<scroll-view>组件结合自定义样式完成。以下是一个常见的实现方式: 模板部分 <scroll-view s…

uniapp 图标角标

uniapp 图标角标

uniapp 图标角标实现方法 在 uniapp 中实现图标角标功能,可以通过以下几种方式实现: 使用 uni.setTabBarBadge API 适用于底部 tabBar 的角标设置,支持动态…

图标 css 制作

图标 css 制作

使用CSS制作图标的方法 使用Unicode字符或字体图标 通过CSS的content属性或字体图标库(如Font Awesome)直接插入图标。例如使用Font Awesome: <link…

图标制作css

图标制作css

使用 CSS 制作图标的方法 纯 CSS 绘制简单图标 通过 CSS 的 border、transform 和伪元素可以创建基本图标: .arrow-icon { width: 0; hei…