Front-end: Let a div float above another div

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. positionrelativeabsolute

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. positionabsolutez-indexdiv

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 positionrelativedivpositionabsolutez-index1

<!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>

Guess you like

Origin blog.csdn.net/weixin_46001736/article/details/134838021