CSS Flexbox with a component returning TopBar and SideBar and another one returning main content

I am having troubles getting my main content next to the sidebar in a flexbox layout. My TopBar and SideBar components are combined into a single component, so they are returned in a single parent div. Is there a solution using CSS flexbox or do I have to use grid?

This is basically how it is returned:

<div style="display: flex; flex-wrap: wrap;">
  <div id="ParentNav">
    <div style="width: 100%; height: 48px; background-color: blue;">TopBar</div>
    <div style="width: 256px; min-height: calc(100vh - 48px); background-color: grey;">SideBar</div>
  </div>
  <div style="flex-grow: 1; background-color: gold;">Main content</div>
</div>

Run code snippet

Expand snippet

This is what I would like to achieve visually, but I cannot return the TopBar and SideBar seperately.

<div style="display: flex; flex-wrap: wrap;">
  <div style="width: 100%; height: 48px; background-color: blue;">TopBar</div>
  <div style="width: 256px; min-height: calc(100vh - 48px); background-color: grey;">SideBar</div>
  <div style="flex-grow: 1; background-color: gold;">Main content</div>
</div>

Key Points:

  • Leverage flex-direction: row;: Set the flex-direction property of the parent div to row to arrange child elements horizontally.
  • Adjust Child Divs: Modify the width and height properties of the child divs to fit your desired layout.
  • Consider flex-shrink and flex-grow: If you want the Main content to expand and the SideBar to shrink, use flex-grow and flex-shrink properties.

Revised Code:
HTML

<div style="display: flex; flex-wrap: wrap; flex-direction: row;">
  <div id="ParentNav">
    <div style="width: 100%; height: 48px; background-color: blue;">TopBar</div>
    <div style="width: 256px; min-height: calc(100vh - 48px); background-color: grey;">SideBar</div>
  </div>
  <div style="flex-grow: 1; background-color: gold;">Main content</div>
</div>

Explanation:

  • The flex-direction: row; property ensures that the child elements are arranged horizontally.
  • The flex-grow: 1; property on the Main content div allows it to expand to fill the remaining space after the TopBar and SideBar.

Additional Tips:

  • If you want the SideBar to remain fixed in width and the Main content to expand, you can adjust the width of the SideBar and use flex-grow on the Main content.
  • For more complex layouts, consider using media queries to adjust the layout based on screen size.

By making these changes, you should achieve the desired layout where the TopBar and SideBar are displayed next to the Main content, even if they are combined in a single parent div.