从上周开始,我们的网站在当前版本的谷歌浏览器上出现了CSS问题.该页面在Edge和Firefox上正常工作.

奇怪的是,这似乎只发生在一些机器上,或者可能发生在Chrome实例上.无论设置是否重置和刷新,或匿名模式无法解决该问题.

它似乎与CSS中的属性 Select 器有关.我们发现的最小的可重复例子很奇怪.它包含许多不相关的样式,删除(看似)不相关的样式可以使页面正常工作.

当它工作时,下面页面上的文本是红色的.当它不工作时,文本为黑色.

Screenshot including style in devtools: (upper is Chrome, lower is Edge)

<!DOCTYPE html>
<html>
<head>
<style>
custom-element .custom-form-control.miss-value:focus + label[data-placeholder]:before,
custom-element .custom-form-control.has-value + label[data-placeholder]:before {
}
custom-element .custom-form-control.has-value.has-data-placeholder:focus + label[data-placeholder],
custom-element .custom-form-control.miss-value.has-data-placeholder:focus + label[data-placeholder] {
}
custom-element .custom-form-control.has-value + label[data-placeholder],
custom-element .custom-form-control.miss-value + label[data-placeholder] {
}
custom-element .custom-form-control.has-value + label[data-placeholder][data-alt]:before,
custom-element .custom-form-control.miss-value + label[data-placeholder][data-alt]:before {
  content: attr(data-alt);
}
custom-element .custom-form-control.has-value + label[data-placeholder]:before,
custom-element .custom-form-control.miss-value + label[data-placeholder]:before {
    color: red;
}
custom-element .custom-form-control.miss-value.has-data-placeholder + label[data-placeholder] {
}
custom-element .custom-form-control.has-value {
}
custom-element .custom-form-control.has-value:hover {
}
custom-element .custom-form-control.has-value label[data-placeholder] {
}
custom-element .custom-form-control:focus + label[data-placeholder]:before,
custom-element .custom-form-control.has-value.is-active + label[data-placeholder]:before,
custom-element .custom-form-control.has-value:focus:hover + label[data-placeholder]:before,
custom-element .custom-form-control.has-value:hover + label[data-placeholder]:before,
custom-element .custom-form-control[type='text'].has-value:hover + label[data-placeholder]:before,
custom-element .custom-form-control[type='text'].has-value:focus + label[data-placeholder]:before,
custom-element .custom-form-control.has-value.custom-bordered:focus + label[data-placeholder]:before,
custom-element .custom-form-control.has-value.custom-bordered:hover + label[data-placeholder]:before {
}
my-unrelated-element .custom-form-control.miss-value:focus + label[data-placeholder]:before,
my-unrelated-element .custom-form-control.has-value + label[data-placeholder]:before {
}
my-unrelated-element .custom-form-control.has-value.has-data-placeholder:focus + label[data-placeholder],
my-unrelated-element .custom-form-control.miss-value.has-data-placeholder:focus + label[data-placeholder] {
}
my-unrelated-element .custom-form-control.has-value + label[data-placeholder],
my-unrelated-element .custom-form-control.miss-value + label[data-placeholder] {
}
my-unrelated-element .custom-form-control.has-value + label[data-placeholder]:before,
my-unrelated-element .custom-form-control.miss-value + label[data-placeholder]:before {
}
my-unrelated-element .custom-form-control.miss-value.has-data-placeholder + label[data-placeholder] {
}
my-unrelated-element .custom-form-control.has-value label[data-placeholder] {
}

some-unrelated-element .custom-form-control:focus + label[data-placeholder]:before,
some-unrelated-element .custom-form-control.has-value.is-active + label[data-placeholder]:before,
some-unrelated-element .custom-form-control.has-value:focus:hover + label[data-placeholder]:before,
some-unrelated-element .custom-form-control.has-value:hover + label[data-placeholder]:before,
some-unrelated-element .custom-form-control[type='text'].has-value:hover + label[data-placeholder]:before,
some-unrelated-element .custom-form-control[type='text'].has-value:focus + label[data-placeholder]:before,
some-unrelated-element .custom-form-control.has-value.custom-bordered:focus + label[data-placeholder]:before,
some-unrelated-element .custom-form-control.has-value.custom-bordered:hover + label[data-placeholder]:before {
}

some-unrelated-element input.miss-value:focus + label[data-placeholder]:before,
some-unrelated-element input.has-value + label[data-placeholder]:before {
}

some-unrelated-element input.has-value.has-data-placeholder:focus + label[data-placeholder],
some-unrelated-element input.miss-value.has-data-placeholder:focus + label[data-placeholder] {
}

some-unrelated-element input.has-value + label[data-placeholder],
some-unrelated-element input.miss-value + label[data-placeholder] {
}
unrelated-element input:focus + label[data-placeholder]:before,
unrelated-element input.has-value.is-active + label[data-placeholder]:before,
unrelated-element input.has-value:focus:hover + label[data-placeholder]:before,
unrelated-element input.has-value:hover + label[data-placeholder]:before,
unrelated-element input[type='text'].has-value:hover + label[data-placeholder]:before,
unrelated-element input[type='text'].has-value:focus + label[data-placeholder]:before,
unrelated-element input.has-value:focus + label[data-placeholder]:before,
unrelated-element input.has-value:hover + label[data-placeholder]:before {
}

</style>
</head>
<body>
<custom-element>
    <div class="form-group">
        <input type="text" class="custom-form-control has-value" id="0ad40e91-f280-42d2-b0c5-2b38dd433e80">
        <label for="0ad40e91-f280-42d2-b0c5-2b38dd433e80" data-placeholder="This text should be red" data-alt="This text should be red"></label>
    </div>
</custom-element>
</html>

推荐答案

我们的客户在升级到Chrome 103后也有类似的(间歇性)问题.

经过大量调试和梳理,我发现如果有50个(或更多)规则,其中[data-attribute]个没有任何值(而[data-attribute="foo"]个工作正常),那么Chrome只会丢弃[data-attribute]个样式.

如果在这pen行中没有看到黄色背景,请try 删除CSS中的任何[data-children]行:-)

我通过添加无效的CSS规则并在 Select 器中指定数据属性值来解决了这个问题:

[data-attribute="foo"] {
   bar: baz;
}

Value 100 needs to exist in at least one place in your HTML.

Html相关问答推荐

一次悬停可触发同一分区中的另一次悬停

如何删除html原生对话框的宽度

Chrome和Safari浏览器中的CSS3动画不同

如何删除在小屏幕上单击按钮时出现的蓝色方块?

如何在htmlAngular 17的@for中使用索引

标签数据的XPath?

如何在图像中垂直和水平居中显示文本?

粘滞的导航栏延伸到超过页边距的右侧

如何翻转卡片图像的背面

SCSS动画错误:心脏在页面刷新时启动动画,原因是:Checked和:Not(:Checked) Select 器

禁用的文本区域会丢失换行符

带有图标和悬停过渡的CSS按钮

如何使div填充父项的剩余高度

如何用背景色填充边框半径创建的间隙

:invalid Select 器的惰性判断

网格项未在同一行上对齐

如何在悬停时更改同级元素 CSS

拨动switch 在重新加载时未重置

为什么图像会影响我的

如果不透明的元素出现在下方,为什么不透明度会使一个元素出现在另一个元素上方?