← 返回完整题库
CSS简单

怎么使用 CSS 如何画一个三角形

参考答案

border 技巧

.triangle {
  width: 0; height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 86px solid #1677ff;   /* 朝上 */
}

原理:宽高为 0 时,border 交界处形成斜边,把其余三边设为透明即只剩一个三角。

其他方案

.clip { width:100px; height:100px; background:#1677ff;
  clip-path: polygon(50% 0, 100% 100%, 0 100%); }
  • clip-path 更灵活,可做任意多边形
  • 也可用 conic-gradient 或 SVG 绘制

本题来自 计算机知识面试题库,共 921 道题。