is it fine to explicitly set height: 120px for an image?

Kevin said don't use heights and don't use pixel units. I want to create a grid of cards where the image is the same height and width for every card. Each card has an image and some text right below it. Unfortunetly all the images are either too tall or too short. I did manage to get a hold of the width by using grid-template-column: repeat(auto-fit,minmax(300px,1fr)) but I'm not sure how to set the height. I was thinking of setting grid-auto-row: 500px or something but I remembered that setting height and px are bad. Edit: I guess what I'm asking is: 1. should I explicitly set a height on images 2. should the explicitly set height be in px or rem and why 3. is there a better way altogether to achieve same height cards across a grid.
No description
17 Replies
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
You can make all images have the same height without having a fixed height if you use the aspect-ratio property instead, that's what i would recommend, and if you do, you should also add object-fit:cover; So you can for example do
.grid-container {
display:grid;
grid-template-columns:repeat(auto-fit, minmax(min(300px, 100%), 1fr))
}
.grid-container img {
aspect-ratio:16/9;
object-fit:cover;
}
.grid-container {
display:grid;
grid-template-columns:repeat(auto-fit, minmax(min(300px, 100%), 1fr))
}
.grid-container img {
aspect-ratio:16/9;
object-fit:cover;
}
soma-foreever
soma-foreeverOP•2w ago
The aspect-ratio CSS property allows you to define the desired width-to-height ratio of an element's box. This means that even if the parent container or viewport size changes, the browser will adjust the element's dimensions to maintain the specified width-to-height ratio. The specified aspect ratio is used in the calculation of auto sizes and some other layout functions.
https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio Does this mean it's setting the height based on the width or setting the width based on the height?
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
The height will be adjusted automatically based on the width of the image to always maintain this 16:9 aspect ratio
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
@soma-foreever Here you have an example https://codepen.io/tok124/pen/QwLgpWq This is a pen i made a month ago. Should be somewhat similar to what you want
Tim
CodePen
Untitled
...
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
These images have different sizes at least some of them do
soma-foreever
soma-foreeverOP•2w ago
thanks. I'll check it out. yes it works
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
Awesome ! 😄
soma-foreever
soma-foreeverOP•2w ago
but it's basically like implicitly setting height on images
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
Yeah kinda, except that the height will change based on the width
soma-foreever
soma-foreeverOP•2w ago
are there any situations where would advice against using this method for setting height on images? this does seem like a rock solid method of doing things
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
if you set height:500px and then you resize screen so the images is 100px wide, they would be 100x500 pixel which would just look extremly strange I always use this for my images and videos. The only time you should not use this is if you for some reason definitely do not want the image the maintain an aspect-ratio, but 99% of the time you want it to have an aspect ratio so yeah... You can basicly always stick to this But probably if you make a navbar and you have a img as logo in navbar, you probably dont need this In this case you can rather just set a fixed height and width, but well, that depends on how big the logo is, but navbar logos are usually rather small, so that should always be fine
soma-foreever
soma-foreeverOP•2w ago
1. should I explicitly set a height on images 2. should the explicitly set height be in px or rem and why 3. is there a better way altogether to achieve same height cards across a grid.
so we went with the 3rd option but do you have an opinion on no.2?
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
Yeah well, you should avoid px unit in most cases. So i would advice you to rather use the rem unit, it works better between different devices, kevin has a video where he explains all this far better than i can ever explain it ^^
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
It was either that one or this https://www.youtube.com/watch?v=_-aDOAMmDHI I cannot remember exactly
Kevin Powell
YouTube
CSS em and rem explained #CSS #responsive
A look at the CSS units EM and REM, how they work and when you might want to use one over the other! First I take a look at some basic examples of both ems and rems, then move onto some more interesting things with buttons and a call to action, as well how we can take advantage of both the em and rem with media queries. The way the em unit is ...
soma-foreever
soma-foreeverOP•2w ago
I'll watch them. thanks
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)•2w ago
Sure, no problem 🙂

Did you find this page helpful?