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;
}
相关推荐
00后程序员张27 分钟前
Fiddler抓包工具使用教程,代理设置与调试方法实战解析(含配置技巧)
前端·测试工具·ios·小程序·fiddler·uni-app·webview
2301_768350236 小时前
Vue第二期:组件及组件化和组件的生命周期
前端·javascript·vue.js
华洛7 小时前
公开一个AI产品的商业逻辑与设计方案——AI带来的涂色卡自由
前端·后端·产品
明远湖之鱼7 小时前
opentype.js 使用与文字渲染
前端·svg·字体
90后的晨仔8 小时前
Vue 3 组合式函数(Composables)全面解析:从原理到实战
前端·vue.js
今天头发还在吗8 小时前
【React】动态SVG连接线实现:图片与按钮的可视化映射
前端·javascript·react.js·typescript·前端框架
小刘不知道叫啥8 小时前
React 源码揭秘 | suspense 和 unwind流程
前端·javascript·react.js
szial8 小时前
为什么 React 推荐 “不可变更新”:深入理解 React 的核心设计理念
前端·react.js·前端框架
mapbar_front8 小时前
面试是一门学问
前端·面试
90后的晨仔9 小时前
Vue 3 中 Provide / Inject 在异步时不起作用原因分析(二)?
前端·vue.js