自动修复Tailwind中的低对比度

huangapple go评论72阅读模式
英文:

Automatically repair bad contrast in Tailwind

问题

我有一个问题需要使用CSS选择器来解决。我正在尝试为Tailwind创建自动颜色覆盖,以使我们的网站始终符合WCAG准则。

我有一个我编写的Tailwind插件,可以自动解决这个问题,但我意识到我定位CSS选择器的方式不对。

假设有一种颜色,白色。还有另一种颜色 - 浅灰色。当浅灰色施加在白色上时,我希望自动将颜色更正为黑色。

我目前是使用以下选择器来实现这一点:

.bg-white .text-light-grey {
  color: black;
}

然而,这会引入问题!它在这个HTML结构上运行良好:

<div class="bg-white">
    <span class="text-light-grey">This is now black.</span>
</div>

但它会破坏这个结构:

<div class="bg-white">
    <span class="text-light-grey">This is now black.</span>

    <div class="bg-black">
        <span class="text-light-grey">This is also now black, but it shouldn't be - It's inside a black container!</span>
    </div>
</div>

我该如何修改CSS选择器,以便在它们之间指定另一个背景时,颜色不会被破坏。

类似这样的东西:

.bg-white :not(某种确保背景白色和文本颜色之间没有其他背景的方法) .text-light-grey {
  color: black;
}

这应该是可能的,对吗?也许可以使用class~=之类的东西?

编辑:了解:has()选择器的人是否知道是否可以使用它来解决这个问题?

英文:

I have a problem I need to solve with CSS selectors. I am trying to make automatic color overrides for Tailwind to have our site always comply to WCAG guidelines.

I have a tailwind plugin I've written which automatically fixes this problem - but I've realized the way I'm targeting the CSS selectors is off.

Assume there is a color, white. And another color - light grey. When light-grey is imposed on white, I want to automatically correct the color to black.

I am doing this currently with the following selectors

.bg-white .text-light-grey {
  color: black;
}

However, this introduces problems! It works fine on this HTML structure:

&lt;div class=&quot;bg-white&quot;&gt;
    &lt;span class=&quot;text-light-grey&quot;&gt;This is now black.&lt;/span&gt;
&lt;/div&gt;

But it will ruin this structure

&lt;div class=&quot;bg-white&quot;&gt;
    &lt;span class=&quot;text-light-grey&quot;&gt;This is now black.&lt;/span&gt;

    &lt;div class=&quot;bg-black&quot;&gt;
        &lt;span class=&quot;text-light-grey&quot;&gt;This is also now black, but it shouldn&#39;t be - It&#39;s inside a black container!.&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;

How can I modify the CSS selectors, so that when there is another background specified between them, the color is not corrupted.

Something like this

.bg-white :not(some fancy way to make sure there are no other bgs inbetween bg white and the text color) .text-light-grey {
  color: black;
}

This has got to be possible right? Maybe something with class~= or something?

Edit: Does anyone with knowledge of the :has() selector know if this could be solved using it?

答案1

得分: 2

以下是您要翻译的内容:

The idea that has come to my mind is to use two selectors. One targets the immediate child and the other checks all intermediary elements with classes for background color. Would this suffice in your situation?

.bg-white {
  background: white;
}

.bg-black {
  background: black;
}

.text-light-grey {
  color: lightgrey;
}

.bg-white > .text-light-grey,
.bg-white :not([class^="bg-"]) .text-light-grey {
  color: black;
}
<div class="bg-white">
  <span class="text-light-grey">This is now black.</span>

  <div class="bg-black">
    <span class="text-light-grey">This is not black!</span>
  </div>

  <div>
    <span class="text-light-grey">This is now black!</span>
  </div>
</div>

EDIT: If you don't have a lot of classes that require adjusted color, you may consider this way:

.bg-white {
  background: white;
}

.bg-black {
  background: black;
}

.text-light-grey {
  color: lightgrey;
}

.bg-blue {
  background: blue;
}

/* Similar to what you wrote in the comment */

.bg-white .text-light-grey {
  color: black;
}

[class*="bg-"]:not(.bg-white) .text-light-grey {
  color: lightgrey;
}
<div class="bg-white">
  <span class="text-light-grey">This is now black.</span>

  <div class="bg-black">
    <span class="text-light-grey">This is not black!</span>
  </div>

  <div>
    <span class="text-light-grey">This is now black!</span>
  </div>

  <div>
    <div class="bg-blue">
      <div>
        <span class="text-light-grey">This is nested and not black!</span>
      </div>
    </div>
  </div>
</div>
英文:

The idea that has come to my mind is to use two selectors. One targets the immediate child and the other checks all intermediary elements with classes for background color. Would this suffice in your situation?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.bg-white {
  background: white;
}

.bg-black {
  background: black;
}

.text-light-grey {
  color: lightgrey;
}

