当前位置:首页 > 工具 > 正文内容

CSS3不规则图形绘制技巧大全

访客 工具 2026年7月16日 1

本文收集了利用CSS3实现各种不规则图形的代码示例,涵盖从基本几何到复杂图案。以下示例均基于纯CSS实现。

三角形变体

核心原理:将元素的宽高设为0,利用边框的透明与不透明组合形成三角形。


/* 朝上三角形 */
.tri-up {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid #ff0000;
}

/* 朝下三角形 */
.tri-down {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid #ff0000;
}

/* 朝左三角形 */
.tri-left {
  width: 0;
  height: 0;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
  border-right: 100px solid #ff0000;
}

/* 朝右三角形 */
.tri-right {
  width: 0;
  height: 0;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
  border-left: 100px solid #ff0000;
}

/* 左上角三角形 */
.tri-tl {
  width: 0;
  height: 0;
  border-right: 100px solid transparent;
  border-top: 100px solid #ff0000;
}

/* 右上角三角形 */
.tri-tr {
  width: 0;
  height: 0;
  border-left: 100px solid transparent;
  border-top: 100px solid #ff0000;
}

/* 左下角三角形 */
.tri-bl {
  width: 0;
  height: 0;
  border-right: 100px solid transparent;
  border-bottom: 100px solid #ff0000;
}

/* 右下角三角形 */
.tri-br {
  width: 0;
  height: 0;
  border-left: 100px solid transparent;
  border-bottom: 100px solid #ff0000;
}

圆形与椭圆系列

通过border-radius和box-shadow可实现多种圆形变体。


/* 标准圆形 */
.circle {
  width: 100px;
  height: 100px;
  background: #ff0000;
  border-radius: 50%;
}

/* 椭圆 */
.elipse {
  width: 200px;
  height: 100px;
  background: #ff0000;
  border-radius: 100px / 50px;
}

/* 扇形 */
.sect {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid #ff0000;
  border-radius: 50%;
}

/* 圆环 */
.ring-shape {
  width: 100px;
  height: 100px;
  border: 5px solid #ff0000;
  border-radius: 50%;
}

/* 月牙 */
.moon {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  box-shadow: 20px 20px 0 0 #ff0000;
}

四边形与梯形

利用transform和border可实现平行四边形、梯形等。


/* 正方形 */
.square-box {
  width: 100px;
  height: 100px;
  background: #ff0000;
}

/* 长方形 */
.rect-box {
  width: 200px;
  height: 100px;
  background: #ff0000;
}

/* 菱形 */
.diamond-shape {
  width: 100px;
  height: 100px;
  background: #ff0000;
  transform: rotate(45deg);
}

/* 平行四边形 */
.para-shape {
  width: 200px;
  height: 100px;
  background: #ff0000;
  transform: skew(-20deg);
}

/* 普通梯形 */
.trap-shape {
  width: 100px;
  height: 0;
  border-bottom: 100px solid #ff0000;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
}

多边图形

通过组合多个CSS元素(border、伪元素)构造复杂多边形。


/* 五边形:梯形+三角形组合 */
.penta-shape {
  width: 60px;
  height: 0;
  position: relative;
  border-top: 55px solid #ff0000;
  border-left: 25px solid transparent;
  border-right: 25px solid transparent;
}
.penta-shape::before {
  content: "";
  position: absolute;
  width: 0; height: 0;
  border-left: 55px solid transparent;
  border-right: 55px solid transparent;
  border-bottom: 35px solid #ff0000;
  left: -25px; top: -90px;
}

/* 六边形:矩形+两个三角形 */
.hexa-shape {
  width: 100px; height: 55px;
  background: #ff0000;
  position: relative;
}
.hexa-shape::before {
  content: "";
  width: 0; height: 0;
  position: absolute;
  top: -25px; left: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 25px solid #ff0000;
}
.hexa-shape::after {
  content: "";
  width: 0; height: 0;
  position: absolute;
  bottom: -25px; left: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 25px solid #ff0000;
}

/* 八边形:矩形+两个梯形 */
.octa-shape {
  width: 100px; height: 100px;
  background: #ff0000;
  position: relative;
}
.octa-shape::before {
  content: "";
  position: absolute;
  width: 42px; height: 0;
  border-left: 29px solid #fff;
  border-right: 29px solid #fff;
  border-bottom: 30px solid #ff0000;
  top: 0; left: 0;
}
.octa-shape::after {
  content: "";
  position: absolute;
  width: 42px; height: 0;
  border-left: 29px solid #fff;
  border-right: 29px solid #fff;
  border-top: 30px solid #ff0000;
  bottom: 0; left: 0;
}

图案与符号

这些示例展示了利用CSS实现爱心、星形、对话框等常见图标。


/* 爱心 */
.heart {
  position: relative;
}
.heart::before,
.heart::after {
  content: "";
  width: 70px; height: 110px;
  background: #ff0000;
  position: absolute;
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
}
.heart::before {
  transform: rotate(45deg);
}
.heart::after {
  transform: rotate(-45deg);
  left: -30px;
}

/* 无穷符号 */
.loop-shape {
  width: 190px; height: 100px;
  position: relative;
}
.loop-shape::before,
.loop-shape::after {
  content: "";
  width: 50px; height: 50px;
  position: absolute;
  border: 20px solid #ff0000;
  border-radius: 50px 50px 0 50px;
}
.loop-shape::before {
  top: 0; left: 0;
  transform: rotate(-45deg);
}
.loop-shape::after {
  top: 0; right: 0;
  border-radius: 50px 50px 50px 0;
  transform: rotate(45deg);
}

/* 鸡蛋形状 */
.egg-shape {
  width: 120px; height: 180px;
  background: #ff0000;
  border-radius: 60% 60% 60% 60% / 70% 70% 50% 50%;
}

