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

The solution record of Vue failing to obtain the element for the first time

Preface The solution to the problem of not being ...

HTML Table Tag Tutorial (47): Nested Tables

<br />In the page, typesetting is achieved b...

MySQL optimization query_cache_limit parameter description

query_cache_limit query_cache_limit specifies the...

About converting textarea text to html, that is, carriage return and line break

Description: Change the carriage return in the tex...

A brief analysis of kubernetes controllers and labels

Table of contents 01 Common controllers in k8s RC...

How to implement load balancing in MySQL

Preface MySQL is a high-speed, high-performance, ...

MySQL sorting Chinese details and examples

Detailed explanation of MySQL sorting Chinese cha...

How to publish a locally built docker image to dockerhub

Today we will introduce how to publish the local ...

DD DT DL tag usage examples

We usually use the <ul><li> tags, but ...

Automatically load kernel module overlayfs operation at CentOS startup

To automatically load kernel modules in CentOS, y...

Detailed explanation of CSS float property

1. What is floating? Floating, as the name sugges...

Detailed explanation of linux crm deployment code

Linux basic configuration Compile and install pyt...