I am working on a video calling application using reactjs. LocalTile component contains video of local user and RemoteTiles is returning videos of all remote users. My desired layout is like for 3 users:
===video1=video2===
=======video3======
for 4 users:
===video1=video2===
===video3=video4===
for 5 users:
===video1=video2=video3===
=======video4=video5======
<div id='gallery-parent' style={{ display: 'flex', flex: '1 1', flexDirection: 'column', gap: '1rem', position: 'relative' }}>
<div style={{ alignItems: 'stretch', display: 'flex', flex: '1 1', gap: '1rem',justifyContent:'center'}}>
<LocalTile />
<RemoteTiles />
</div>
</div>
its not working for me as all users are rendering in same column and shrinking videos.
3
Answers
Try setting a
min-width: in (vw or %) unit + flex-wrap: wrap;
min-width: in vw unit relative to screen width, in % to parent width
flex-wrap: wrap; to allow the remaining el to pass to the next line
Like this:
I’m assuming a maximum of 2 rows and a maximum of 5 children.
This is possible by using a combination of
nth-child
andnth-last-child
to target elements based on the number of children.This requires setting certain widths based on the number of elements but since CSS cannot actually count elements this is not scalable.
Note: If more rows are required, it would be better to have layouts like
2 + 1 for 3 users
2 + 2 for 4 users
2 + 2 + 1 for 5 users etc.
That makes the CSS much simpler and very scalable.
I’m not 100% sure how you’d implement this in your larger codebase, but this is how I’d do it in vanilla JS. After the list of callers is rendered, you can grab the one just past halfway and insert a non-shrinking full-width element before it which will force the wrap to happen.
This little example utility allows n number of users in the input box and will split them as expected
Alternatively, if you don’t want the second row to take up the full width
and just center the two callers, it can be done with CSS instead of DOM manipulation. This uses the bitwise operator
n ^ 1
to determine ifn
is odd and if so, bump it up to the next even number