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

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

相关文章

vue实现图标加文字

vue实现图标加文字

Vue 实现图标加文字的几种方法 在 Vue 中实现图标加文字的常见方式有多种,可以根据项目需求和使用的图标库选择合适的方法。 使用 Element UI 或 Ant Design Vue 等 UI…

vue如何实现图标管理

vue如何实现图标管理

Vue 图标管理方案 使用第三方图标库 Vue项目可以集成第三方图标库如Font Awesome、Element UI的图标或Ant Design的图标。安装对应库后,通过组件直接调用图标。 npm…

用css制作火车图标

用css制作火车图标

使用CSS制作火车图标 火车图标可以通过CSS的伪元素、边框和背景属性组合实现。以下是一个简单的火车图标实现方法: 火车主体部分 .train { width: 120px; hei…

图标css制作

图标css制作

使用CSS制作图标的方法 内联SVG结合CSS 将SVG代码直接嵌入HTML,通过CSS控制样式和动画: <svg class="icon" viewBox="0 0 24 24">…

图标制作css

图标制作css

使用 CSS 制作图标的方法 CSS 可以用于创建矢量图标,通常通过伪元素、边框和变换属性实现。以下是几种常见的方法: 使用伪元素和边框 通过 ::before 或 ::after 伪元素结合边框…

jquery 图标

jquery 图标

以下是关于 jQuery 图标的相关信息,包括获取方式和使用方法: 获取 jQuery 图标 jQuery 官方图标通常指 jQuery 的 Logo 或相关 UI 图标库。可通过以下途径获取:…