html - 如何使用 css 网格在第一行居中 1 列并将其他 3 列放在第二行?
问题描述
我是 CSS 网格的新手。我有一个 div,里面有 4 个 div,如代码所示。我希望将第一个内部 div 放置在行的中心。第二排剩下的3个孩子。
我尝试了一些基本的 css 样式来让这些孩子放在正确的位置。但我希望有更好的方法来做到这一点。
<style>
.parent {
width: 100%;
display: grid;
grid-template-columns: repeat: (3, 1fr)
}
</style>
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
我希望得到如下所示的结果:http: //prntscr.com/orr41c
解决方案
这是我所做的:
/* first child goes in the first row / second column and span 1 column */
.parent .child:nth-of-type(1) {
grid-column: 2/ span 1;
grid-row: 1;
background: red;
}
/* the 3 other children place themselves in the second row */
.parent .child:nth-of-type(n+2) {
grid-row: 2;
}
<style>
.parent {
width: 150px;
height: 100px;
display: grid;
grid-template-columns: repeat: (3, 1fr);
margin: 0 auto;
}
.parent .child {
background: yellow;
width: 100%;
height: 100%;
}
.parent .child:nth-of-type(1) {
grid-column: 2/ span 1;
grid-row: 1;
background: red;
}
.parent .child:nth-of-type(n+2) {
grid-row: 2;
}
</style>
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
推荐阅读
- javascript - 我无法获取要在我的插槽中显示的信息
- ruby-on-rails - 识别对服务器的伪造查询/仅允许 UI 进行的查询
- java - 在 Spring 框架中创建多个 DataSource 是昂贵的
- typescript - TS 服务器似乎无法识别元组中联合的有效类型
- powerbi - 直接从服务器获取数据时,是否可以在 Power BI 中对数字进行排序?
- quarkus - 在 Quarkus 命令模式下停用监听端口的网络服务器
- apache - 如何将单个子域重定向到不同的域?
- python-3.x - python selenium - 即使按钮被禁用,is_enabled 也会返回 True
- reactjs - 选项卡中的 Formik React
- amazon-web-services - 如何在每次 AWS ElasticBeanstalk 更改尝试时调用脚本