如何将 html div 重叠在另一个 div 上?

How to overlap a html div over another div?

为了在 html 中创建一个多部分下拉列表,我使用了一个普通的 select 和一个带有选项作为复选框的 div。当我单击 select 时,div 出现在下面元素的顶部。但我需要带复选框的 div,因为它与以下元素重叠。这是我的代码。

代码:

var expanded = false;

function showCheckboxes() {
  var checkboxes = document.getElementById("checkboxes");
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}
<form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
      <select>
        <option>Select an option</option>
      </select>
      <div class="overSelect"></div>
    </div>
    <div id="checkboxes">
      <label for="one">
        <input type="checkbox" id="one" />First checkbox</label>
      <label for="two">
        <input type="checkbox" id="two" />Second checkbox</label>
      <label for="three">
        <input type="checkbox" id="three" />Third checkbox</label>
    </div>
    <div>
    <button type="button">Submit</button>
    </div>
  </div>
</form>

如何更改代码以满足要求?

为您的 #checkboxes

使用 position: absolute;

var expanded = false;

function showCheckboxes() {
  var checkboxes = document.getElementById("checkboxes");
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  background: #fff;
  position: absolute;
  border: 1px #dadada solid;
  width: 100%;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}
<form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
      <select>
        <option>Select an option</option>
      </select>
      <div class="overSelect"></div>
      <div id="checkboxes">
        <label for="one">
        <input type="checkbox" id="one" />First checkbox</label>
        <label for="two">
        <input type="checkbox" id="two" />Second checkbox</label>
        <label for="three">
        <input type="checkbox" id="three" />Third checkbox</label>
      </div>
    </div>
    <div>
      <button type="button">Submit</button>
    </div>
  </div>
</form>