html - flexbox按钮的子节点不以百分比应用高度
问题描述
我有以下代码
.flex {
display: flex;
width: 100%;
height: 48px;
background: black;
flex-direction: row
}
.flex-item {
height: 100%;
background: grey
}
<button class="flex">
<div class="flex-item">
Click Me
</div>
</button>
问题是,父节点<button>
带有flex-direction: row
,子元素不接受百分比高度。百分比高度仅在父节点为<div>
解决方案
* {
margin: 0;
padding: 0;
}
.flex {
display: flex;
width: 100%;
height: 48px;
background: black;
flex-direction: row;
border: none;
}
.flex-item {
height: 100%;
background: grey;
}
<button class="flex">
<div class="flex-item">
Click Me
</div>
</button>
但如果您使用flex
,最好使用它的功能添加到父级
align-items: stretch;
* {
margin: 0;
padding: 0;
}
.flex {
display: flex;
width: 100%;
height: 48px;
background: black;
flex-direction: row;
align-items: stretch;
border: none;
}
.flex-item {
background: grey;
}
<button class="flex">
<div class="flex-item">
Click Me
</div>
</button>
推荐阅读
- excel - SumIF + 索引 + 匹配公式
- ios - 如何使用 Alamofire 将参数发布到服务器?
- swift - 无法在 swift 4 中解决此 Null 问题?
- angular - 角度 CookieService 实例启动错误
- c# - 将方法传递给新表单的控件
- c# - 如何在 button_click 方法中使用文本框数组?
- javascript - 导入默认类不会让我使用它的功能
- javascript - Javascript Azure 函数输出进入死信队列
- c# - 无法通过 Id Asp.Net Mvc 从 DropdownList 中获取名称
- php - Laravel 5.6 中带有 2 个表的 json 响应