【发布时间】:2017-08-25 04:31:45
【问题描述】:
我将动态图像拉入 2 列布局。如果只有一张图片,我将如何将其居中,或将列折叠成一张?当前,单个图像放置在左列中。是否有纯 CSS 解决方案?
相同的逻辑是否适用于列中的最后一项,如果它是该行中唯一的一项?
p{
column-count: 2;
column-gap: 0;
}
p>img{
display: block;
width: 100%;
height: auto;
border: 3px solid transparent;
box-sizing: border-box;
}
感谢 Eric N 的回答和 column-span,以下添加的 CSS 使 2 列布局中的第一个也是唯一的项目居中:
p>img:first-of-type:last-of-type{
column-span: all;
margin: auto;
width: 50%;
}
此外,为了在 2 列布局中定位 last 项,如果它是其行中的唯一项,我现在使用这个:
p>img:nth-child(odd):last-of-type{
column-span: all;
margin: auto;
width: 50%;
}
【问题讨论】:
-
添加您的标记
标签: html css css-multicolumn-layout