# 用CSS实现各种形状|博客系列

> 本文详细介绍了如何使用CSS绘制各种常见图形，包括扇形、三角形等，提供了完整的CSS代码和实现思路，适合前端开发者学习和参考。

- 原文：https://feinterview.poetries.top/docs/fe-blog-docs/blog-docs/css/-%E7%94%A8CSS%E5%AE%9E%E7%8E%B0%E5%90%84%E7%A7%8D%E5%BD%A2%E7%8A%B6
- 来源：前端进阶之旅（feinterview.poetries.top）
- 更新：2025.10.25

---

## 用CSS实现各种形状

### 如何画扇形？

```css
.sector {
  width: 0;
  height: 0;
  border: 100px solid red;
  border-color: red transparent transparent transparent;
  border-radius: 50%;
}
/*或者*/
.sector {
  width: 100px;
  height: 100px;
  border: 100px solid transparent;
  border-top-color: red;
  box-sizing: border-box; /* 这步很重要 */
  border-radius: 50%;
}
```

**HTML代码：**

```html
<div class="sector"></div>
```

### 如何画三角形？

```css
.triangle {
  width: 0;
  height: 0;
  border: 100px solid red;
  border-color: red transparent transparent transparent;
}
/*或者*/
.triangle {
  width: 100px;
  height: 100px;
  border: 100px solid transparent;
  border-top-color: red;
  box-sizing: border-box;
}
```

**HTML代码：**

```html
<div class="triangle"></div>
```

### 圆？半圆？椭圆？

```css
div {
  width: 100px;
  height: 100px;
  background-color: red;
  margin-top: 20px;
}
.box1 { /* 圆 */
  /* border-radius: 50%; */
  border-radius: 50px;
}
.box2 { /* 半圆 */
  height: 50px;
  border-radius: 50px 50px 0 0;
}
.box3 { /* 椭圆 */
  height: 50px;
  border-radius: 50px/25px; /* x轴/y轴 */
}
```

**HTML代码：**

```html
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
```
