纯css制作带三角(兼容所有浏览器)
如何用 border 来制作三角。
html 代码如下:
代码如下:
1 2 3 4 | < div class="arrow-up"></ div > < div class="arrow-down"></ div > < div class="arrow-left"></ div > < div class="arrow-right"></ div > |
css 代码如下:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | .arrow-up { width : 0 ; height : 0 ; border-left : 5px solid transparent ; border-right : 5px solid transparent ; _border-left : 5px solid white ; _border-right : 5px solid white ; border-bottom : 5px solid black ; overflow : hidden ; } .arrow-down { width : 0 ; height : 0 ; border-left : 20px solid transparent ; border-right : 20px solid transparent ; _border-left : 20px solid white ; _border-right : 20px solid white ; border-top : 20px solid #f00 ; overflow : hidden ; } .arrow- right { width : 0 ; height : 0 ; border-top : 60px solid transparent ; border-bottom : 60px solid transparent ; _border-top : 60px solid white ; _border-bottom : 60px solid white ; border-left : 60px solid green ; overflow : hidden ; } .arrow- left { width : 0 ; height : 0 ; border-top : 10px solid transparent ; border-bottom : 10px solid transparent ; _border-top : 10px solid white ; _border-bottom : 10px solid white ; border-right : 10px solid blue ; overflow : hidden ; } |
效果图如下:
首先附上效果图:
以上的效果完全是用 css 来实现的,那么是怎么实现的呢?
我们知道 html 中有一些特殊的字符,具体的请点击 HTML特殊字符大全。
通过特殊字符,利用 css 中的 margin 或者 position 方法完全可以实现以上效果。
代码如下:
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>字符制作三角边框</title> <style type="text/css"> *{margin:0;padding:0;} body{font-family:SimSun;} h1{text-align:center;} .demo1{width:300px;margin:20px auto;border:1px solid red;height:100px;} .demo1 em,.demo1 span{display:block;width:30px;height:16px;font-size:30px;overflow:hidden;_position:relative;margin-left:10px;} .demo1 em{margin-top:-16px;color:red;font-style:normal;} .demo1 span{margin-top:-14px;color:white;} .demo2{width:300px;border:1px solid #F00;height:100px;position:relative;margin-left:auto;margin-right:auto;} .demo2 em,.demo2 span{font-style:normal;font-size:30px;position:absolute;left:-16px;top:40px;color:red;} .demo2 span{left:-14px;color:white;} </style> </head> <body> <h1>demo1 是用 margin 方法</h1> <div class="demo1"> <em>◆</em> <span>◆</span> </div> <h1>demo2 是用 position 方法</h1> <div class="demo2"> <em>◆</em> <span>◆</span> </div> </body> </html>
分类:
前端
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