EDIT: My question is about how to style img elements with CSS without stretching the image (altering aspect ratio) so that they have a consistent height and width in a grid, without the use of divs.
EDIT2: The suggested duplicate question does not answer my question. I accepted the answer about the CSS clip-path property since it most closely matched my needs.
I'm trying to create a 3x3 image gallery. I got the widths to work correctly, but I can't figure out how to get the images to be the same height without explicitly setting a height in pixels (which stretches the images, doesn't adapt to the user's screen size, and looks horrendous).
Here's what I have so far (only showing 2 rows to demonstrate height difference)
I've found a few answers on Stack Overflow which use divs with background images to solve the problem, but the images I'm using are content so they need to be included in my HTML as img elements.
Here is my HTML (not including src and alt img attributes for simplicity):
<section>
<img>
<img>
<img>
<img>
<img>
<img>
<img>
<img>
<img>
</section>
Here are my styles for the img elements:
section img {
width: 100%;
display: block;
margin: 1em 0;
@include for-tablet-up {
display: inline-block;
margin: .5em 2.5%;
width: calc(85%/2);
}
@include for-desktop-up {
width: calc(85%/3);
}
}
Here are my Sass mixins:
@mixin for-tablet-up {
@media screen and (min-width: 768px) { @content; }
}
@mixin for-desktop-up {
@media screen and (min-width: 1024px) { @content; }
}