③. CSS3 and the cascade context of the new era
The advent of CSS3, in addition to bringing new attributes, also challenged many of the rules of the past. For example, CSS3 the transform
effect of overflow hiding on position:fixed positioning. And here, the cascading context of this piece of the impact is more extensive and significant.
As follows:
z-index
The value is not an auto
flex
item (parent element display:flex|inline-flex
).
- The value of the element
opacity
is not 1
.
- The value of the element
transform
is not none
.
- The element
mix-blend-mode
value is not normal
.
- The value of the element
filter
is not none
.
- The value of the element
isolation
is isolate
.
will-change
The specified property value is any one of the above.
- The element is
-webkit-overflow-scrolling
set to touch
.
Basically every item has a lot of slots.
1. Display:flex|inline-flex and cascading contexts
Note that the rules here are somewhat complex. To meet two conditions to form a cascading context: Condition 1 is the parent needs to be display:flex
or display:inline-flex
level, condition 2 is the z-index of the child element is not auto
, must be a numeric value. At this point, the child element is a cascading context element, yes, note that it is a child element, not a flex parent element.
Seeing is believing, let's give you an example.
The following HTML and CSS code:
<div class= "box" > <div> </div></div>
. box { }.box > div {background-color:blue; z-index:1;} /* At this time the div is a normal element, Z-index invalid */.box > div > img { position:relative; Z-index:-1; right: -150px; /* Note that this is a negative z-index */}
The results are as follows:
Will find that the sister ran under the blue background. Why is it? The Cascade sequence diagram can find the answer, as follows:
You can see that the stacking order of the negative z-index is below the block horizontal element, and the blue background div
element is a common element, so the sister crosses through the past and displays behind the blue background.
Now, with our CSS trimmer, add display:flex
the following:
. box {Display:flex;}. Box > div {background-color:blue; z-index:1;} /* At this point the div is a cascading context element, while the z-index takes effect */.box > div > img { position:relative; Z-index:-1; right: -150px; /* Note that this is a negative z-index */}
Results:
Will find, sister on the blue background shows, why? The Cascade sequence diagram can find the answer, as follows:
You can see that the stacking order of negative values z-index
is above the current first parent cascade context element, and at this point the z-index
1
value of the parent element of the blue background of the value <div>
display
is flex
, suddenly ladder into a cascading context element, so, The picture is shown above the blue background. This behavior also confirms that the cascading context element is a flex
child element, not a flex
container element.
In addition, this example also overturns our traditional understanding of the pair z-index
. In the CSS2.1 era, z-index
attributes had to be used in conjunction with positioned elements, but in the CSS3 world, non-locating elements could also be z-index
easily and happily based.
2. Opacity and cascading contexts
We look directly at the code, and the same principle as the example above does not explain.
The following HTML and CSS code:
<div class= "box" > </div>
. box {background-color:blue; }. Box > img { position:relative; Z-index:-1; right: -150px;}
The results are as follows:
Then the price transparency, for example 50% transparent:
. box {background-color:blue; opacity:0.5; }. Box > img { position:relative; Z-index:-1; right: -150px;}
The results are as follows:
The reason is that the translucent elements have a cascading context, sister pictures z-index:-1
can not penetrate, so, on the blue background is shown obediently.
3. Transform and cascading contexts
Elements that have the transform transform applied also have a menu context.
We directly look at the results after the application, the following CSS code:
. box {background-color:blue; transform:rotate (15deg); }. Box > img { position:relative; Z-index:-1; right: -150px;}
The results are as follows:
Sister is also above the blue background.
4. Mix-blend-mode and cascading contexts
mix-blend-mode
Similar to the blending mode in PS, previously specifically an article Introduction-"CSS3 Mixed Mode Mix-blend-mode Introduction".
element and white background blend. Either way, it's all white, or there's no change. In order to make people feel intuitive, so the following example I specifically added an original tile background:
. box {background-color:blue; mix-blend-mode:darken; }. Box > img { position:relative; Z-index:-1; right: -150px;}
The results are as follows:
It is important to note that currently, IE (including IE14) is not supported mix-blend-mode
, so if you want to see your sister on the background color, please use Chrome or Firefox.
Similarly, because the blue background element is upgraded to a cascading context, it z-index:-1
cannot penetrate and is displayed on a blue background.
5. Filter and Cascade context
Here is the filter
standard filter in CSS3, not those of the old IE era, although the purpose is similar. Similarly, I have mentioned before, such as the grayscale of the sample or the glass effect of the picture.
We use the usual blur effect to indicate the following:
. box {background-color:blue; Filter:blur (5px); }. Box > img { position:relative; Z-index:-1; right: -150px;}
The results are as follows:
Well, sure enough, you guessed right, sister blue bed lying, just your glasses picked, see some not real.
6. Isolation:isolate and cascading contexts
isolation:isolate
This statement was mix-blend-mode
born. By default, the mix-blend-mode
z-axis is blended with all the elements stacked below, what if we don't want a cascade of elements to participate in the mix? is to use isolation:isolate
. Because of the long story, I specifically wrote an article: "Understanding the performance and role of CSS3 Isolation:isolate", explaining the principle of its barrier mixing mode, we suggest you look.
To demonstrate this effect, I need to redesign the following HTML structure:
<div class= "box" > </div>
The main code of CSS is as follows:
. mode {/ * Vertical Sister absolute positioning, simultaneous blending mode */ position:absolute; mix-blend-mode:darken;} . box { background:blue; }. Box > img { position:relative; Z-index:-1;}
The structure is as follows:
Will find that the cross sister has been mixed mode. At this point, we add the container to isolation:isolate
the sister, as shown in the following CSS:
. mode {/ * Vertical Sister absolute positioning, simultaneous blending mode */ position:absolute; mix-blend-mode:darken;} . box { background:blue; isolation:isolate; }. Box > img { position:relative; Z-index:-1;}
The result is:
Will find the cross of the sister ran to the blue background. This indicates that a cascading context has indeed been created.
7. Will-change and cascading contexts
will-change
If you have a classmate who doesn't understand it, see the article I wrote earlier: "Use CSS3 will-change to improve rendering performance with page scrolling, animations, and more."
are similar to the demo code:
. box {background-color:blue; will-change:transform; }. Box > img { position:relative; Z-index:-1; right: -150px;}
The results are as follows:
As expected, her sister was on a blue background.
Vii. stacking context and stacking orderThis article has repeatedly mentioned that once a common element has a cascading context, its stacking order becomes higher. Where exactly is the stacking order?
There are two kinds of situations to discuss here:
- If the cascading context element does not depend on
z-index
numeric values, its stacking order is z-index:auto
considered to be a z:index:0
level;
- If the cascading context element relies on
z-index
numeric values, its stacking order is determined by the z-index
value.
As a matter of fact, the Cascade Order table we provide above actually lacks other important information. I also took the time to redraw a more complete 7 hierarchy sequence diagram (same copyright, commercial please buy, can get no watermark large image):
Do you know why positioning elements are stacked on top of ordinary elements?
The root cause is that once the element becomes a positioning element, it z-index
will automatically take effect, at which point it z-index
is the default, that is, the auto
0
level, according to the above cascading order table, will overwrite inline
or block
or float
element.
Instead of supporting the Z-index's cascading context element's natural z-index:auto
level, it means that cascading context elements and positioning elements are stacked sequentially, so when they cascade, they follow the "catch up" rule.
We can speed test under:
Will find that the two styles are exactly the same, only in the DOM stream position is not the same, resulting in their cascading performance is not the same, the back of the sister lying on the front sister's body. This also shows that the stacking order of the cascading context elements is z-index:auto
level.
Z-index Values and stacking order
If the element supports Z-index values, then the stacking order will be good to understand, compared to the size of the small pot friends will be, in essence, the application of the "who the big who on" the guidelines. In the past, we only had to care about the z-index of the positioning elements, but in the CSS3 era, Flex children also supported z-index
, so we face the situation more complex than before. However, the good thing is that the rules are the same, for z-index
the use and performance of the same, apply the above 7 hierarchy stack table is OK.
Similarly, for a simple example, look at z-index:-1
and z-index:1
change the effect on the Cascade performance, the following two paragraphs of HTML:
<div style= "Display:flex; Background:blue; " > </div>
<div style= "Display:flex; Background:blue; " > </div>
Finally, it will be found, z-index:-1
ran to the background color of the facet, and high above z-index:1
.
An interesting display phenomenon related to cascading contexts
In the actual project, we may gradually use the CSS3 Fadein fade-in animation effect enhancement experience, so we may encounter a similar phenomenon:
You can click here: CSS3 Fadein fade-in animation animation interesting phenomenon
There is an absolutely positioned black translucent layer overlaid on the picture, which is shown by default:
However, once the picture starts to walk Fadein fade out of the CSS3 animation, the text runs to the back of the picture:
Why is that?
In fact, learning the contents of this article, it is very simple! Fadein Animation Essence is opacity
the change of transparency:
@keyframes FadeIn { 0% { opacity:0; } 100% { opacity:1; }}
You know, opacity
when the value is not 1
, there is a cascading context, stacking order is a z-index:auto
level, with no z-index
value of absolute
absolute positioning elements are equal. and the text element in this demo is in front of the picture element, so, when the CSS3 animation is not the final moment opacity: 1
, the picture behind the Dom stream will follow the "catch up" rule, covering the text.
That's why, so it's easy for us to solve the problem.
1. Adjust the sequence of Dom flow;
2. Improve the stacking order of text, for example, set z-index:1
;