HTML Dropdown box shifting html elements downwards

雨燕双飞 提交于 2021-02-09 09:22:42

问题


So I'm trying to make a dropdown list overlap the elements below when expanded. Unfortunately, even when elements in the same stacking context have a position, and dropdown container div set to a z-index of 2, the issue still persists; instead of overlapping the elements below, it shifts them down. I have also attempted shuffling around the stacking context by adding the opacity property to various elements, but that hasn't worked either. The code can be seen below:

#firstone {
  z-index: 2;
}

#contain {
  height: 400px;
  width: 400px;
  background-color: rgba(100, 25, 25, 0.2);
}

body {
  text-align: center;
}

input:focus {
  outline: none;
}

.fancy_input_box {
  text-align: left;
  display: inline-block;
  margin: 40px 3% 1px;
  position: relative;
}

.fancy_input {
  font: 15px/24px "Lato", Arial, sans-serif;
  color: #aaa;
  box-sizing: border-box;
  letter-spacing: 1px;
  border: 0;
  padding: 7px 7px;
  border: 1px solid #ccc;
  position: relative;
  background: transparent;
}

.fancy_input:focus~.dropdown-content {
  display: block;
  cursor: default;
}

.fancy_input:focus~.dropdown-content>span {
  display: block;
  cursor: default;
}

.dropdown-content {
  display: none;
  background-color: rgba(0, 0, 0, 0.2);
  width: 222px;
  overflow: auto;
  position: relative;
}

.dropdown-content:active {
  display: block;
}

.dropdown-content>span {
  padding: 12px 16px;
  display: none;
}

.dropdown-content>span:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.fancy_input~.fancy_label {
  position: absolute;
  left: 14px;
  width: 100%;
  top: 10px;
  color: #aaa;
  letter-spacing: 0.5px;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
  z-index: -1;
}

.fancy_input:focus~.fancy_label {
  top: -18px;
  left: 1px;
  font-size: 12px;
  color: grey !important;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
}

.fancy_input:focus {
  border: 1px solid grey !important;
}

.fancy_input:not(focus):valid~.fancy_label {
  top: -18px;
  font-size: 12px;
  left: 1px;
  color: #aaa;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" href="test.css">
</head>

<body>
  <div id="contain">

    <div id="firstone" class="fancy_input_box">
      <input class="fancy_input" type="text" placeholder="" maxlength="25" size="25" required>
      <label class="fancy_label">Search</label>

      <div class="dropdown-content">
        <span>a</span>
        <span>b</span>
        <span>c</span>
        <span>d</span>
        <span>e</span>
      </div>
    </div>

    <div class="fancy_input_box">
      <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
      <label class="fancy_label">Thing</label>
    </div>

    <br>

    <div>
      <div class="fancy_input_box">
        <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
        <label class="fancy_label">stuff</label>
      </div>

      <div class="fancy_input_box">
        <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
        <label class="fancy_label">more stuff</label>
      </div>

      <div class="fancy_input_box">
        <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
        <label class="fancy_label">more stuff</label>
      </div>
    </div>

  </div>
</body>

</html>

回答1:


You dropdown content should be absolute positioned.

Update your dropdown styles as shown below :

.dropdown-content {
  display: none;
  background-color: rgba(0, 0, 0, 0.2);
  width: 222px;
  overflow: auto;
  position: absolute;
  left: 0;
  top: 100%;
}



回答2:


Try following code just set position absolute. also i change width 100% in dropdown-content div for good design.

#firstone {
  z-index: 2;
}

#contain {
  height: 400px;
  width: 400px;
  background-color: rgba(100, 25, 25, 0.2);
}

body {
  text-align: center;
}

input:focus {
  outline: none;
}

.fancy_input_box {
  text-align: left;
  display: inline-block;
  margin: 40px 3% 1px;
  position: relative;
}

.fancy_input {
  font: 15px/24px "Lato", Arial, sans-serif;
  color: #aaa;
  box-sizing: border-box;
  letter-spacing: 1px;
  border: 0;
  padding: 7px 7px;
  border: 1px solid #ccc;
  position: relative;
  background: transparent;
}

.fancy_input:focus~.dropdown-content {
  display: block;
  cursor: default;
}

.fancy_input:focus~.dropdown-content>span {
  display: block;
  cursor: default;
}

.dropdown-content {
  display: none;
  background-color: rgba(0, 0, 0, 0.2);
  width: 100%;
  overflow: auto;
  position: absolute;
  
}

.dropdown-content:active {
  display: block;
}

.dropdown-content>span {
  padding: 12px 16px;
  display: none;
}

.dropdown-content>span:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.fancy_input~.fancy_label {
  position: absolute;
  left: 14px;
  width: 100%;
  top: 10px;
  color: #aaa;
  letter-spacing: 0.5px;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
  z-index: -1;
}

.fancy_input:focus~.fancy_label {
  top: -18px;
  left: 1px;
  font-size: 12px;
  color: grey !important;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
}

.fancy_input:focus {
  border: 1px solid grey !important;
}

.fancy_input:not(focus):valid~.fancy_label {
  top: -18px;
  font-size: 12px;
  left: 1px;
  color: #aaa;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
}
<div id="contain">

    <div id="firstone" class="fancy_input_box">
      <input class="fancy_input" type="text" placeholder="" maxlength="25" size="25" required>
      <label class="fancy_label">Search</label>

      <div class="dropdown-content">
        <span>a</span>
        <span>b</span>
        <span>c</span>
        <span>d</span>
        <span>e</span>
      </div>
    </div>

    <div class="fancy_input_box">
      <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
      <label class="fancy_label">Thing</label>
    </div>

    <br>

    <div>
      <div class="fancy_input_box">
        <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
        <label class="fancy_label">stuff</label>
      </div>

      <div class="fancy_input_box">
        <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
        <label class="fancy_label">more stuff</label>
      </div>

      <div class="fancy_input_box">
        <input class="fancy_input" type="text" placeholder="" maxlength="4" size="10">
        <label class="fancy_label">more stuff</label>
      </div>
    </div>

  </div>



回答3:


just add position:absoute and top:100%; in your dropdown-content class

jsfiddle from your code

  .dropdown-content {
  display: none;
  background-color: rgba(0, 0, 0, 0.2);
  width: 222px;
  overflow: auto;
  position: absolute;
  left: 0;
  top: 100%;
  }


来源:https://stackoverflow.com/questions/52591295/html-dropdown-box-shifting-html-elements-downwards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!