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

Graphic tutorial on installing Ubuntu 18.04 on VMware 15 virtual machine

In the past few years, I have been moving back an...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

In-depth explanation of the global status of WeChat applet

Preface In WeChat applet, you can use globalData ...

Ubuntu 16.04 installation tutorial under VMware 12

This article shares with you the installation tut...

How to import, register and use components in batches in Vue

Preface Components are something we use very ofte...

Detailed explanation of Mysql transaction processing

1. MySQL transaction concept MySQL transactions a...

4 functions implemented by the transform attribute in CSS3

In CSS3, the transform function can be used to im...

JS realizes automatic playback of timeline

Recently, I have implemented such an effect: clic...

How to quickly paginate MySQL data volumes of tens of millions

Preface In backend development, in order to preve...

Vue3 manual encapsulation pop-up box component message method

This article shares the specific code of Vue3 man...

Detailed explanation of the 4 codes that turn the website black, white and gray

The 2008.5.12 Wenchuan earthquake in Sichuan took...

JavaScript ES new feature block scope

Table of contents 1. What is block scope? 2. Why ...

JS gets the position of the nth occurrence of a specified string in a string

Learn about similar methods for getting character...

Sample code for batch deployment of Nginx with Ansible

1.1 Copy the nginx installation package and insta...