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;
}
相关推荐
!win !1 小时前
前端跨标签页通信方案(下)
前端·javascript
f***45321 小时前
基于SpringBoot和PostGIS的各省与地级市空间距离分析
android·前端·后端
编码追梦人1 小时前
从 “手忙脚乱“ 到 “行云流水“:华为云 DevUI 与 MateChat 如何让前端开发飞起来
前端·华为云
S***H2832 小时前
前端动画实现经验,性能优化与兼容性
前端
xw52 小时前
前端跨标签页通信方案(下)
前端·javascript
zzlyx993 小时前
IoTSharp前端VUE采用npm run build编译提示require() of ES Module 出错
前端·vue.js·npm
全栈技术负责人3 小时前
拒绝“无法复现”:前端全链路日志排查实战手册
前端·全链路·问题排查思路
加洛斯3 小时前
前端小知识003:JS中 == 与 === 的区别
开发语言·前端·javascript
b***9103 小时前
【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus
android·前端·后端·mybatis
G***E3163 小时前
前端路由懒加载实现,Vue Router与React Router
前端·vue.js·react.js