代码之家  ›  专栏  ›  技术社区  ›  learner

创建多个子div

  •  -1
  • learner  · 技术社区  · 4 年前

    我必须创建一个在另一个里面的div:

    enter image description here

    我尝试了这个代码,但不明白解决这个问题的正确方法是什么:

    <div style="background-color: yellow;">
      One
      <div style="background-color: blue;">
        Two
        <div style="background-color: black;">
          Three
          <div style="background-color: grey;">
            Four
          </div>
        </div>
      </div>
    </div>

    这是我的代码输出:

    enter image description here

    1 回复  |  直到 4 年前
        1
  •  0
  •   ali memar    4 年前

    我认为这个代码就是你想要的

    div {
      background-color: yellow;
      width: 200px;
      height: 200px;
      padding-left: 30px;
    }
    
    div div {
      background-color: blue;
      width: 150px;
      height: 150px;
      padding-top: 10px;
    }
    
    div div div {
      background-color: black;
      width: 100px;
      height: 100px;
      padding-top: 30px;
    }
    
    div div div div {
      background-color: grey;
      width: 25px;
      height: 25px;
    }
    <div>
      <div>
          <div>
              <div></div>
          </div>
      </div>
    </div>
        2
  •  0
  •   Sohan Arafat    4 年前

    您必须在代码中放入z索引:

    <div style="background-color: blue;z-index:8;">
        Two
        <div  style="background-color: black;z-index:7;">
            Three
            <div  style="background-color: grey;z-index:6;">
                Four
            </div>
        </div>
    </div>
    </div>
    

    然后您也可以在代码中添加框效果。 要更好地理解盒子效应,请参阅 this

        3
  •  0
  •   j08691    4 年前

    你走在正确的轨道上!你的HTML是正确的,你只需要CSS。试着弄一些填充物来把它做好。

    div {
      padding: 30px;
    }
    <div style="background-color: yellow;">
      One
      <div style="background-color: blue;">
        Two
        <div style="background-color: black;">
          Three
          <div style="background-color: grey;">
            Four
          </div>
        </div>
      </div>
    </div>

    之所以看起来这些块是一个在另一个上面的,只是因为文本给了div一些高度。另一个是他们共享边界。填充会将这些边界拉开,你会开始看到你的元素实际上处于你需要的精确位置。

        4
  •  0
  •   skaz    4 年前

    编辑:你说创建,所以我认为你想通过编程在彼此内部创建div。如果你只是想创建一个包含div的HTML,那么你已经正确地设置了HTML,你只需要将每个div的宽度和高度设置为小于其父div的100%(请参阅我的CSS,将宽度和高度设为90%。我会保留下面的代码,以防它对你有任何帮助。

    附加片段向您展示如何在div中创建div。更重要的是,它向您展示了如何找到最深的div,这样您就可以在该div中添加一个新的div。祝您好运。

    const addDivButton = document.querySelector('.add_div_button');
    const mainDiv = document.querySelector('.main_div');
    addDivButton.addEventListener('click', e => {
        allChildren = mainDiv.querySelectorAll('div');
        const allChildrenArray = Object.values(allChildren)
        let lastChild;
        let newChild = document.createElement('div')
        newChild.id = `child_${allChildrenArray.length+1}`
        if (allChildren.length > 0) {
            lastChild = allChildrenArray[allChildrenArray.length - 1]
        } else {
            lastChild = mainDiv
        }
        if (getComputedStyle(lastChild).backgroundColor === 'rgb(80, 80, 80)') {
            newChild.style.backgroundColor = 'rgb(175, 175, 175)'
        } else {
            newChild.style.backgroundColor = 'rgb(80, 80, 80)'
        }
        lastChild.append(newChild)
    })
    .main_div {
        height: 90vh;
        width: 90vw;
        background-color: rgb(175, 175, 175);
    }
    
    .main_div div {
        width: 90%;
        height: 90%;
    }
    <button class="add_div_button">Click to Add Div</button>
        <div class="main_div">
            
    
        </div>