Home  >  Article  >  Web Front-end  >  CSS cascading property analysis: z-index and position

CSS cascading property analysis: z-index and position

王林
王林Original
2023-10-20 09:27:331465browse

CSS 层叠属性解析:z-index 和 position

CSS cascading attribute analysis: z-index and position

In CSS, z-index and position are two commonly used cascading attributes, used to control the position of elements. Stacking order and positioning. This article will analyze these two properties in detail and provide relevant code examples.

1. z-index attribute

The z-index attribute is used to control the stacking order of elements. It accepts an integer value as a parameter. The larger the value, the higher the element is displayed. By default, the z-index value of an element is 0.

Syntax: z-index: numerical value;

It should be noted that only positioned elements (i.e. elements whose position value is relative, absolute or fixed) can use the z-index attribute. The z-index attribute of a positioned element affects the display order of its children and other parent and sibling elements.

The following is an example that demonstrates the use of the z-index attribute:

<!DOCTYPE html>
<html>
<head>
<style>
#div1 {
  width: 200px;
  height: 200px;
  background-color: red;
  z-index: 1;
}

#div2 {
  width: 200px;
  height: 200px;
  background-color: blue;
  position: relative;
  top: 50px;
  left: 50px;
  z-index: 2;
}
</style>
</head>
<body>

<div id="div1"></div>
<div id="div2"></div>

</body>
</html>

In the above code, div1 and div2 are two positioning elements respectively, and the z-index value of div2 is larger. large, so div2 will cover and display above div1.

2. Position attribute

The position attribute is used to control the positioning method of elements. Common values ​​are static, relative, absolute and fixed.

  1. static: Default value. Elements follow normal document flow layout and do not undergo any special positioning.
  2. relative: relative positioning. The element is positioned relative to its normal position. The position of the element can be adjusted through the top, bottom, left, and right attributes.
  3. absolute: absolute positioning. An element is positioned relative to its nearest positioned ancestor, or its original containing block if there is no positioned ancestor.
  4. fixed: Fixed positioning. Elements are positioned relative to the browser window and their position does not change even if the page is scrolled.

The following is an example that demonstrates the use of the position attribute:

<!DOCTYPE html>
<html>
<head>
<style>
#div1 {
  width: 200px;
  height: 200px;
  background-color: red;
  position: relative;
  top: 50px;
  left: 50px;
}

#div2 {
  width: 200px;
  height: 200px;
  background-color: blue;
  position: absolute;
  top: 100px;
  left: 100px;
}
</style>
</head>
<body>

<div id="div1"></div>
<div id="div2"></div>

</body>
</html>

In the above code, div2 uses the position:absolute attribute to position it relative to div1. By adjusting the values ​​of the top and left attributes, the position of div2 can be changed.

Summary:

z-index and position are commonly used stacking properties in CSS. They can control the stacking order and positioning of elements. By rationally using these two attributes, rich and diverse page layout effects can be achieved.

The above is the analysis of CSS cascading properties z-index and position, as well as related code examples. Hope it helps.

The above is the detailed content of CSS cascading property analysis: z-index and position. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn