跨越父按钮的 100% 高度

LeB*_*eau 7 css height button

我有以下标记

<button class="filter"><div class="radio"><div class="circle"></div></div> <span>Account Management</span></button>
Run Code Online (Sandbox Code Playgroud)

和 CSS

.filter {
    font-size: 3vw;
    text-align: left;
    line-height: 1.6;
    padding: 0px;
    display: block;
    height:auto;
    overflow: hidden;
    margin-bottom: 3px;
}
.filter span {
    background: $leithyellow;
    height: 100%;
    overflow:auto;
    display: block;
    width: calc(100% - 60px);
    float: left;
    margin-left:10px;
    padding-left:20px;
}
Run Code Online (Sandbox Code Playgroud)

我无法将跨度扩展到按钮的 100% 高度。这能做到吗?

Pra*_*man 6

仅当为祖先正确定义高度时,高度才适用。如果你想让高度起作用,那是一个棘手的问题。您可以使用我的最爱之一,但您需要确保它适用于所有情况:

  1. position: relative;父母。
  2. position: absolute;需要满height和的元素width
  3. 给出元素,0所有边的值。

片段

.parent {
  position: relative;
  width: 100px;
  height: 50px;
  background: red;
}
.parent .child {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: skyblue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <span class="child"></span>
</div>
Run Code Online (Sandbox Code Playgroud)

在上面的代码段中,注意到这也可以工作,如果你给出:

.parent {
  position: relative;
  width: 100px;
  height: 50px;
  background: red;
}
.parent .child {
  position: absolute;
  width: 100%;
  height: 100%;
  background: skyblue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <span class="child"></span>
</div>
Run Code Online (Sandbox Code Playgroud)

这种方法的一个好处是,您不需要使用危险的calc

.parent {
  position: relative;
  width: 150px;
  height: 50px;
  background: red;
}
.parent .child {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 60px;
  background: skyblue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <span class="child"></span>
</div>
Run Code Online (Sandbox Code Playgroud)

注意:在相关说明中,您还可以查看此问题和答案:Calc() 替代固定侧边栏与内容?


Jam*_* H. 5

  1. 设置display: flex为父
  2. align-self: stretch为孩子设置

这将拉伸子 div/button 的高度以适应其父级的高度,而无需任何技巧。

通过使用position: absolute而不是flex-box,最终当您添加更多内容或稍后重新安排将是噩梦时,它最终不会很好。