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

瀏覽器相容性

另見