咨询一个CSS层定位的问题?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <style type="text/css">
        .container {
            position: relative;
        }

        .nobox {
            width: 100px;
            height: 100px;
            background-color: green;
        }

        .box {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: red;
        }


        .boxs {
            width: 100px;
            height: 100px;
            background-color: yellow;
        }
    </style>
    <div class="container">
        <div class="nobox">Box 0</div>
        <div class="box">Box 1</div>
        <div class="boxs">Box 2</div>
    </div>
</body>

</html>

image.png

Box 2跑到Box 1下面去了,我需要的是Box 2在Box 1下面的地方,而不是被其遮挡?

阅读 1.8k
4 个回答
✓ 已被采纳

好想看没啥好办法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<style type="text/css">
    .container {
        position: relative;
    }

    .nobox {
        width: 100px;
        height: 100px;
        background-color: green;
    }

    .box {
        position: absolute;
        width: 100px;
        height: 100px;
        background-color: red;
    }


    .boxs {
        position: absolute;
        top: 100px;
        width: 100px;
        height: 100px;
        background-color: yellow;
    }
</style>
<div class="container">
    <div class="nobox">Box 0</div>
    <div class="box">Box 1
        <div class="boxs">Box 2</div>
    </div>

</div>
</body>

</html>

这样符合你的要求不

给box1 加个 z-index: -1;

新手上路,请多包涵

把父元素的高度定死,把Box2定位定下去,absolute导致的高度塌陷css其他方法没啥用

<div class="container">
  <div class="nobox">Box 0</div>
  <div class="box">Box 1</div>
  <div class='boxs'></div>
  <div class="boxs">Box 2</div>
</div>

image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