我正在try 为css中的文本创建一种"循环彩虹发光"的效果.我或多或少做到了这一点,但我遇到了一些我无法解释的事情.当我从父元素中删除多余的filter: blur(0px)时,psudo-元素之后的::向左折叠.

.rainbow {
    display: inline-block;
    color: white;
    filter: blur(0px);
}

.rainbow::after {
    content: attr(data-text);
    z-index: -1;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: linear-gradient(90deg,
        hsl(0, 100%, 50%),
        hsl(30, 100%, 50%),
        hsl(60, 100%, 50%),
        hsl(90, 100%, 50%),
        hsl(120, 100%, 50%),
        hsl(150, 100%, 50%),
        hsl(180, 100%, 50%),
        hsl(210, 100%, 50%),
        hsl(240, 100%, 50%),
        hsl(270, 100%, 50%),
        hsl(300, 100%, 50%),
        hsl(330, 100%, 50%),
        hsl(360, 100%, 50%))
        0 0 / 200% 100%;
    -webkit-background-clip: text;
    -moz-background-clip: text;        
    -webkit-text-fill-color: transparent;
    -moz-text-fill-color: transparent;
    filter: blur(10px);
    animation: scroll_background 2s linear infinite;
}

@keyframes scroll_background {
  to { background-position: -200% 0 }
}

https://jsfiddle.net/tpm1gf5v/

推荐答案

看起来这是因为没有filter: blur(0px),rainbow不再是相对容器(根据这answer),也没有堆叠上下文来正确放置::after.

这可能导致相对于导致错误的body放置::after.

它似乎是固定的,给rainbow一个相对位置,并指定一个新的堆叠环境:

.rainbow {
  display: inline-block;
  color: white;

  /* ???? Add this */
  position: relative;

  /* ???? Add one of these two for a new stacking context */
  isolation: isolate;
  /* z-index: 0; */
}

示例:(从发布的jsfiddle修改)

html,
body {
  background: #d6daf0;
  font-family: arial, helvetica, sans-serif;
  font-size: 100px;
}

.rainbow {
  display: inline-block;
  color: white;
  
  /* ???? Add this */
  position: relative;
  
  /* ???? Add one of these two for a new stacking context */
  isolation: isolate;
  /* z-index: 0; */
}

.rainbow::after {
  content: attr(data-text);
  z-index: -1;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: linear-gradient(90deg,
      hsl(0, 100%, 50%),
      hsl(30, 100%, 50%),
      hsl(60, 100%, 50%),
      hsl(90, 100%, 50%),
      hsl(120, 100%, 50%),
      hsl(150, 100%, 50%),
      hsl(180, 100%, 50%),
      hsl(210, 100%, 50%),
      hsl(240, 100%, 50%),
      hsl(270, 100%, 50%),
      hsl(300, 100%, 50%),
      hsl(330, 100%, 50%),
      hsl(360, 100%, 50%)) 0 0 / 200% 100%;
  -webkit-background-clip: text;
  -moz-background-clip: text;
  -webkit-text-fill-color: transparent;
  -moz-text-fill-color: transparent;
  filter: blur(10px);
  animation: scroll_background 2s linear infinite;
}

@keyframes scroll_background {
  to {
    background-position: -200% 0
  }
}
<span class="rainbow" data-text="Hello, World!">Hello, World!</span>

希望这能有所帮助.

Css相关问答推荐

正确的`最小/最大宽度`+`非(设备类型)`css媒体查询的语法

无法在 React 组件 (TypeScript) 中应用 Tailwind CSS 类

使用普通 CSS 的视口对角线长度

如何取消 CSS 伪类中的属性设置?

当滚动量较小时,在滚动问题上动画粘性导航

在变量中存储匹配 Select 器的子元素的计数

Tailwind CSS 隐藏和可见

用css画3个三角形

使用 React-Router 和 Outlet 时如何确保内容保持在下方

如何为柔和的配色方案提供易于使用的高对比度替代方案?

JavaFX TreeTableView Css for unfocused selected line

Bootstrap:在列之间添加边距/填充空间

Div Size 自动调整内容大小

如何将 bootstrap 列高设为 100% 行高?

从 css 添加标题属性

更改 FontAwesome 图标的字体粗细?

如何仅在一侧设置 CSS 边框?

模拟显示:React Native 中的内联

在 CSS 中结合border-top、border-right、border-left、border-bottom

CSS垂直对齐不适用于浮动