变黑白网页,仅需一行代码

文章介绍了如何使用CSS滤镜功能`filter`配合`grayscale`将图像转换为灰度,以及为了兼容性应添加的`-webkit-`前缀。当网页包含绝对或固定定位元素时,需将`filter`应用到`html`元素上,以避免影响布局。这涉及到CSS中的containingblock概念,确保对后代元素的定位正确处理。
摘要由CSDN通过智能技术生成

css 代码:

filter: grayscale(1)

为了更好兼容,可以加-webkit-

filter:grayscale(1); -webkit-filter:grayscale(1)

原理:使用了css特性filter(滤镜)配合grayscale(将图像转换为灰度图像0%到100%)

filter样式加到哪里

  1. 添加到body

  1. 如果网页有绝对和固定定位元素,一定要加到html上

详情:

引用:

A value other than none for the filter property results in the creation of a containing block for absolute and fixed positioned descendants unless the element it applies to is a document root element in the current browsing context. The list of functions are applied in the order provided.

翻译:

对于filter属性,除none之外的值会导致为绝对和固定位置的后代创建containing block,除非它所应用的元素是当前浏览上下文中的文档根元素。函数列表按提供的顺序应用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值