一个示例

考察下面的代码:

  <head>
<title>css filter issue</title>
<style>
body {
height: 200vh;
background: #ddd;
}
.container {
background: grey;
height: 200px;
}
.fixed {
color: red;
position: fixed;
top: 0;
right: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="fixed">fixed item</div>
</div>
</body>

页面中有一个位于右上角 position: fixed 定位的元素。按照预期,在页面滚动时它将固定在那个位置。

正常的 fixed 元素其表现

现在对容器加应用 CSS filter 样式,

 .container {
+ filter: invert(1);
background: grey;
height: 200px;
}

再观察结果,注意到 fixed 元素不再 fixed 了。

fixed 元素不再生效

原因

根据 W3C 对 filter 的描述

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 时,被应用该样式的元素其子元素中如果有 positionabsolutefixed 的元素,会为这些元素创建一个新的容器,使得这些绝对或固定定位的元素其定位的基准相对于这个新创建的容器。

相同的情况对 transform 也适用,以下是 W3C 规范的描述

In the HTML namespace, any value other than none for the transform results in the creation of both a stacking context and a containing block. The object acts as a containing block for fixed positioned descendants.

解决

这里只看 filter 的修复。条件允许的情况下,有如下两种方式。

filter 应用到根节点

因为根据规范中对 filter 的描述,如果 filter 应用到根节点,则不会创建新的容器。所以可以将 filter 样式写到 html,如果条件允许的话。

html{
filter: invert(1);
}

将 `filter` 应用到根节点后的修复

filter 应用到非正常定位的元素上

对于 filter 样式,还可以将其只应用到出问题的这些非正常定位的元素身上来解决。

 .fixed {
+ filter: invert(1);
color: red;
position: fixed;
top: 0;
right: 0;
}

将 `filter` 应用到固定定位的元素本身身上

相关资源

CSS fixed 定位元素失效的问题的更多相关文章

  1. 『心善渊』Selenium3.0基础 — 8、使用CSS选择器定位元素

    目录 1.CSS选择器介绍 2.CSS选择器定位语法 3.Selenium中使用CSS选择器定位元素 (1)通过属性定位元素 (2)通过标签定位元素 (3)通过层级关系定位元素 (4)通过索引定位元素 ...

  2. 我是如何通过debug成功甩锅浏览器的:解决fixed定位元素,在页面滚动后touch事件失效问题

    如果你关注我应该知道,我最近对PC端页面进行移动适配.在这个过程中,为了节省用户300ms的时间,同时给予用户更及时的点击反馈(这意味着更好的用户体验),我在尝试使用移动端独有的 touchstart ...

  3. Vue——解决移动端键盘弹起导致的页面fixed定位元素布局错乱

    最近做了一个移动端项目,页面主体是由form表单和底部fixed定位的按钮组成,当用户进行表单输入时,手机软键盘弹起,此时页面的尺寸发生变化,底部fixed定位的元素自然也会上移,可能就会覆盖页面中的 ...

  4. 【写给大家看的CSS】定位元素:使用position/display布局

    引言 使用position和display来实现的当鼠标移动到图片上时显示和隐藏文字或者视频的效果. 知识点 1.relative.absolute的使用. 当子元素使用position:absolu ...

  5. 【CSS】定位元素居中显示

    1.利用margin div { width: 100px; height: 100px; background-color: skyblue; position: absolute; top: 50 ...

  6. 章节十、4-CSS Classes---用多个CSS Classes定位元素

    以下演示操作以该网址中的输入框为例:https://learn.letskodeit.com/p/practice 一.使用input[class=inputs]验证元素是否唯一 注意:使用“clas ...

  7. Python+Selenium练习篇之8-利用css定位元素

    前面介绍了,XPath, id , class , link text, partial link text, tag name, name 七大元素定位方法,本文介绍webdriver支持的最后一个 ...

  8. CSS Positioning(定位)

    Positioning(定位) CSS定位属性允许你为一个元素定位.它也可以将一个元素放在另一个元素后面,并指定一个元素的内容太大时,应该发生什么. 元素可以使用的顶部,底部,左侧和右侧属性定位.然而 ...

  9. 【CSS】定位层

    html:定位层1.概念: >>.定位层是由html元素(标签)形成的一个特殊的box盒子. >>.其重点在于“定位”,而html元素(标签)的定位方式由CSS来控制. 通常情 ...

随机推荐

  1. SEO优化-robots.txt解读

    一.什么是robots.txt robots.txt 文件由一条或多条规则组成.每条规则可禁止(或允许)特定抓取工具抓取相应网站中的指定文件路径. 通俗一点的说法就是:告诉爬虫,我这个网站,你哪些能看 ...

  2. Qt5.7 实现Https 认证全过程解析(亲自动手版)

    #### NetworkRequestManager.h #include <QSsl>#include <QSslKey>#include <QSslSocket> ...

  3. html 标签内联元素和块元素分类【转】

    常见的块状元素与内联元素 块状元素 内联元素 address - 地址 blockquote - 块引用 center - 居中对齐 dir - 目录列表 div - 常用块级容易,也是CSS lay ...

  4. Springboot 框架学习

    Springboot 框架学习 前言 Spring Boot是Spring 官方的顶级项目之一,她的其他小伙伴还有Spring Cloud.Spring Framework.Spring Data等等 ...

  5. 【转】Sentry介绍

    Sentry 是一个开源的实时错误报告工具,支持 web 前后端.移动应用以及游戏,支持 Python.OC.Java.Go.Node.Django.RoR 等主流编程语言和框架 ,还提供了 GitH ...

  6. PAT1124:Raffle for Weibo Followers

    1124. Raffle for Weibo Followers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. 主成分分析PCA详解

    转载请声明出处:http://blog.csdn.net/zhongkelee/article/details/44064401 一.PCA简介 1. 相关背景 上完陈恩红老师的<机器学习与知识 ...

  8. Linux下gcc和g++的区别

    首先编写了第一个C++程序,Hello,world! #include <iostream> using namespace std; void main() ...{ cout < ...

  9. 定时器Timer的使用

    概述 Timer类的主要作用是设置计划任务,但封装任务的类却是TimerTask类.执行计划任务的代码要放入TimerTask的子类中,因为TimerTask是一个抽象类. 方法schedule(ta ...

  10. centos6.9 升级内核版本

    想在centos6.9上安装docket,不过因为内核版本是2.6的故而想升级到最新的内核版本 晚上有编译升级的比较麻烦,不过有助于理解内核升级,我使用的直接升级到最新版方法 1. 导入public ...