I'm pulling dynamic images into a 2-column layout. If there's only one image, how would I center it, or collapse the columns into one? Currently a single image is placed in the left column. Is there a CSS-only solution?
Would the same logic apply to the last item in a column, if it's the only one in its row?
p{
column-count: 2;
column-gap: 0;
}
p>img{
display: block;
width: 100%;
height: auto;
border: 3px solid transparent;
box-sizing: border-box;
}
Thanks to Eric N's answer and column-span, the following added CSS centers the first and only item in a 2-column layout:
p>img:first-of-type:last-of-type{
column-span: all;
margin: auto;
width: 50%;
}
Furthermore, to target the last item in a 2-column layout, if it's the only item in its row, I'm now using this:
p>img:nth-child(odd):last-of-type{
column-span: all;
margin: auto;
width: 50%;
}