如何在CSS网格布局中定位特定的列或行?是否可以使用CSS选择特定的网格列或行?例如,假设我有一个3行×2列CSS网格布局:grid-template-rows: 1fr 1fr 1fr; grid-template-columns: 1fr 1fr;。如何从第2列中选择所有元素?例如:( grid:nth-child(column:2)只是我的想法,不是有效的代码)。我已经nth-child在div元素上尝试了选择器,但这不允许我在Grid Layout引擎自动放置项目时指定行或列。body {
display: grid;
grid-template-rows: 1fr 1fr 1fr;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;}.item {
background: #999;}<div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p></div><div class="item">
<p>Right Justify</p>
<p>Element 1 | Element 2</p></div><div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p></div><div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p></div><div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p></div><div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p></div><div class="item">
<p>Customer Name</p>
<p>Element 1 | Element 2</p></div>
3 回答
MM们
TA贡献1886条经验 获得超2个赞
CSS无法实现。
CSS以HTML元素,属性和属性值为目标。
网格列和行没有这些“钩子”。
您必须直接定位网格项目。
你写了:
例如,假设我有一个3行×2列CSS网格布局:
grid-template-rows: 1fr 1fr 1fr; grid-template-columns: 1fr 1fr;
。如何从第2列中选择所有元素?
grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-gap: 10px; padding: 10px; height: 50vh; background-color: gray;}grid-item { background-color: lightgreen;}grid-item:nth-child(2n) { border: 2px dashed red;}
<grid-container> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item></grid-container>
喵喔喔
TA贡献1735条经验 获得超5个赞
如果您想要设置行的样式,则应用相同的主体。以上为例:
grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr 1fr 1fr; grid-gap: 10px; padding: 10px; height: 50vh; background-color: gray;}grid-item { background-color: lightgreen;}grid-item:nth-child(4n+3),grid-item:nth-child(4n) { border: 2px dashed red;}
<grid-container> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item> <grid-item></grid-item></grid-container>
- 3 回答
- 0 关注
- 535 浏览
相关问题推荐
添加回答
举报
0/150
提交
取消