I'm attempting to resize images in a row with CSS, based on how many images are in the same row. For example, if there are four img
elements in the same p
, they are styled a certain way. Here is the working code, based on this answer:
HTML:
<p>
<img src="preview.png">
<img src="preview.png">
<img src="preview.png">
<img src="preview.png">
</p>
CSS:
.post-content p img:first-child:nth-last-child(4),
.post-content p img:first-child:nth-last-child(4) ~ img {
width: 25%; /* fallback for browsers that don't support calc */
width: calc((100% / 4) - 0.8%);
margin-left: calc(1%);
}
.post-content p img:first-child:nth-last-child(4) { /* First image in group of four */
margin-left: 0;
}
The result looks like this:
However, this does not work for images wrapped in a
tags, but with the rest of the formatting identical, like this:
<p>
<a href="#"><img src="preview.png"></a>
<a href="#"><img src="preview.png"></a>
<a href="#"><img src="preview.png"></a>
<a href="#"><img src="preview.png"></a>
</p>
I am unable to find a solution in this case. Any help would be much appreciated.