css反转_如何使用CSS反转颜色

css反转

I had this problem. I added a “black on white” image on a page, only to realize that with dark mode, my page correctly changes the background to black, but the image remains white on black.

我有这个问题。 我在页面上添加了“黑白”图像,只是意识到在暗模式下,我的页面可以将背景正确地更改为黑色,但是图像仍然保持黑白。

Kind of bad.

有点不好

So I added this rule to my CSS to detect dark mode and automatically invert the color of the image:

因此,我将此规则添加到CSS中以检测暗模式并自动反转图像的颜色:

@media (prefers-color-scheme: dark) {
  .my-image {
    filter: invert(100%);
  }
}

It’s not 100% accurate in my case, because my dark background color is not perfectly black, but it’s better than nothing.

对于我而言,它不是100%准确的,因为我的深色背景颜色不是完美的黑色,但总比没有好。

To make things perfect you could also add the image using a CSS background image instead of an img tag in HTML, and I could easily swap that in dark mode.

为了使事情变得完美,您还可以使用CSS背景图像而不是HTML中的img标签添加图像,并且我可以轻松地在暗模式下交换该图像。

翻译自: https://flaviocopes.com/css-invert-colors/

css反转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值