所有沿垂直轴的中心的项目,除了最后一个使用CSS网格坐在底部的项目吗?
我正在尝试获得回答另一个问题使用'方法5 CSS网格布局在垂直轴上工作。
目的是使所有项目都沿垂直轴中心,除了最后一个使用CSS网格在底部的物品?
要求是:
- 中心项目的数量可能会有所不同。
- 容器的高度可能会有所不同。
- 无法更改HTML。
如您所见,我第一次尝试从水平转换为垂直行业的尝试是不起作用的:
- 为什么这些物品是错误的?
- 为什么最后一个物品坐在底部?
- 我该如何解决?
ul {
display: grid;
grid-template-columns: 1fr repeat(3, auto) 1fr;
grid-column-gap: 5px;
justify-items: center;
}
ul li:nth-child(1) {
grid-column-start: 2;
}
ul li:nth-child(4) {
margin-left: auto;
}
ul,
ol {
padding: 0;
margin: 0;
list-style: none;
background: pink;
}
li {
padding: 5px;
background: #aaa;
}
p {
text-align: center;
}
/* My attempt to get this working on a vertical layout */
ol {
height: 400px;
display: grid;
grid-template-rows: 1fr repeat(3, auto) 1fr;
row-gap: 5px;
justify-items: center;
}
ol li:nth-child(1) {
grid-row-start: 2;
background-color: coral;
}
ol li:nth-child(4) {
margin-top: auto;
background-color: chartreuse;
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<p><span>| true center |</span></p>
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
I'm trying to get the answer to another question to work on the vertical axis using 'Method 5 CSS Grid Layout'.
The aim is to get all items centered along the vertical axis except the last which should sit on the bottom using CSS Grid?
The requirements are:
- The number of centered items can vary.
- The height of the container can vary.
- Can't change the HTML.
As you can see my first attempt at translating from the horizontal to the vertical is not working:
- Why are the items out of order?
- Why isn't the last item sitting on the bottom?
- How do I fix this?
ul {
display: grid;
grid-template-columns: 1fr repeat(3, auto) 1fr;
grid-column-gap: 5px;
justify-items: center;
}
ul li:nth-child(1) {
grid-column-start: 2;
}
ul li:nth-child(4) {
margin-left: auto;
}
ul,
ol {
padding: 0;
margin: 0;
list-style: none;
background: pink;
}
li {
padding: 5px;
background: #aaa;
}
p {
text-align: center;
}
/* My attempt to get this working on a vertical layout */
ol {
height: 400px;
display: grid;
grid-template-rows: 1fr repeat(3, auto) 1fr;
row-gap: 5px;
justify-items: center;
}
ol li:nth-child(1) {
grid-row-start: 2;
background-color: coral;
}
ol li:nth-child(4) {
margin-top: auto;
background-color: chartreuse;
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<p><span>| true center |</span></p>
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎很好,您需要添加
网格 - auto-flow:column
。如果要在相对的轴上进行相同的操作,则必须更改流量算法。You are almost good, you need to add
grid-auto-flow: column
. You have to change the flow algorithm if you want to do the same in the opposite axis.