当前位置:首页 > 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形状属性,无需图片即可创建矢量风格的火车图标,适合需要轻量级图标的场景。

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

相关文章

动画实现react图标

动画实现react图标

使用CSS动画实现React图标旋转 安装React Icons库(如未安装): npm install react-icons 引入所需图标和CSS模块: import { FaReact }…

图标 css 制作

图标 css 制作

使用CSS制作图标的方法 CSS可以通过多种方式制作图标,以下是几种常见的方法: 使用Unicode字符或字体图标 Unicode字符或字体图标库(如Font Awesome)可以直接通过CSS引入…

制作css图标

制作css图标

使用CSS制作图标的方法 纯CSS绘制基础形状 通过CSS的border、width、height等属性可以创建简单图标: .arrow { width: 0; height: 0; b…

在线制作css图标

在线制作css图标

使用在线工具制作CSS图标 方法1:利用CSS图标生成器(如CSS.gg) 访问 CSS.gg 或其他类似工具,选择需要的图标,直接复制生成的CSS代码。这些工具通常提供纯CSS实现的图标,无需依赖图…

jquery图标

jquery图标

以下是关于jQuery图标的几种常见实现方式及相关资源: 使用jQuery UI内置图标 jQuery UI提供了一套内置的图标集,可通过CSS类名直接调用。例如使用ui-icon基类配合具体图标类…

css3 图标制作

css3 图标制作

使用CSS3制作图标的方法 纯CSS绘制基础形状 通过CSS的border、transform、box-shadow等属性组合可创建简单图标。例如绘制三角形: .triangle { width…