如何在卡片底部添加下拉菜单

时间:2019-01-26 12:30:04

标签: html css flexbox

我用HTML,CSS制作了一张flexbox卡。我应该在页脚部分下方的卡片底部添加一个下拉菜单,单击该按钮即可显示该菜单。我尝试过但无法获得与卡片相同宽度且位置正确的菜单。我该如何实现?

function showMenu() {
  document.getElementById('dropdown').classList.toggle('show');
}
.card {
  display: flex;
  flex-direction: column;
  height: 300px;
  width: 200px;
  border: 1px solid black;
  position: relative;
}

.card .header {
  display: flex;
  flex: 2;
  justify-content: center;
  align-items: center;
}

.card .body {
  display: flex;
  flex: 5;
  justify-content: center;
  align-items: center;
  border-bottom: 1px solid black;
  border-top: 1px solid black;
}

.card .footer {
  display: flex;
  flex: 2;
  justify-content: center;
  align-items: center;
}

.dropdown-content {
  display: none;
  flex: 2;
  position: absolute;
  background-color: blue;
  overflow: none;
  z-index: 999;
  color: #ffffff;
  flex-direction: column;
}

.show {
  display: block;
}
<div class="card">
  <div class="header">
    Header
  </div>
  <div class="body">
    Body
  </div>
  <div class="footer">
    <button onclick="showMenu()">Toggle Menu</button>
  </div>
  <ul id="dropdown" class="dropdown-content">
    <li>Menu 1</li>
    <li>Menu 2</li>
    <li>Menu 3</li>
    <li>Menu 4</li>
  </ul>
</div>

我需要与卡大小相同且位于页脚下方的下拉菜单

1 个答案:

答案 0 :(得分:0)

在菜单中添加top: 100%box-sizing: border-boxmargin: 0

function showMenu() {
  document.getElementById('dropdown').classList.toggle('show');
}
.card {
  display: flex;
  flex-direction: column;
  height: 300px;
  width: 200px;
  border: 1px solid black;
  position: relative;
}

.card .header {
  display: flex;
  flex: 2;
  justify-content: center;
  align-items: center;
}

.card .body {
  display: flex;
  flex: 5;
  justify-content: center;
  align-items: center;
  border-bottom: 1px solid black;
  border-top: 1px solid black;
}

.card .footer {
  display: flex;
  flex: 2;
  justify-content: center;
  align-items: center;
}

.dropdown-content {
  margin: 0;
  box-sizing: border-box;
  display: none;
  position: absolute;
  top: 100%;
  width: 100%;
  flex: 2;
  background-color: blue;
  overflow: none;
  z-index: 999;
  color: #ffffff;
  flex-direction: column;
}

.show {
  display: block;
}
<div class="card">
  <div class="header">
    Header
  </div>
  <div class="body">
    Body
  </div>
  <div class="footer">
    <button onclick="showMenu()">Toggle Menu</button>
  </div>
  <ul id="dropdown" class="dropdown-content">
    <li>Menu 1</li>
    <li>Menu 2</li>
    <li>Menu 3</li>
    <li>Menu 4</li>
  </ul>
</div>