我仅使用纯CSS创建了一个下拉列表,但问题是它会将其他内容向下推。有人可以帮忙吗?

HTML:

 <div class="select">
    <input id="is-focus" type="radio" name="item" />
    <label for="is-focus"><span class="label"></span></label>
    <ul class="options">
        <li>
          <input type="radio" name="item" value="pie" id="pie" />
          <label for="pie" data-title="Pie">Pie</label>
        </li>
        <li>
          <input type="radio" name="item" value="cake" id="cake" />
          <label for="cake" data-title="Cake">Cake</label>
        </li>
    </ul>
  </div>


CSS:

.select {
  position: relative;
  overflow: hidden;
 }
.select .label {
  padding: 8px;
  max-width: 150px;
}
.select .label:after {
  content: "";
  position: absolute;
  left: 12px;
  top: 14px;
  border: 8px solid transparent;
  border-top-color: #999;
}
.select label {
  max-width: 150px;
  display: block;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 3px;
  padding: 8px;
  cursor: pointer;
}
.select .options {
  max-width: 220px;
  margin: 0;
  padding: 0;
  color: #666;
  height: 0;
  border-right: 1px dotted #999;
  border-left: 1px dotted #999;
  border-radius: 5px;
  max-height: 162px;
  overflow-y: auto;
  overflow-x: hidden;
}
.select .options:hover {
  height: auto;
  border-bottom: 2px solid #999;
}
.select .options li label {
  text-align: left;
  display: block;
  padding: 6px 220px 6px 15px;
  cursor: pointer;
  overflow: hidden;
  box-sizing: border-box;
}
.select .options li label:hover {
  background-image: linear-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0));
}
.select .options li [type="radio"] {
  position: absolute;
  display: block;
  left: -999px;
}
.select .options li [type="radio"]:checked ~ label:after {
  content: attr(data-title);
  display: block;
  position: absolute;
  top: 6px;
  left: 20px;
  padding: 5px 25px;
  color: #666;
  pointer-events: none;
  font-size: 80%;
  overflow: hidden;
}
.select #is-focus {
  position: absolute;
  display: block;
  opacity: 0;
  left: 150px;
}
.select #is-focus:focus ~ label span.label:after {
  border-top-color: transparent;
  border-bottom-color: #999;
  top: 0.3em;
}
.select #is-focus:focus ~ .options {
  height: auto;
  border-bottom: 2px solid #999;
}


这是我想要做的一个Codepen链接:
http://codepen.io/anon/pen/ogYKzQ

谢谢

最佳答案

将.options位置从静态更改为固定
并在菜单标签上添加白色背景。

关于html - 自定义下拉菜单将内容向下推,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27808773/

10-11 15:07