I have two DIVs (one is a sidebar and the other one is a content part) that I need to have the same height but I have a problem. The sidebar includes some li
s. I want the height of the sidebar to be the same as the content part but when I use flex
, the height of the sidebar can be longer than the content part (because both parts are dynamic). So I need the sidebar to be scrolled when the height is longer than the content part.
<section class="container">
<div class="sidebar">
<ul>
<li><li>
<li><li>
<li><li>
<li><li>
<li><li>
</ul>
</div>
<div class="content"></div>
</section>
My CSS code:
.container {
display: flex;
}
.sidebar,.content {
flex: 1 0 auto;
}
I even used JQuery but we see the wrong height because images are being loaded slowly in the content part.
My JQuery code:
jQuery(document).ready(function($) {
$(".sidebar").height($(".content").height());
});
I even used the following code but nothing happens:
jQuery( window ).load(function($) {
$(".sidebar").height($(".content").height());
});
I do not want to use position:absolute
because... You can see my page in this link: https://end-eng.com/landing-grammar/
Here is your code it's very simple way to achieve sidebar and main content using flexbox; Codepen example goes here
Html goes here...
css goes here
What you have is very close! If you instead set the flex properties on the parent, you will have two equal-height divs. I added some colours to illustrate:
Here's some reading on the
flex
shorthand you're using: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-growIf you need the sidebar to be scrolled when the height is longer than the content, then you can use jQuery to match the elements' heights on a
resize
event (to keep it consistent) like so:as I told above, I wanted sidebar to be scrolled when the sidebar is longer than content. so I solved it by pure CSS:
my new HTML code:
my new CSS code: