polygon()

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

polygon() CSS 函数是 <basic-shape> 数据类型 之一。它通过提供一个或多个坐标对(每个坐标对代表形状的一个顶点)来绘制 多边形

试一试

clip-path: polygon(
  0% 20%,
  60% 20%,
  60% 0%,
  100% 50%,
  60% 100%,
  60% 80%,
  0% 80%
);
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element"></div>
</section>
#default-example {
  background: #ffee99;
}

#example-element {
  background: linear-gradient(to bottom right, #ff5522, #0055ff);
  width: 100%;
  height: 100%;
}

语法

css
/* Specified as coordinate list */
/* polygon(<length-percentage> <length-percentage>, ... )*/
polygon(50% 2.4%, 34.5% 33.8%, 0% 38.8%, 25% 63.1%, 19.1% 97.6%)
polygon(0px 0px, 200px 100px, 0px 200px)
polygon(0% 0px, 100% 100px, 0px 100%)
polygon(0 0, 50% 1rem, 100% 2vw, calc(100% - 20px) 100%, 0 100%)

/* Specified as coordinate list and fill rule*/
/* polygon(<fill-rule> <length-percentage> <length-percentage>, ... )*/
polygon(nonzero, 0% 0%, 50% 50%, 0% 100%)
polygon(evenodd, 0% 0%, 50% 50%, 0% 100%)

polygon() 参数用逗号和可选的空白字符分隔。第一个参数是可选的 <fill-rule> 值。附加参数是定义多边形的点。每个点都是一对 x/y 坐标 <length-percentage> 值,用空格分隔,例如,左上角和右下角分别为“0 0”和“100% 100%”。

注意:SVG <polygon> 元素对 fill-rulepoints 有单独的属性,并且 points 对空格和逗号分隔符的使用很灵活。CSS polygon() 对分隔符的规则是严格执行的。

参数

<fill-rule> 可选

可选值 nonzero(省略时为默认值)或 evenodd,用于指定填充规则。

<length-percentage>

多边形的每个顶点都由一对 <length-percentage> 值表示,这些值给出顶点相对于形状的 参考框 的 x/y 坐标。

返回值

返回一个 <basic-shape> 值。

描述

你可以通过指定点的坐标来使用 polygon() 函数创建几乎任何形状。定义点的顺序很重要,并且可能导致不同的形状。polygon() 函数至少需要 3 个点,这将创建一个三角形,但没有上限。

polygon() 函数接受逗号分隔的坐标或点作为其值。每个点由一对用空格分隔的 xy 值表示,这些值表示点在多边形内的坐标。

polygon(x1 y1, x2 y2, x3 y3, x4 y4, xn yn)

鉴于上述情况,容器坐标的映射可以可视化为

点 1 点 2 点 3 点 4 点 n
x 0% 100% 100% 0% xn
y 0% 0% 100% 100% yn

使用 polygon() 函数将这些坐标应用于 CSS clip-path 属性

css
clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);

这将通过指定其四个角的坐标(左上角 (0% 0%)、右上角 (100% 0%)、右下角 (100% 100%) 和左下角 (0% 100%))创建一个与其父内容大小相同的矩形形状。

正式语法

<polygon()> = 
polygon( <'fill-rule'>? [ round <length> ]? , [ <length-percentage> <length-percentage> ]# )

<fill-rule> =
nonzero |
evenodd

<length-percentage> =
<length> |
<percentage>

示例

创建一个三角形

在此示例中,通过定义其三个点的坐标形成一个三角形。

HTML

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

CSS

css
.triangle {
  width: 400px;
  height: 400px;
  background-color: magenta;
  clip-path: polygon(100% 0%, 50% 50%, 100% 100%);
}

结果

三角形的坐标是容器的右上角 (100% 0%)、中心点 (50% 50%) 和右下角 (100% 100%)。

为 shape-outside 设置多边形

在此示例中,使用 shape-outside 属性创建了一个用于文本环绕的形状。

html
<div class="box">
  <div class="shape"></div>
  <p>
    One November night in the year 1782, so the story runs, two brothers sat
    over their winter fire in the little French town of Annonay, watching the
    grey smoke-wreaths from the hearth curl up the wide chimney. Their names
    were Stephen and Joseph Montgolfier, they were papermakers by trade, and
    were noted as possessing thoughtful minds and a deep interest in all
    scientific knowledge and new discovery. Before that night—a memorable night,
    as it was to prove—hundreds of millions of people had watched the rising
    smoke-wreaths of their fires without drawing any special inspiration from
    the fact.
  </p>
</div>
css
.box {
  width: 250px;
}

.shape {
  float: left;
  shape-outside: polygon(
    0 5%,
    15% 12%,
    30% 15%,
    40% 26%,
    45% 35%,
    45% 45%,
    40% 55%,
    10% 90%,
    10% 98%,
    8% 100%,
    0 100%
  );
  width: 300px;
  height: 320px;
}

p {
  font-size: 0.9rem;
}

规范

规范
CSS Shapes Module Level 1
# funcdef-basic-shape-polygon

浏览器兼容性

另见