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

浏览器兼容性

另见