.bg-white&gt;.text-light-grey,
.bg-white :not([class^=&quot;bg-&quot;]) .text-light-grey {
  color: black;
}

<!-- language: lang-html -->

&lt;div class=&quot;bg-white&quot;&gt;
  &lt;span class=&quot;text-light-grey&quot;&gt;This is now black.&lt;/span&gt;

  &lt;div class=&quot;bg-black&quot;&gt;
    &lt;span class=&quot;text-light-grey&quot;&gt;This is not black!&lt;/span&gt;
  &lt;/div&gt;

  &lt;div&gt;
    &lt;span class=&quot;text-light-grey&quot;&gt;This is now black!&lt;/span&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

EDIT: If you don't have a lot of classes that require adjusted color, you may consider this way:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.bg-white {
  background: white;
}

.bg-black {
  background: black;
}

.text-light-grey {
  color: lightgrey;
}

.bg-blue {
  background: blue;
}


/* Similar to what you wrote in the comment */

.bg-white .text-light-grey {
  color: black;
}

[class*=&quot;bg-&quot;]:not(.bg-white) .text-light-grey {
  color: lightgrey;
}

<!-- language: lang-html -->

&lt;div class=&quot;bg-white&quot;&gt;
  &lt;span class=&quot;text-light-grey&quot;&gt;This is now black.&lt;/span&gt;

  &lt;div class=&quot;bg-black&quot;&gt;
    &lt;span class=&quot;text-light-grey&quot;&gt;This is not black!&lt;/span&gt;
  &lt;/div&gt;

  &lt;div&gt;
    &lt;span class=&quot;text-light-grey&quot;&gt;This is now black!&lt;/span&gt;
  &lt;/div&gt;

  &lt;div&gt;
    &lt;div class=&quot;bg-blue&quot;&gt;
      &lt;div&gt;
        &lt;span class=&quot;text-light-grey&quot;&gt;This is nested and not black!.&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

考虑利用级联。使用自动级联到bg-*类的CSS变量。这有一个优点,它可以适用于无限嵌套。

英文:

Consider leveraging the cascade. Use CSS variables that automatically cascade down on the bg-* classes. This has the advantage that it would work for an infinite amount of nesting:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

tailwind.config = {
  theme: {
    extend: {
      colors: {
        &#39;light-gray&#39;: &#39;rgb(var(--light-gray) / &lt;alpha-value&gt;)&#39;,
      }
    },
  },
};

<!-- language: lang-css -->

.bg-white {
  --light-gray: 0 0 0;
}

.bg-black {
  --light-gray: 255 255 255;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;bg-white&quot;&gt;
  &lt;span class=&quot;text-light-gray&quot;&gt;This is now black.&lt;/span&gt;

  &lt;div class=&quot;bg-black&quot;&gt;
    &lt;span class=&quot;text-light-gray&quot;&gt;This is also now black, but it shouldn&#39;t be - It&#39;s inside a black container!.&lt;/span&gt;

    &lt;div class=&quot;bg-white&quot;&gt;
      &lt;span class=&quot;text-light-gray&quot;&gt;This is now black.&lt;/span&gt;

      &lt;div class=&quot;bg-black&quot;&gt;
        &lt;span class=&quot;text-light-gray&quot;&gt;This is also now black, but it shouldn&#39;t be - It&#39;s inside a black container!.&lt;/span&gt;
      &lt;/div&gt;

      &lt;div class=&quot;bg-white&quot;&gt;
        &lt;span class=&quot;text-light-gray&quot;&gt;This is now black.&lt;/span&gt;

        &lt;div class=&quot;bg-black&quot;&gt;
          &lt;span class=&quot;text-light-gray&quot;&gt;This is also now black, but it shouldn&#39;t be - It&#39;s inside a black container!.&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

我尝试使用`currentcolor`。不确定这会如何影响您项目的其余部分但我分享一下以防有所帮助这与@Wongjn的解决方案相似

.bg-white {
  color: black;
}

.bg-black {
  color: white;
}

.text-light-gray {
  color: currentcolor;
}
另一个更类似于@Wongjn的解决方案但分享一下以防有所帮助

.bg-white {
  --text: black;
}

.bg-black {
  --text: white;
}

.text-light-gray {
  color: var(--text);
}
英文:

I had a go using currentcolor. Not sure how that would affect the rest of your project but sharing it in case it helps at all. It is similar to @Wongjn's solution.

.bg-white {
  color: black;
}

.bg-black {
  color: white;
}

.text-light-gray {
  color: currentcolor;
}

https://play.tailwindcss.com/hQ4VW1RysN?file=css

And another one, even more similar to @Wongjn's solution, but sharing in case it helps.

.bg-white {
  --text: black;
}

.bg-black {
  --text: white;
}

.text-light-gray {
  color: var(--text);
}

https://play.tailwindcss.com/CdT0KVvQga?file=css

huangapple
  • 本文由 发表于 2023年6月29日 03:10:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定