css - CSS网格输入大小增加
问题描述
我有几个关于 CSS 网格的问题。
- 为什么标签和输入的大小与网格的行高匹配并填充它?
- 为什么标签和输入元素在 css 网格中显示为块,而在简单 div 中显示为内联元素?
- 为什么浏览器会忽略将它们的显示覆盖到内联?
这是一个例子:
.wrapper {
background-color: purple;
display: grid;
grid-template-columns: 1fr 4rem 4rem 1fr;
}
.wrapper > label {
display: inline;
grid-column: 2;
background-color:green;
}
.wrapper > input[type=checkbox] {
grid-column: 3;
border: 1px solid red;
background-color: gray;
}
.wrapper > input[type=text] {
grid-column: 4;
outline: 2px solid yellow;
}
.simple {
width: 50px;
height: 100px;
background-color: blue;
}
.simple > label {
height: 50px;
}
.wrapper2 {
background-color: gray;
display: grid;
grid-template-rows: 100px;
grid-auto-rows: 100px;
grid-template-columns: 1fr 8rem 8rem 1fr;
}
.wrapper2 > label {
grid-column: 2;
display: inline;
background-color:green;
}
.wrapper2 > input[type=checkbox] {
grid-column: 3;
background-color:yellow;
border: 1px solid gray;
}
.wrapper2 > input[type=text] {
grid-column: 4;
outline: 2px solid yellow;
border: 1px solid gray;
}
<div class="wrapper">
<label for="cb">Long Label</label>
<input id="cb" type="checkbox">
<input id="t3" type="text">
</div>
<div class="simple">
<label for="cb2">Long Label</label>
<input id="cb2" type="checkbox">
<input id="t2" type="text">
</div>
<div class="wrapper2">
<label for="cb3">Long Label</label>
<input id="cb3" type="checkbox">
<input id="t3" type="text">
</div>
解决方案
1. 为什么label和input的大小和grid的行高匹配并填充?
因为 height 是auto
,所以在 CSS Gridauto
中被拉伸了。stretch
由于对齐属性align-items
和的默认值justify-content
,我们可以覆盖它:
align-items:flex-start
在块轴(垂直)justify-content:flex-start
在内联轴上(水平)
[grid] {
background: orange;
display: inline-grid;
grid-template-columns: 50px;
grid-template-rows: 50px;
}
[grid]>div {
background: red;
height: auto;
}
[fix] {
align-items: flex-start;
justify-items: flex-start;
}
[grid]>[notAuto] {
height: 30px;
width: 30px;
}
<h4>align-items: stretch; justify-items: stretch; (default)</h4>
<div grid>
<div>Text</div>
</div>
<h4>align-items: flex-start; justify-items: flex-start;</h4>
<div grid fix>
<div>Text</div>
</div>
<h4>When width/height are specified (not auto)</h4>
<div grid>
<div notAuto>Text</div>
</div>
注意:我们可以使用Grid 项而不是容器上的-self
对应项来覆盖特定元素上的该行为justify-self
align-self
2、为什么label和input元素在css grid中显示为block,而在简单div中显示为inline元素?
3. 为什么浏览器会忽略将它们的显示覆盖到内联?
规范说:
网格项的
display
值是blockified
:如果生成网格容器的元素的流入子项的指定显示是内联级值,则它计算为其块级等效值。
推荐阅读
- xamarin - 在 webview 中加载时,某些设备上的 mp4 视频“无法播放”
- python - 循环时代码的长度不清楚
- c# - Jmeter 响应代码:非 HTTP 响应代码:java.net.SocketException
- pentaho - 动态读取列标题以传递到 ETL 元数据注入步骤
- javascript - 如何将复选框值插入数据库两列
- r - 在 R 中修改并重新创建 data.frame 列表
- python - python使用mox模拟打开方法
- python - 将 Pandas 数据框转换为 TensorFlow 数据集
- reactjs - axios如何从api中检索最后3篇文章
- freemarker - 如何重定向到 FreeMarker 模板中的新页面?