inset()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

inset() CSS 函式根據參考框的各邊向內指定的距離定義一個矩形。它是一個基本的形狀函式,用於定義 <basic-shape> 資料型別之一。

試一試

clip-path: inset(30px);
clip-path: inset(1rem 2rem 3rem 4rem);
clip-path: inset(20% 30% round 20px);
clip-path: inset(4rem 20% round 1rem 2rem 3rem 4rem);
<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
shape-outside: inset(20px 50px 10px 0 round 50px);

<length-percentage>{1,4}

當提供了所有四個引數時,它們表示從參考框向內偏移的頂部、右側、底部和左側距離,這些距離定義了內嵌矩形邊緣的位置。這些引數遵循 margin 簡寫語法,允許您使用一個、兩個或四個值設定所有四個內嵌距離。

如果某一維度的一對內嵌距離之和大於該維度的 100%,則兩個值將按比例減小,使其和等於 100%。例如,值 inset(90% 10% 60% 10%) 的頂部內嵌距離為 90%,底部內嵌距離為 60%。這些值將按比例減小為 inset(60% 10% 40% 10%)。像這種不包含任何區域且沒有 shape-margin 的形狀不會影響文字環繞。

<border-radius>

可選的 <border-radius> 引數使用 border-radius 簡寫語法定義內嵌矩形的圓角。

正式語法

<inset()> = 
inset( <length-percentage>{1,4} [ round <'border-radius'> ]? )

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

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

示例

基本內嵌示例

在下面的示例中,我們使用 inset() 形狀將內容拉到浮動元素上方。更改偏移值以檢視形狀如何變化。

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: 400px;
  margin: 0 auto;
}

.shape {
  float: left;
  width: 150px;
  height: 100px;
  clip-path: inset(45px 50px 15px 0 round 50px);
  shape-outside: inset(40px 40px 10px 0 round 50px);
  background-color: coral;
  border-radius: 20px;
  margin: 0;
  padding: 20px;
}

規範

規範
CSS Shapes Module Level 1
# funcdef-basic-shape-inset

瀏覽器相容性

另見