css - 如何让 CSS 网格填充父元素的高度?
问题描述
我希望使用 CSS 网格制作一个按钮,元素填充容器的整个高度:
button {
display: grid;
height: 50px;
grid-auto-flow: column;
align-items: center;
}
.one {
color: white;
background-color: purple;
border-right: 1px solid red;
}
<button>
<span class="one">one</span>
<span class="two">two</span>
</button>
从检查员可以看出,这是一行,没有填满容器的高度:
我想让网格填充容器,所以容器的整个左侧都是紫色的。
解决方案
您可以height: 100%;
对网格内的任何项目执行此操作
工作示例:
button {
display: grid;
height: 50px;
grid-auto-flow: column;
align-items: center;
}
.one {
color: white;
background-color: purple;
border-right: 1px solid red;
height: 100%;
}
<button>
<span class="one">one</span>
<span class="two">two</span>
</button>
如果您希望文本垂直居中,您可以使用display: grid
ordisplay: inline-grid
和align-items: center
。
工作示例:
button {
display: grid;
height: 50px;
grid-auto-flow: column;
align-items: center;
}
.one {
color: white;
background-color: purple;
border-right: 1px solid red;
height: 100%;
align-items: center;
display: grid;
}
<button>
<span class="one">one</span>
<span class="two">two</span>
</button>
推荐阅读
- omnet++ - RSU 在静脉中的簇
- android - Spinner 中的项目可见但 onItemSelected 不起作用
- c++ - 在 Xubuntu 19.04 中编译 c++ 是否应该与在 Ubuntu 18.04 中编译 c++ 不同
- javascript - 在 HTML5UP 主题“镜头”中使用视频
- c# - WPF - 在 ListView 中禁用按钮时的奇怪行为
- postgresql - 在 PostgreSQL 中检查组内的条件
- .htaccess - 我在typo3中找不到htaccess
- arrays - 将浮点数写为 bytearray 在 python 中产生换行符
- sql-server - 如何在 SQL Server 的函数中使用变量
- c - 在 C 中创建 BITMAP 时初始化程序无效