border-radius

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

border-radius CSS 属性用于设置元素外边框边缘的圆角。您可以设置一个半径来创建圆形角,或设置两个半径来创建椭圆形角。

试一试

border-radius: 30px;
border-radius: 25% 10%;
border-radius: 10% 30% 50% 70%;
border-radius: 10% / 50%;
border-radius: 10px 100px / 120px;
border-radius: 50% 20% / 10% 40%;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    This is a box with rounded corners.
  </div>
</section>
#example-element {
  width: 80%;
  height: 80%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  background-color: #5b6dcd;
  color: white;
  padding: 10px;
}

即使元素没有边框,半径也适用于整个background;裁剪的确切位置由background-clip属性定义。

border-collapsecollapse时,border-radius属性不适用于表格元素。

注意:与任何简写属性一样,单独的子属性不能继承,例如border-radius:0 0 inherit inherit,这会部分覆盖现有定义。相反,必须使用单独的完整属性。

构成属性

此属性是以下 CSS 属性的简写:

语法

css
/* The syntax of the first radius allows one to four values */
/* Radius is set for all 4 sides */
border-radius: 10px;

/* top-left-and-bottom-right | top-right-and-bottom-left */
border-radius: 10px 5%;

/* top-left | top-right-and-bottom-left | bottom-right */
border-radius: 2px 4px 2px;

/* top-left | top-right | bottom-right | bottom-left */
border-radius: 1px 0 3px 4px;

/* The syntax of the second radius allows one to four values */
/* (first radius values) / radius */
border-radius: 10px / 20px;

/* (first radius values) / top-left-and-bottom-right | top-right-and-bottom-left */
border-radius: 10px 5% / 20px 30px;

/* (first radius values) / top-left | top-right-and-bottom-left | bottom-right */
border-radius: 10px 5px 2em / 20px 25px 30%;

/* (first radius values) / top-left | top-right | bottom-right | bottom-left */
border-radius: 10px 5% / 20px 25em 30px 35em;

/* Global values */
border-radius: inherit;
border-radius: initial;
border-radius: revert;
border-radius: revert-layer;
border-radius: unset;

border-radius属性的指定方式为

  • 一个、两个、三个或四个<length><percentage>值。这用于为角设置一个单一半径。
  • 可选地,后面跟着"/"和一个、两个、三个或四个<length><percentage>值。这用于设置一个额外的半径,以便您可以拥有椭圆形角。

半径 A light blue rectangle with a light gray border. All 4 corners are rounded. <length><percentage>,表示用于边框每个角的半径。它仅用于单值语法。
左上角和右下角 A light blue rectangle with a light gray border. The 2 corners on the top left and the bottom right have are rounded. <length><percentage>,表示用于元素框左上角和右下角边框的半径。它仅用于双值语法。
右上角和左下角 A light blue rectangle with a light gray border. The 2 corners on the top right and the bottom left are rounded. <length><percentage>,表示用于元素框右上角和左下角边框的半径。它仅用于双值和三值语法。
左上角 A light blue rectangle with a light gray border. The corner on the top left is rounded. <length><percentage>,表示用于元素框左上角边框的半径。它仅用于三值和四值语法。
右上角 A light blue rectangle with a light gray border. The corner on the top right is rounded. <length><percentage>,表示用于元素框右上角边框的半径。它仅用于四值语法。
右下角 A light blue rectangle with a light gray border. The bottom right corner is rounded. <length><percentage>,表示用于元素框右下角边框的半径。它仅用于三值和四值语法。
左下角 A light blue rectangle with a light gray border. The bottom left corner is rounded. <length><percentage>,表示用于元素框左下角边框的半径。它仅用于四值语法。
<length>

使用长度值表示圆形半径的大小,或椭圆的半长轴和半短轴。负值无效。

<percentage>

使用百分比值表示圆形半径的大小,或椭圆的半长轴和半短轴。水平轴的百分比参考框的宽度;垂直轴的百分比参考框的高度。负值无效。

例如

css
border-radius: 1em / 5em;

/* It is equivalent to: */
border-top-left-radius: 1em 5em;
border-top-right-radius: 1em 5em;
border-bottom-right-radius: 1em 5em;
border-bottom-left-radius: 1em 5em;
css
border-radius: 4px 3px 6px / 2px 4px;

/* It is equivalent to: */
border-top-left-radius: 4px 2px;
border-top-right-radius: 3px 4px;
border-bottom-right-radius: 6px 2px;
border-bottom-left-radius: 3px 4px;

正式定义

初始值作为简写中的每个属性
应用于所有元素;但当border-collapsecollapse时,用户代理不要求应用于tableinline-table元素。目前,内部表格元素的行为未定义。它也适用于::first-letter
继承性
百分比参考边框框的相应维度
计算值作为简写中的每个属性
动画类型作为简写中的每个属性

正式语法

border-radius = 
<length-percentage [0,∞]>{1,4} [ / <length-percentage [0,∞]>{1,4} ]?

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

示例

比较边框样式

以下示例有七个<pre>元素,每个都演示了borderborder-radius样式的组合。应用于每个<pre>元素的样式作为元素的内容包含在内,因此您可以看到创建相关边框样式所需的CSS声明

corner-shapeborder-radius结合使用

当非0border-radius值应用于盒子角时,您可以使用corner-shape属性(或其长手和简写之一)为该角应用自定义形状,例如斜角、切口或超椭圆。此示例演示了corner-shape的用法。

HTML

此示例的标记包含一个单独的 <div> 元素。

html
<div></div>

CSS

我们为盒子提供了一些基本样式,为了简洁起见我们将其隐藏。我们还应用了一个box-shadow,一个0 20% 50px 30%border-radius,以及一个superellipse(0.5) bevel notch squirclecorner-shape

css
div {
  box-shadow: 1px 1px 3px gray;
  border-radius: 0 20% 50px 30%;
  corner-shape: superellipse(0.5) bevel notch squircle;
}

结果

渲染结果如下所示:

请注意,左上角没有应用任何角形状,因为它设置了border-radius0

规范

规范
CSS Backgrounds and Borders Module Level 3
# border-radius

浏览器兼容性

另见