IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

用CSS绘制各种角度的三角形

叶德华的博客 2015-11-08 21:50:23 累计浏览 1,812 次
本机暂存

   下面是一些利用CSS中border的特性绘制的各种角度的三角形:

   (1)正方形

正方形

图1:正方形

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-left: 50px solid gray;
        border-right : 50px solid green;
        border-bottom: 50px solid red;
        border-top : 50px solid yellow;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   如上图1,由于设置了width、height都为0了,所以四个点也就归并成了一个点,后面的各种角度的三角形,其实也就是不断调整那个中心点而已。

   (2)向上三角

向上三角

图2:向上三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-left: 50px solid transparent;
        border-right : 50px solid transparent;
        border-bottom: 100px solid red;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (3)向下三角

向下三角

图3:向下三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-left: 50px solid transparent;
        border-right : 50px solid transparent;
        border-top: 100px solid red;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (4)向左三角

向左三角

图4:向左三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-right: 100px solid red;
        border-top : 50px solid transparent;
        border-bottom: 50px solid transparent;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (5)向右三角

向右三角

图5:向右三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-left: 100px solid red;
        border-top : 50px solid transparent;
        border-bottom: 50px solid transparent;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (6)左上三角

左上三角

图6:左上三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-top : 100px solid red;
        border-right: 100px solid transparent;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (7)右上三角

右上三角

图7:右上三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-top : 100px solid red;
        border-left: 100px solid transparent;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (8)左下三角

左下三角

图8:左下三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-bottom : 100px solid red;
        border-right: 100px solid transparent;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

   (9)右下三角

右下三角

图9:右下三角

   代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
<style type="text/css">
    #triangle {
        width : 0;
        height: 0;
        border-bottom : 100px solid red;
        border-left: 100px solid transparent;
    }
</style>
</head>
<body>
<div id="triangle"></div>
</body>
</html>

同分类推荐文章

  1. translateZ() (2026-06-25 21:18:56)
  2. translateY() (2026-06-25 21:17:56)
  3. translateX() (2026-06-25 21:16:01)

查看更多 前端 文章 →

建议继续学习

  1. 50个活力和动感的网页设计-颜色的灵感 (累计阅读 34,444)
  2. 视觉设计前瞻实用性研究(PNVD) 第二期 (累计阅读 12,980)
  3. 各公司对前端开发的职位描述 (累计阅读 10,407)
  4. iframe大小自适应 (累计阅读 10,061)
  5. 浏览器的渲染原理简介 (累计阅读 8,378)
  6. 解决IE6从Nginx服务器下载图片不Cache的Bug (累计阅读 8,358)
  7. 程序员眼里IE浏览器是什么样的 (累计阅读 8,012)
  8. 2010网页设计趋势 (累计阅读 7,820)
  9. Web前端工程师编程能力飞升之路 (累计阅读 7,695)
  10. 颜色的代码表达式 (累计阅读 7,668)