shape-image-threshold

Baseline 广泛可用 *

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

* 此特性的某些部分可能存在不同级别的支持。

shape-image-threshold CSS 属性设置用于从图像中提取形状的 alpha 通道阈值,其中图像用作 shape-outside 的值。

试一试

shape-outside: linear-gradient(
  50deg,
  rgb(77 26 103),
  transparent 80%,
  transparent
);
shape-image-threshold: 0.2;
shape-outside: linear-gradient(
  50deg,
  rgb(77 26 103),
  transparent 80%,
  transparent
);
shape-image-threshold: 0.4;
shape-outside: linear-gradient(
  50deg,
  rgb(77 26 103),
  transparent 80%,
  transparent
);
shape-image-threshold: 0.6;
<section class="default-example" id="default-example">
  <div class="example-container">
    <div class="transition-all" id="example-element"></div>
    We had agreed, my companion and I, that I should call for him at his house,
    after dinner, not later than eleven o’clock. This athletic young Frenchman
    belongs to a small set of Parisian sportsmen, who have taken up “ballooning”
    as a pastime. After having exhausted all the sensations that are to be found
    in ordinary sports, even those of “automobiling” at a breakneck speed, the
    members of the “Aéro Club” now seek in the air, where they indulge in all
    kinds of daring feats, the nerve-racking excitement that they have ceased to
    find on earth.
  </div>
</section>
.example-container {
  text-align: left;
  padding: 20px;
}

#example-element {
  float: left;
  width: 150px;
  height: 150px;
  margin: 20px;
  background-image: linear-gradient(
    50deg,
    rgb(77 26 103),
    transparent 80%,
    transparent
  );
}

任何 alpha 分量值大于阈值的像素都被视为形状的一部分,用于确定其边界。例如,值为 0.5 意味着形状将包含所有不透明度超过 50% 的像素。

语法

css
/* <number> value */
shape-image-threshold: 0.7;

/* Global values */
shape-image-threshold: inherit;
shape-image-threshold: initial;
shape-image-threshold: revert;
shape-image-threshold: revert-layer;
shape-image-threshold: unset;

<alpha-value>

设置用于从图像中提取形状的阈值。形状由 alpha 值大于阈值的像素定义。超出 0.0(完全透明)到 1.0(完全不透明)范围的值将被限制在此范围内。

正式定义

初始值0.0
应用于浮动
继承性
计算值与将 <number> 裁剪到 [0.0, 1.0] 区间后的指定值相同。
动画类型一个 number

正式语法

shape-image-threshold = 
<opacity-value>

<opacity-value> =
<number> |
<percentage>

示例

文本与渐变对齐

此示例创建了一个带有渐变背景图像的 <div> 块。渐变使用 shape-outside 建立为 CSS 形状,以便渐变中至少 20% 不透明(即 alpha 分量大于 0.2 的像素)的像素被视为形状的一部分。

HTML

html
<div id="gradient-shape"></div>

<p>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel at commodi
  voluptates enim, distinctio officia. Saepe optio accusamus doloribus sint
  facilis itaque ab nulla, dolor molestiae assumenda cum sit placeat adipisci,
  libero quae nihil porro debitis laboriosam inventore animi impedit nostrum
  nesciunt quisquam expedita! Dolores consectetur iure atque a mollitia dicta
  repudiandae illum exercitationem aliquam repellendus ipsum porro modi, id nemo
  eligendi, architecto ratione quibusdam iusto nisi soluta? Totam inventore ea
  eum sed velit et eligendi suscipit accusamus iusto dolore, at provident eius
  alias maxime pariatur non deleniti ipsum sequi rem eveniet laboriosam magni
  expedita?
</p>

CSS

css
#gradient-shape {
  width: 150px;
  height: 150px;
  float: left;
  background-image: linear-gradient(30deg, black, transparent 80%, transparent);
  shape-outside: linear-gradient(30deg, black, transparent 80%, transparent);
  shape-image-threshold: 0.2;
}

此处使用带有线性渐变的 background-image 而不是图像文件来建立形状。相同的渐变也用作图像,从中派生形状以建立浮动区域,使用 shape-outside 属性。

然后使用 shape-image-threshold 并将其值设置为 0.2 来建立将渐变像素视为形状一部分的 20% 不透明度阈值。

结果

规范

规范
CSS Shapes Module Level 1
# shape-image-threshold-property

浏览器兼容性

另见