I had a look at the project called “personal portfolio page” done by free code camp here .
How is the height of each image calculated. It obviously works well as all the images have a similar height. I was wondering how this was achieved.
Line 224 in the css section says : calc(100% - 6.8rem). 100% of what is this based on?. I am a bit confused as a fixed height was not set for any parent containers. Am i missing something?
the percentage for height, written like that, is based on the parent element. If the parent element has no defined height, then height will take the default value of auto, which maintains the aspect ratio of the image, so the height is calculated from the width.
I made a codepen to explore this a little bit, feel free to ask or tweak it.
To fix this, we add height:100%;. What will happen here? The tallest image of the row will remain unchanged, and the rest (smaller ones), squeezed. (uncomment height:100% in the code)
Now object-fit:cover comes into play, uncomment the next line in the code, and the images will all look nice again.
In the example the images are inside an anchor tag which are really the grid children (not the images), and the images are siblings with a paragraph. Like so (i’m not being literal):
Thanks a lot for the detailed explanation and the examples!
My expectation was that all images would default to the height of the tallest element when height is not set. (i.e. whatever the height of the tallest element is based on its aspect ratio)
When i do uncomment “height : 100%” there is some strange behaviour as the page does not display anything and is blank
I’ve checked and the code of pen2 was fine. So it might be a browser issue. You can send me a private message if you have any other question. The pen was this one just in case. and for me in Firefox works fine.
Your expectation almost right, if I understand you. In FCC codepen all the grid children have the same height (only across one same row!), and that’s defined by the tallest element, in this case the tallest image (as the paragraphs are all same height). So it’s not the image (those are not direct children and height is auto) but the wrapper. In the one I made for you, images are direct children, but they do not get stretch, in some sense, luckily retain the aspect ratio.
Now as a side comment, I’ve made a change in the codepen
It all looks fine now and yes you were right, for some reason the code pen was not working well in google chrome when (height : 100%) was uncommented . It works fine in safari though!
Thanks a lot for the help. This has really helped my css knowledge!
You’re welcome. now you witness something very interesting, that browsers do not work the same, and we should test our pages (at least in the three main ones: safari chrome ffox).
There are some tools helping for that, one is the autoprefixer, another one normalizer, you’ll surely learn them in the next months.