搜索

首页  >  问答  >  正文

Div容器为何可见?

我正在尝试将3个div放入1个div中。根据我的代码,我试图让Div 3成为Div 4-6的容器。然而,尽管Div 5和Div 6的代码相同,但它们重叠在一起。

<!DOCTYPE html>
<html>

<head>
<style>
    #div1 {
        background-color: blue;
        width: 60%;
        height: 400px;
        text-align: center;
        margin: auto;
    }

    #div2 {
        background-color: red;
        width: 90%;
        height: 300px;
        text-align: center;
        margin: auto;
    }

    #div3 {
        background-color: gray;
        width: 95%;
        height: 200px;
        text-align: center;
        margin: auto;
    }

    #div4 {
        background-color: yellow;
        width: 20%;
        height: 100%;
        text-align: center;
    float:left;
    }

    #div5 {
        background-color: green;
        width: 40%;
        height: 100%;
        text-align: center;
    float:left;
    }

    #div6 {
        background-color: purple;
        width: 40%;
        height: 100%;
        text-align: center;
    float:left;
    }

</style>
</head>

<body>

    <div id="div1">test 1
    <div id="div2">test 2
    <div id="div3">test 3
    <div id="div4">test 4</div>
    <div id="div5">test 5</div>
    <div id="div6">test 6</div>
    </div>
    </div>
    </div>

</body>
</html>

显示了test 3和test 6,但我不想看到test 3

我的教授帮我检查了代码,并说问题出在我的浏览器上。我真的不知道该怎么办。

我尝试使用flex,但所有内容都跑到了div之外。我尝试更改div 6的浮动位置,但没有任何变化。我尝试了我学过的所有方法,但都没有起作用。

P粉539055526P粉539055526531 天前602

全部回复(2)我来回复

  • P粉982009874

    P粉9820098742023-09-10 00:49:59

    如果你像这样使用flex,除了需要考虑DIV 3的文本外,它很容易修复。使用不同的flex方向的另一个包装器可以解决这个问题。

    <!DOCTYPE html>
    <html>
    <head>
    <style>
        #div1 {
            background-color: blue;
            width: 60%;
            height: 400px;
            text-align: center;
            margin: auto;
        }
    
        #div2 {
            background-color: red;
            width: 90%;
            height: 300px;
            text-align: center;
            margin: auto;
        }
    
        #div3 {
            background-color: gray;
            width: 95%;
            height: 200px;
            text-align: center;
            margin: auto;
            display:flex;
        }
    
        #div4 {
            background-color: yellow;
            width: 20%;
            height: 100%;
            text-align: center;
    
        }
    
        #div5 {
            background-color: green;
            width: 40%;
            height: 100%;
            text-align: center;
    
        }
    
        #div6 {
            background-color: purple;
            width: 40%;
            height:100%;
            text-align: center;
        }
    </style>
    </head>
    <body>
    <div id="div1">测试 1
    <div id="div2">测试 2
    <div id="div3">
    <div id="div4">测试 4</div>
    <div id="div5">测试 5</div>
    <div id="div6">测试 6</div>
    </div>
    </div>
    </div>
    </body>
    </html>

    回复
    0
  • P粉551084295

    P粉5510842952023-09-10 00:37:36

    DIV 4、5和6具有float: left属性,并围绕着文本“test 3”浮动。如果删除该文本,则DIV 3将不再可见:

    #div1 {
      background-color: blue;
      width: 60%;
      height: 400px;
      text-align: center;
      margin: auto;
    }
    
    #div2 {
      background-color: red;
      width: 90%;
      height: 300px;
      text-align: center;
      margin: auto;
    }
    
    #div3 {
      background-color: gray;
      width: 95%;
      height: 200px;
      text-align: center;
      margin: auto;
    }
    
    #div4 {
      background-color: yellow;
      width: 20%;
      height: 100%;
      text-align: center;
      float:left;
    }
    
    #div5 {
      background-color: green;
      width: 40%;
      height: 100%;
      text-align: center;
      float:left;
    }
    
    #div6 {
      background-color: purple;
      width: 40%;
      height: 100%;
      text-align: center;
      float:left;
    }
    <div id="div1">test 1
      <div id="div2">test 2
        <div id="div3">
          <div id="div4">test 4</div>
          <div id="div5">test 5</div>
          <div id="div6">test 6</div>
        </div>
      </div>
    </div>

    回复
    0
  • 取消回复