Home >Web Front-end >CSS Tutorial >Different uses of z-index in css
Most CSS properties are easy to use. Often, when you use CSS properties on elements of a markup language, the results appear immediately as you refresh the page. Other CSS properties are more complex and will only work under given circumstances.
The Z-index attribute belongs to the latter group mentioned above. Z-index undoubtedly causes confusion (compatibility) and frustration (developer psychology) more frequently than any other property. But the funny thing is, once you really understand Z-index, you will find that it is a very easy to use attribute, and will provide a powerful help in solving many layout challenges.
In this article, we will explain exactly what Z-index is, why it is so poorly understood, and discuss some issues related to its practical use. We'll also describe some of the browser-to-browser differences you'll encounter, which are unique issues that exist in existing versions of IE and Firefox. This comprehensive perspective on the Z-index attribute will provide developers with a good foundation of confidence and powerful help when using the Z-index attribute.
What's this?
The Z-index attribute determines the cascading level of an HTML element. The stacking level of an element is relative to the element's position on the Z axis (as opposed to the X axis or Y axis). A higher Z-index value means that the element will be closer to the top in the stacking order. This layering sequence is presented along vertical thread axes.
To more clearly describe how Z-index works, the image above exaggerates the visual positioning of stacked elements.
Natural stacking order
In an HTML page, the natural stacking order (that is, the order of elements on the Z axis) is determined by many factors. The following is a list, which displays the list items in a stacking context (no suitable Chinese translation has been found yet, it should refer to the stacking environment where the stacked elements are located). These items are in this stacking context. The bottom of the environment. None of the items in this list have been assigned a Z-index attribute.
The background and border of the element will create a stacking context
Quote:
·Stacking contexts elements with negative values are arranged in the order of appearance (the further back, the higher the level )
·Unlocated, non-floating block-level elements are arranged in the order of appearance
·Unlocated, floating elements are arranged in the order of appearance
·Inline elements , arranged in the order of appearance
· Positioned elements, arranged in the order of appearance
The Z-index attribute, when used correctly, will change the natural stacking order.
Of course, unless the elements have been positioned to overlap each other, the stacking order of the elements will not be particularly obvious. Below, a BOX with negative margins is shown to illustrate the natural stacking sequence.
The above BOX is defined with different background and border colors, and the latter two are staggered and define a negative top margin, so we You can see the natural layering sequence. The gray BOX is in the first place in the mark, the blue BOX is in the second place, and the gold one is in the third place. The applied negative margins clearly indicate the fact that these elements do not have a Z-index property set; their stacking order is natural, or the default, compound rule. The phenomenon of interlacing is caused by negative margins.
Why does it create confusion?
Even though Z-index is not a difficult property to understand, it can cause many junior developers to get confused due to wrong assumptions. The confusion occurs because Z-index only works on elements that have the absolute, fixed, or relative positioning attributes explicitly defined.
To prove that Z-index only works on positioned elements, here are the same three BOXes that apply the Z-index attribute to try to break their natural stacking order.
The gray BOX has a Z-index value of "9999", the blue BOX has a Z-index value of "500", and the gold one has a Z-index value of "1 "Z-index value. Logically, you would think that the stacking order of these three BOXes would be reversed. But this is not the case, because none of these elements have the position attribute set.
Below are the same three BOX, each of which is set to position: relative. Their Z-index values are still set according to the above paragraph.
The result now is what we expected: the stacking order of these elements is reversed; the gray BOX is covered with blue, and the blue is covered with gold. .
Syntax
#grey_box {
width: 200px;
height: 200px;
border: solid 1px #ccc;
background : #ddd;
position: relative;
z-index: 9999;
}
#blue_box {
width: 200px;
height: 200px;
border: solid 1px #4a7497;
background: #8daac3;
position: relative;
z-index: 500;
}
#gold_box {
width: 200px;
height: 200px;
border: solid 1px #8b6125;
background: #ba945d;
position: relative;
z-index: 1;
}
Repeat Now, the Z-index attribute only works on elements that have the position attribute defined. This is something that is not taken seriously enough, especially by newbies.
UsingJavaScript
If you want to dynamically add the Z-index attribute to an element through JavaScript, its syntax can be used like most other CSS elements. Access is similar, using "camelCase" instead of hyphens in CSS properties, as shown in the following code.
var myElement = document.getElementById(”gold_box”);
myElement.style.position = “relative”;
myElement.style.zIndex = “9999″;
In IE and Improper parsing in Firefox (compatibility issue)
In some specific cases, there will be some minor inconsistencies in the parsing of the Z-index attribute in IE6, IE7 and Firefox2 versions.
The
The
drop-down menus that cover the use JavaScript to temporarily hide the
The z-index value of the gray BOX is "9999"; the z-index value of the blue BOX is "1". Both BOX are set to position. Therefore, the correct implementation should be that the gray BOX is overlaid on the blue one.
But in IE6 and IE7, we will see the blue BOX on top of the gray one. This is caused by the positioning of the outer parent container of the gray BOX being also set. Both browsers mistakenly "reset" the stacking context of the positioned parent container, but this should not be the case. The gray BOX has a very high Z-index value, and it should be above the blue BOX. Other browsers will resolve this issue correctly.
Negative values in Firefox 2:
In the Firefox 2 version, a negative Z-index value will cause the element to be behind the stacking context instead of the accepted background and Border such elements before stacking context. The screenshot below shows this Firefox 2 bug.
The following is the HTML version of the above screenshot (limited to the current blog, it cannot be as content as Smashing Magazine The code demo is displayed. If you need to view the original example, please go to the original article to view). If you view it in Firefox 3 or other browsers currently being used, you will see the normal parsing results: the background of the gray BOX (the stacking context of the element Base) appears below all physical objects, and the text inside the gray BOX appears on top of the blue BOX.
Display of common applications
Applying the z-index attribute to elements on the page can easily solve various problems. The layout challenge also allows designers to be more creative using cascading items in their designs.
Staggered sliding door menu:
A practical application case of this CSS property:CTCOnlineCME This website uses the Z-index attribute for the "being clicked" tab and clear interlaced PNG images, creating very good effects.
CSS bubble:
The Z-index property can also be used to implement CSS-based prompt bubbles, as shown belowtrentrichardson.comAs shown
Light Box:
If it were not for the z-index attribute being applied, Now there are not so many high-quality Light Box scripts available for free, such as FancyBox, a plug-in for JQuery.
Light box script uses a translucent PNG image to darken the background, and then takes a new element, often using a window-like p, to placed in the front row. The PNG that covers the screen and the p that follows both use the Z-index attribute to ensure that these two elements will be positioned above other elements on the page.
Dropdown menu:
A dropdown menu similar to Brainjar's classic Revenge of the Menu Bar uses Z-index to ensure that the menu's buttons and their dropdown items are on top of each other top.
Gallery effect picture display:
Combine JQuery animation and Z-index to create a slideshow or gallery style Excellent results. usejquery.com This demo in the website shows us the surprise that the wonderful combination of the two brings us.
Polaroid Photo Gallery by Chris Spooner Uses more powerful CSS3 to work with Z-index to create a mouse-swipe Super cool effect that will be re-coded when it expires.
In Fancy Thumbnail Hover Effect Soh Tanaka uses Query-based script to change the z-index assignment.
CSS experiments by Stu Nicholls:
Explained by Stu Nicholls on his website CSSplay There are many CSS cases. Below is some work on the z-index property.
CSS Image Map
CSS Game
##CSS imitation framework
##Enhanced version of cascading layout:
24 ways
This website uses Z-index as a tool to improve its template experience. It extends the length and width of the year and date to be equal to the outer container of the website and interweaves with each other, creating a very interesting effect.
#Janko At Warp Speed Z-index is used in "Pick Column".
and applies it Arrived at the ringvemedia.com website. Apply z-index to the content container to ensure it appears on top of the image that looks like a "background" but isn't.
Cascading relationships in CSS is a complex topic. This article does not intend to discuss all the details of this topic, but to conduct an in-depth discussion of how Z-index affects the stacking order of our web pages. What is said here, when truly fully understood, we will find that this CSS property is so powerful.
Beginners should now have a very good understanding of this property and avoid many of the problems that often arise in its use. In addition, experienced developers will have a deeper understanding of how to use Z-index correctly to avoid many layout problems, and open the door to creating more CSS works of art.The above is the detailed content of Different uses of z-index in css. For more information, please follow other related articles on the PHP Chinese website!