/* 吃豆人 */
.pac-shape {
  width: 0; height: 0;
  border: 60px solid #ff0000;
  border-right: 60px solid transparent;
  border-radius: 100%;
}

/* 对话框:圆角矩形+三角形 */
.dialog-box {
  width: 150px; height: 100px;
  background: #ff0000;
  border-radius: 10px;
  position: relative;
}
.dialog-box::before {
  content: "";
  width: 0; height: 0;
  position: absolute;
  left: -20px; top: 40px;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-right: 20px solid #ff0000;
}

/* 钻石形态:梯形+三角形 */
.diamond-gem {
  width: 50px; height: 0;
  position: relative;
  border-bottom: 25px solid #ff0000;
  border-left: 25px solid transparent;
  border-right: 25px solid transparent;
}
.diamond-gem::before {
  content: "";
  width: 0; height: 0;
  position: absolute;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 70px solid #ff0000;
  left: -25px; top: 25px;
}

/* 五角星 */
.star-5 {
  width: 0; height: 0;
  position: relative;
  border-left: 80px solid transparent;
  border-right: 80px solid transparent;
  border-bottom: 60px solid #ff0000;
  transform: rotate(35deg);
}
.star-5::before {
  content: "";
  position: absolute;
  width: 0; height: 0;
  border-left: 80px solid transparent;
  border-right: 80px solid transparent;
  border-bottom: 60px solid #ff0000;
  transform: rotate(-70deg);
  top: 3px; left: -80px;
}
.star-5::after {
  content: "";
  position: absolute;
  width: 0; height: 0;
  border-bottom: 60px solid #ff0000;
  border-right: 20px solid transparent;
  border-left: 20px solid transparent;
  transform: rotate(-35deg);
  top: -40px; left: -49px;
}

/* 六角星:两个三角形 */
.star-6 {
  width: 0; height: 0;
  position: relative;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid #ff0000;
}
.star-6::before {
  content: "";
  width: 0; height: 0;
  position: absolute;
  top: 30px; left: -50px;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid #ff0000;
}

/* 八角星:两个正方形旋转 */
.star-8 {
  width: 100px; height: 100px;
  background: #ff0000;
  position: relative;
}
.star-8::before {
  content: "";
  width: 100px; height: 100px;
  background: #ff0000;
  position: absolute;
  top: 0; left: 0;
  transform: rotate(45deg);
}

/* 十二角星:三个正方形 */
.star-12 {
  width: 100px; height: 100px;
  background: #ff0000;
  position: relative;
}
.star-12::before,
.star-12::after {
  content: "";
  width: 100px; height: 100px;
  background: #ff0000;
  position: absolute;
  top: 0; left: 0;
}
.star-12::before {
  transform: rotate(-30deg);
}
.star-12::after {
  transform: rotate(30deg);
}

以上所有图形均基于CSS3的border、border-radius、transform、伪元素等特性实现,无需额外图片资源。实际使用时可根据设计需求调整尺寸和颜色。

相关文章

Trojan服务器搭建与配置

一、整体架构(先对齐认知)Clash Meta (PC / iOS / Android)        ↓ TLS   Trojan Server (443)        ↓     InternetTrojan 的核心是: TLS + HTTPS 流量伪装 看起来像正常网站 非常适合...

Tailscale 的详细用法

Tailscale 是一种基于 WireGuard 协议 的 零配置 VPN(虚拟私有网络)服务,让设备之间能够 安全、加密地直接连接,就像它们在同一个本地网络一样。它的核心特点是 简单、安全、跨平台。Tailscale 非常适合 没有公网 IP、两台电脑不在同一局域网 的场景。 简单来说,Tailscale 是什么?Tailscale 是一款让你的各种设备(电脑、服务器、手机...

Clash Tun 模式 导致 爱快(iKuai SD-Wan)内网域名无法访问

一、Clash  DNS 配置dns:  enable: true  listen: 0.0.0.0:53  ipv6: true  enhanced-mode: redir-host  nameserver:    - 223.5.5.5    - 223.6.6.6iKuai 内网域名 ...

深入解析Node.js运行环境与异步I/O架构

深入解析Node.js运行环境与异步I/O架构

核心定义与价值Node.js本质上是一个JavaScript运行环境,而非编程语言或应用框架。它赋予了JavaScript脱离浏览器在服务端、命令行工具及网络应用中执行的能力。其核心意义在于:用单一语言打通前后端开发壁垒。基于事件驱动与非阻塞I/O的架构特性,Node.js在处理API网关、实时通信及微服务等I/O密集型场景时表现卓越,已成为现代后端工程的主流选择。浏览器沙箱限制1995年Java...

ADO.NET SQL参数化查询的最佳实践

在 ADO.NET 中执行 SQL 查询时,参数化查询是一种关键的安全措施和性能优化手段。它通过将 SQL 命令和用户提供的数据分开处理,有效防止了 SQL 注入攻击,并有助于数据库缓存执行计划。下面总结了几种常用的参数化查询方式。 1. 使用 SqlParameter 对象(推荐) 这是最推荐的参数化查询方式。通过显式创建 SqlParameter 对象,您可以精确控制参数的类...

基于ELK的日志集中化分析系统搭建

构建统一日志管理平台的必要性 在分布式架构中,各服务节点独立运行,日志分散存储于不同主机。传统通过命令行工具如grep、awk逐个检索日志的方式,在数据量庞大时效率极低,难以实现快速定位问题。为提升运维效率,需建立集中式日志处理体系,具备日志采集、传输、存储、分析与告警能力。 ELK技术栈核心组件解析 Elasticsearch:分布式搜索引擎,支持全文检索、实时数据分析和高可用集群部署,...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。