Use CSS's position
property and z-index
property
First, set the attribute of the second div
element to or . This makes the element a positioned element, allowing descendant elements to be positioned relative to it. position
relative
absolute
Then, set the attribute of the div
element to be suspended to and set < The value of the a i=4> attribute is greater than the value of the second element. position
absolute
z-index
div
For example, in the following example, the attribute of the second div
element is set to and is to be floated The element's attribute is set to , and the attribute is set to a value of :position
relative
div
position
absolute
z-index
1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="position: relative; width: 200px; height: 200px;
background-color: lightgray;">
<p>这是第二个 div 元素</p>
</div>
<div style="position: absolute; z-index: 2; top: 50px; left: 50px;
width: 100px; height: 100px; background-color: red;">
<p>这是要悬浮的 div 元素</p>
</div>
</body>
</html>