make div height of first child

Hey, how would you limit the height of a div to the first child’s height? In the image, I have a design where I’d like the first piece of content to show but the rest you’d have to scroll down to. I feel this may be something you have to achieve with js, but I also think that there’s a chance it could be possible with html and css, I’d appreciate any insight, thank you in advance.
No description
24 Replies
Jochem
Jochem2mo ago
I'm not sure you can without JS, but I'd also not recomment having it be the exact size. Have some overflow from the first, or show the top of the second item, that way it's extra obvious to people that the container scrolls
snxxwyy
snxxwyy2mo ago
Ah okay, so grab the height of the first child and then apply that value to the parent? And taking your advice, add a little extra height on top of that?
Jochem
Jochem2mo ago
yeah, or subtracting a little. Just for UX reasons, lots of folks won't think to scroll unless the scrollbar is very obvious or it's like an app they use all the time it's not the best solution though, and I'd recommend having a sane guess as a default height in the CSS as a backup
snxxwyy
snxxwyy2mo ago
Yeah that makes sense, it would just be a bit magic numbery haha, but anything that either shows a bit extra or cuts off a bit should do
Jochem
Jochem2mo ago
yeah, and magic numbers are more of a thing to be aware of than to completely avoid anyway. There's lots of situations where a magic number is at the very least a decent fallback
snxxwyy
snxxwyy2mo ago
Yeah I agree
ἔρως
ἔρως2mo ago
wouldnt grid do the job? as long as you can keep the parent the same height, you can do this with grid or possibly with subgrid, but ive never used it for this
snxxwyy
snxxwyy2mo ago
Hm, what’s your thought process with that method?
ἔρως
ἔρως2mo ago
easy: 1fr is always the same chunk of the parent, if both parents have the same exact height you can still set grid-template-rows: 1fr 1fr 1fr and the element will be in the first row, but the other 2 will be empty
snxxwyy
snxxwyy2mo ago
The other rows would have something in them though no? That’s the content that you’d have to scroll down to? Apologies if I’m not understanding what you mean
ἔρως
ἔρως2mo ago
what do you have so far?
Chris Bolson
Chris Bolson2mo ago
I suspect that there is a miss-understanding here, possibly due to there being 2 parent elements in the initial diagram. As I understand it snxxwyy only has one container/parent. The aim is to define the height of that single parent container according to the first child height, hiding the other children within a vertical scroll.
Jochem
Jochem2mo ago
maybe make a codepen?
snxxwyy
snxxwyy2mo ago
Chris has the right idea, I’ll make a codepen when I get home to give a better idea
ἔρως
ἔρως2mo ago
ah, and it is to have the height of a single child?
Chris Bolson
Chris Bolson2mo ago
I was playing around with the idea of giving all but the first child a position: absolute thus taking them out of the flow which would allow the parent element to have the height of the first child. It would then need to automatically position the children below their previous sibling. It is working except for calculating the correct y position for each child. I'll keep at it...
snxxwyy
snxxwyy2mo ago
Yeah, exactly. Ah that sounds like a good method, I appreciate you taking the time to do that. I’ll play around with it myself too in a short while
Chris Bolson
Chris Bolson2mo ago
Can you modify the HTML?
snxxwyy
snxxwyy2mo ago
What do you mean, sorry?
Chris Bolson
Chris Bolson2mo ago
Can you change the way the elements are defined in the HTML? For example, could you wrap all but the first child in a new container?
snxxwyy
snxxwyy2mo ago
Oh yeah for sure, there’s no restrictions
Chris Bolson
Chris Bolson2mo ago
OK. I think that you can achieve this without JS by wrapping all the children, expect for the first one, in a wrapper. Give this wrapper a position: absolute; along with some margins/gaps etc and it should work. I have updated my Codepen https://codepen.io/cbolson/pen/rNXBVbz to do this so you will understand it better. Of course now that you are modifying the HTML there may well be alternative methods.. It's even responsive 😆
snxxwyy
snxxwyy2mo ago
Oh that’s awesome, I appreciate the demonstration, thank you And thank you to everyone else
Want results from more Discord servers?
Add your server