How to block alert z index. Z-index positioning property in CSS. d Z axis representation

Most CSS properties are easy to understand. Often, applying any of them to a markup element gives immediate results - you just need to refresh the page, and you can see the effect of the application. However, there are some properties that are more complex and will only work under certain circumstances.

The z-index property belongs to the second group, which undoubtedly causes more confusion than any other. Ironically, however, with a full understanding of what z-index is, it is very easy to use and is often an effective way to overcome many layout problems.

In this article, we will look at what z-index is, what problems there are in understanding it, and also discuss some examples of its use. We will also consider the difference in how browsers handle it, especially in previous versions of Internet Explorer and FireFox.

So what is this property?

The z-index property determines the level of placement in the stack, the depth of the html element. "Depth level" means the position of the element along the Z axis (as perpendicular to the X and Y axes of the screen). The higher the z-index value, the taller the element will be.

Natural arrangement of elements

On a page, the normal placement of elements (by which I mean along the Z axis) is determined by several factors. Below is a list of these factors, starting with the lowest elements. This list assumes that no element is explicitly assigned a z-index property.

  1. The background and borders of the element defining the stack context.
  2. Elements with negative stack context, in display order.
  3. Unpositioned (position: static), as well as without the float property set (float: none) block elements (display: block), in display order.
  4. Unpositioned, float-property-set block elements, in display order.
  5. Inline elements, in display order.
  6. Elements with the position property set, in display order.

Correct application of the z-index property can change the natural position in the stack.

Of course, the order of elements in a stack is not obvious until you need to display the elements one above the other. Therefore, to see the normal order of elements, the example below uses negative margins.

These elements have different background and border colors, and the last two are offset due to the negative margin property being set. This way you can see the natural placement of elements, each next one is “above” the previous one. These elements do not have a z-index property set, and their stacking order is natural.

Where might the problems be?

Let's look at the most popular problem among novice developers. The thing is that the z-index property only works on elements that have their position property set to absolute , fixed or relative .

To demonstrate this, let's display the same three elements, but with the z-index property set to try to change the order along the Z axis.

Let's set the gray element's z-index to 9999, the blue element to 500, and the brown element to 1. It's logical to expect the order to change. But not in this case, since the position property is static by default.

Let's set the position property to relative and see what happens:

But now we have the expected result. The order of the elements has changed, the gray element is on top of all, and the brown one is at the very bottom.

Syntax

The z-index property affects both block elements and inline elements. The value can be a positive or negative number, or the default is auto . The default value means that the element is at the same level as its parent.

Below is the CSS for the third example, where the z-index property is applied correctly:

#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 ; )

Once again, especially for those new to CSS, the z-index property will not work unless you set the position property.

Use in javascript

You can affect the z-index property dynamically using javaScript. The syntax is similar to that of most CSS properties, using camel notation to replace the hyphen in CSS properties.

Var myElement = document.getElementById("gold_box"); myElement.style.position = "relative"; myElement.style.zIndex = "9999";

Incorrect implementation in IE and FireFox

In some cases, in IE6, IE7 and FireFox 2, there is an incorrect implementation of the z-index property.

Select element in IE6

In Internet Explorer 6, the select element is a windows control, for this reason it is always displayed on top of all elements, ignoring the normal placement order, as well as the position and z-index properties. The problem is shown in the picture below:

The select element is the first element in the document, its z-index property is set to 1, and its position is set to relative . The div is displayed after the select and its z-index is "9999". Based on all this, the div should be located above the select , as it happens in other browsers:

Choose Year - 2009 2010 2011

If you're not viewing this article in IE6, you'll see that the div is positioned above the select .

This IE6 bug is a big problem for dropdown menus when they need to overlap the select element. A solution might be to use JavaScript to temporarily hide the selectors, and then show them again after the menu disappears. Another solution could be to use an iframe.

Positioned parents in IE6/IE7

Internet Explorer versions 6 and 7 do not correctly reset the stack context with respect to the closest positioned parent. To demonstrate this error, we will again display two divs. But this time we'll wrap the first one in a positioned element.

The gray element has a z-index of 9999, the blue element has a z-index of 1, both elements are positioned. Therefore, if implemented correctly, the gray element will appear on top of the blue one.

If you open this page in IE6 or IE7, you will see that the blue element overlaps the gray one. This happens because the gray element is wrapped in another div whose position is set to relative .

Both browsers "reset" stack context on relatively positioned elements, even though they shouldn't. The gray element has a higher z-index than the blue element, so it should appear higher. There is no such problem with other browsers.

Negative values ​​in FireFox 2

In FireFox 2, elements with a negative z-index are below the stacking context, instead of being above the background and borders of the element that forms the stacking context. You can see an example in the picture:


Below is the html version

As you can see, the background of the gray element (which forms the stack context) is below the blue one, and the text (which is the inline element in the gray element) is above it, following the natural layout rules described above.

Examples of using

The original article gives many examples of using the property. Honestly, I'm too lazy to translate this, basically it's a screenshot, a small comment and a link. If you really need it, then write, I’ll make time for it.

Any positioned elements on a web page can be stacked on top of each other in a specific order, thereby simulating a third dimension perpendicular to the screen. Each element can be either below or above other objects on the web page, their placement along the z-axis is controlled by z-index . This property only works for elements whose position value is absolute , fixed , or relative .

brief information

Designations

DescriptionExample
<тип> Indicates the type of the value.<размер>
A && BThe values ​​must be output in the order specified.<размер> && <цвет>
A | BIndicates that you need to select only one value from the proposed ones (A or B).normal | small-caps
A || BEach value can be used independently or together with others in any order.width || count
Groups values.[ crop || cross ]
* Repeat zero or more times.[,<время>]*
+ Repeat one or more times.<число>+
? The specified type, word, or group is optional.inset?
(A, B)Repeat at least A, but no more than B times.<радиус>{1,4}
# Repeat one or more times separated by commas.<время>#
×

Values

Integers (positive, negative and zero) are used as values. The higher the value, the higher the element is compared to those elements that have a lower value. If the z-index value is equal, the element in the foreground is the one described below in the HTML code. It is acceptable to use a negative value.

In addition to numeric values, auto is used - the order of elements in this case is built automatically, based on their position in the HTML code and their belonging to the parent, since child elements have the same number as their parent element.

Example

Card order

In this example, when you hover over the map, it comes to the front edge, partially overlapping the rest of the images (Fig. 1).

Rice. 1. Changing the order of cards

Object Model

An object.style.zIndex

Note

List created with