CSS绘制各种三角形

思路

从盒模型来看,如果content宽高为0, border有宽度,那么上下左右的border就会填充满整个盒子,于是四个方向的border都是三在这里插入图片描述 角形形状,这样想要哪边就显示哪边

效果

代码

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="index.css">
    <script src="/index.js"></script>
    <title>CSS三角形</title>
</head>
<body>
    <div class="CENTER flex_box">
        <div class="tri no_cotent tri_bottom"></div>
        <div class="tri no_cotent tri_bottom_border"></div>
        <div class="tri no_cotent around"></div>
        <div class="tri content around"></div>
    </div>
</body>
</html>

CSS

css 复制代码
.CENTER{
    position: absolute;
    left: 50%;
    top: 50%;
    width: 0;
    height: 0;
    transform: translate(-50%, -50%);
}
.flex_box{
    display: flex;
    width: 500px;
    align-items: center;
    justify-content: space-between;

}
.tri{
    margin: 0;
    padding: 0;
    border-width: 50px;
    border-color: transparent;
    border-style: solid;
}
.content{
    width: 50px;
    height: 50px;
}
/* 如果要绘制三角形, 那么content部分需要设置宽高为0, 只用border填充box */
.no_cotent{
    width: 0;
    height: 0;
}
/* 下三角形 */
.tri_bottom{
    border-bottom-color: rgb(66, 210, 218);
}
/* 带边框下三角形, 思路是两个三角形重叠, 使用伪元素实现边框 */
.tri_bottom_border{
    border-bottom-color: rgb(255, 255, 255);
    border-bottom-style: dashed;
}
.tri_bottom_border::after{
    position: relative;
    content: '';
    padding: 0;
    top: -28px;
    left: -60.5px;
    z-index: -1;
    border-style: solid;
    border-bottom-color: rgb(227, 193, 255) !important;
    border-width: 60px;
    border-color: transparent;
}
/* 三角形四周环绕 */
.around{
    border-left-color: red;
    border-right-color: blue;
    border-top-color: aqua;
    border-bottom-color: orange;
}
相关推荐
掘金安东尼12 分钟前
更多审查智能体,更少盲区:GitHub Copilot 代码审查是怎么做的
前端·github copilot
Hilaku2 小时前
2026 年了,jQuery 真的该死了吗?
前端·javascript·程序员
zach3 小时前
吃透 React setState 同步异步之谜:从批量更新、事件循环到版本演进的完整拆解
前端·react.js
不服老的小黑哥3 小时前
02-从 pnpm dev 到 Spring Boot 启动:后端服务到底怎么跑起来?
前端
htzyl2063 小时前
javaScript——this 的四种绑定:为什么箭头函数改不了它
前端·javascript
欧雷殿3 小时前
做自己的第一位客户:一人公司的数据治理公开实践
前端·ai编程·团队管理
掘金安东尼4 小时前
换上 Gemini 4 Neon 之后,我改了哪些上下文工程大模型
前端
涛涛ing4 小时前
我把项目里的 Zustand 全删了:代码量减半,Bug 少了,性能还起飞了
前端
map_3d_vis5 小时前
百度地图地图类型切换:普通地图、卫星图、混合图
前端·javascript·地图·百度地图
圆心角16 小时前
代码也有折旧率
前端·程序员