I want to create a layout where a .parent
div has two children, .left
and .right
, both taking up equal halves of the entire width and stacked side by side. The height of the .parent
should be the same as the .left
child, while the .right
child should have the same height as the .left
. Here’s an example image:
The yellow box wrapping the two containers is .parent
, which has its content-box height (i.e., without padding, border, margin) equal to .left
. The red box at the left has the height as minimum as possible to wrap the content. The pink box at the right has scrollbars to manage exceeding content.
I’ve tried using flex
and grid
to create this layout, but I was unsuccessful. Can someone please help me code or explain how I can achieve this layout? Thank you.
3
Answers
try this
You can use
display: grid;
and the trick with.right { height: 0; min-height: 100%; }
:You can use
Element.offsetHeight
to get the numeric height of left column, measured in pixels(add ‘px’ to the value). It includes padding and border. also setalign-items:flex-start;
otherwise, flex-box will stretch the smaller div automatically. Set the height of right-column equal to height of left-column. Be aware of your margins.