Detailed explanation of the causes and solutions of conflicts between filters and fixed

Detailed explanation of the causes and solutions of conflicts between filters and fixed

Problem Description

When filter attribute is used in body , it will cause incorrect positioning of the fixed element, that is, it will no longer be positioned relative to viewport , but relative to the entire web page ( body element).

<html>
  <head>
    <title>css filter issue</title>
    <style>
      body {
        height: 600px;
        background: red;
        filter: grayscale(1); /* Key code*/
      }

      .fixed {
        color: yellow;
        position: fixed;
        top: 0;
        right: 0;
      }
    </style>
  </head>
  <body>
    <div class="fixed">fixed item</div>
  </body>
</html>

The effect diagram is as follows: Note that fixed item element in the upper right corner is no longer positioned relative to the upper right corner of the screen view.

Solution

The reason for this problem is: when filter is not none , if the element or its child element has absolute or fixed attribute, a new containing block/container will be created for it, which will cause the positioning of the absolute or fixed element to change (that is, the positioning <parent> element of absolute or fixed element is changed to the newly created element).

In the above example, when filter attribute is used in the body tag, filter will generate a new containing block with the same position and size as body , and then fixed element will be positioned according to this containing block, so we will see that fixed element loses its original characteristics;

However, if the filter acts on the root element (ie, the HTML tag), it will not create a new containing block for the absolute or fixed child elements.

Reference: https://drafts.fxtf.org/filter-effects/#FilterProperty

So the solution is very simple, just put the filter attribute on the html tag

html {
	filter: grayscale(1);
}

Extensions

1. position: fixed

When an element includes the fixed attribute, the screen viewport creates a containing block for it, whose size is the size of viewport , and the fixed element is then positioned based on the containing block. So usually we say that fixed elements are positioned relative to viewport .

Additionally, fixed attribute creates a new stacking context. When transform , perspective , or filter property of an element's ancestor is non none , the container is changed from the viewport to that ancestor.

Reference: https://www.w3.org/TR/css-position-3/#fixed-pos

2. HTML tags and body tags

The difference between the two can be found in this blog: http://phrogz.net/css/htmlvsbody.html

ps: HTML element is max(screen height <viewport height>, internal element height <body element height>)

This concludes this article on the causes and solutions for conflicts between filters and fixed. For more information on conflicts between filters and fixed, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. We hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Recommended tips for web front-end engineers

>>:  A detailed introduction to the basics of Linux scripting

Recommend

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

MySQL startup error InnoDB: Unable to lock/ibdata1 error

An error message appears when MySQL is started in...

CSS float property diagram float property details

Using the CSS float property correctly can become...

How to configure Linux CentOS to run scripts regularly

Many times we want the server to run a script reg...

How to use DPlayer.js video playback plug-in

DPlayer.js video player plug-in is easy to use Ma...

Installation tutorial of mysql5.7.21 decompression version under win10

Install the unzipped version of Mysql under win10...

Vue implements horizontal scrolling of marquee style text

This article shares the specific code for Vue to ...

Detailed explanation of MySQL phantom reads and how to eliminate them

Table of contents Transaction Isolation Level Wha...

Bootstrap 3.0 study notes grid system principle

Through the brief introduction in the previous tw...

How to clear floating example code in css

Overview The framework diagram of this article is...

Why MySQL database avoids NULL as much as possible

Many tables in MySQL contain columns that can be ...

VMwarea virtual machine installation win7 operating system tutorial diagram

The installation process of VMwarea will not be d...

Solution to the problem of not finding Tomcat configuration in Intelli Idea

I joined a new company these two days. The compan...