Search code examples
htmlcsscss-floatcss-tables

How to make aside column grow with the size of the content in parallel column


Hi how can I make the side menu to grow with the content of the parallel container, that should expand with its content as well.

I am trying to ensure that the left column enclosed in aside tag, is the same size as the content on the right enclosed in div column .right_panel. I was able to accomplish this by setting the size of html and body tag as well as .wrapper class to height: 100% as per similar queries on stackoverflow.

However now the .wrapper is not expanding when I add more content to main. I tried to change height to min-height but it shrink the aside to its content size.

I know I can accomplish this with flex layout but because flex is outside the scope of my course and also because I want to learn alternative approaches in case I would need to use them in the future for compatibility with the older browsers I am looking for solution that would not use flex layout.

Snippet below and JSFiddle link:

html,
body {
    height: 100%;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    background-color: blue;
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}

.wrapper {
    margin: 10px auto 30px;
    padding: 0;
    width: 80%;
    background-color: white;
    height: calc(100% - 40px);
    max-height: 100%;
}

aside.left_panel {
    float: left;
    min-height: 100%;
    width: 130px;
    background-color: #9eb9f1;
    padding-left: 30px;
    overflow: auto;
}

.right_panel {
    margin-left: 160px;
}

nav ul {
    list-style-type: none;
    padding: 0;
}

header {
    background-color: #6f90d1;
    padding: 20px;
}

header h1 {

    font-size: 60px;
    color: darkblue;
}

main {
    padding: 20px 20px 0;
}

main h2 {
    color: #6f90d1;
}

main #lantern {
    height: 400px;
    width: 200px;
}

main img {
    margin: 10px;
}

main h2{
    margin-bottom: 30px;
}
main p {
    margin-bottom: 30px;
}

footer {
    text-align: center;
    margin: 10px 0;
}

.f-right {
    float: right;
    overflow: auto;
}

.f-left {
    float: left;
    overflow: auto;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="styles/styles.css">
</head>

<body>
    <div class="wrapper">
        <aside class="left_panel">
            <nav>
                <ul>
                    <li>Home</li>
                    <li>Manu item 1</li>
                    <li>Manu item 2</li>
                    <li>Manu item 3</li>
                </ul>
            </nav>
        </aside>
        <div class="right_panel">
            <header>
                <h1>Name of the website</h1>
            </header>
            <main>
                <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
                <h2>Subheading 1</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
                <h2>Subheading 2</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
                <h2>Subheading 3</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
                <h2>Subheading 4</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>
                
            </main>
            <footer>
                <p>Copyright &copy; 2019</p>
            </footer>
        </div>
    </div>
</body>

</html>


Solution

  • Using Table Layout

    One solution is to use a table layout using display: table on the wrapper and making your left-section and right-section into a table-cell - see demo below:

    html,body {
      height: 100%;
    }
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 16px;
      background-color: blue;
    }
    h1,h2,h3,h4,h5,h6 {
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    }
    .wrapper {
      margin: 10px auto 30px;
      padding: 0;
      width: 80%;
      background-color: white;
      height: calc(100% - 40px);
      max-height: 100%;
      display: table; /* added */
    }
    aside.left_panel {
      /* float: left; */
      min-height: 100%;
      width: 130px;
      background-color: #9eb9f1;
      padding-left: 30px;
      overflow: auto;
      display: table-cell; /* added */
      vertical-align: top; /* added */
    }
    .right_panel {
      /*margin-left: 160px;*/
      display: table-cell; /* added */
    }
    nav ul {
      list-style-type: none;
      padding: 0;
    }
    header {
      background-color: #6f90d1;
      padding: 20px;
    }
    header h1 {
      font-size: 60px;
      color: darkblue;
    }
    main {
      padding: 20px 20px 0;
    }
    main h2 {
      color: #6f90d1;
    }
    main #lantern {
      height: 400px;
      width: 200px;
    }
    main img {
      margin: 10px;
    }
    main h2 {
      margin-bottom: 30px;
    }
    main p {
      margin-bottom: 30px;
    }
    footer {
      text-align: center;
      margin: 10px 0;
    }
    .f-right {
      float: right;
      overflow: auto;
    }
    .f-left {
      float: left;
      overflow: auto;
    }
    <div class="wrapper">
      <aside class="left_panel">
        <nav>
          <ul>
            <li>Home</li>
            <li>Manu item 1</li>
            <li>Manu item 2</li>
            <li>Manu item 3</li>
          </ul>
        </nav>
      </aside>
      <div class="right_panel">
        <header>
          <h1>Name of the website</h1>
        </header>
        <main>
          <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
          <h2>Subheading 1</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 2</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
          <h2>Subheading 3</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 4</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>
    
        </main>
        <footer>
          <p>Copyright &copy; 2019</p>
        </footer>
      </div>
    </div>

    Using Float

    You can try this - but you don't get the overflow right or won't be able to make the height of the columns the same - see an example to fiddle with that:

    html,body {
      height: 100%;
    }
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 16px;
      background-color: blue;
    }
    h1,h2,h3,h4,h5,h6 {
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    }
    .wrapper {
      margin: 10px auto 30px;
      padding: 0;
      width: 80%;
      background-color: white;
      height: calc(100% - 40px);
      max-height: 100%;
      overflow: hidden; /* to clear the float */
    }
    aside.left_panel {
      float: left;
      min-height: 100%;
      width: 130px;
      background-color: #9eb9f1;
      padding-left: 30px;
      overflow-y: auto; /*changed to overflow-y */
    }
    .right_panel {
      float: left; /* added */
      width: calc(100% - 160px); /* added */
      /*margin-left: 160px;*/
    }
    nav ul {
      list-style-type: none;
      padding: 0;
    }
    header {
      background-color: #6f90d1;
      padding: 20px;
    }
    header h1 {
      font-size: 60px;
      color: darkblue;
    }
    main {
      padding: 20px 20px 0;
    }
    main h2 {
      color: #6f90d1;
    }
    main #lantern {
      height: 400px;
      width: 200px;
    }
    main img {
      margin: 10px;
    }
    main h2 {
      margin-bottom: 30px;
    }
    main p {
      margin-bottom: 30px;
    }
    footer {
      text-align: center;
      margin: 10px 0;
    }
    .f-right {
      float: right;
      overflow: auto;
    }
    .f-left {
      float: left;
      overflow: auto;
    }
    <div class="wrapper">
      <aside class="left_panel">
        <nav>
          <ul>
            <li>Home</li>
            <li>Manu item 1</li>
            <li>Manu item 2</li>
            <li>Manu item 3</li>
          </ul>
        </nav>
      </aside>
      <div class="right_panel">
        <header>
          <h1>Name of the website</h1>
        </header>
        <main>
          <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
          <h2>Subheading 1</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 2</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
          <h2>Subheading 3</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 4</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>
    
        </main>
        <footer>
          <p>Copyright &copy; 2019</p>
        </footer>
      </div>
    </div>

    But there is a hack for that - using large margin & padding. Note that you need to remove the height and max-height set on the wrapper - see demo below:

    html,body {
      height: 100%;
    }
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 16px;
      background-color: blue;
    }
    h1,h2,h3,h4,h5,h6 {
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    }
    .wrapper {
      margin: 10px auto 30px;
      padding: 0;
      width: 80%;
      background-color: white;
      /*height: calc(100% - 40px);
      max-height: 100%;*/
      overflow: hidden; /* to clear the float */
    }
    aside.left_panel {
      float: left;
      min-height: 100%;
      width: 130px;
      background-color: #9eb9f1;
      padding-left: 30px;
      overflow-y: auto; /*changed to overflow-y */
      margin-bottom: -100000px; /* a large value */
      padding-bottom: 100000px; /* a large value */
    }
    .right_panel {
      float: left; /* added */
      width: calc(100% - 160px); /* added */
      /*margin-left: 160px;*/
      margin-bottom: -100000px; /* a large value */
      padding-bottom: 100000px; /* a large value */
    }
    nav ul {
      list-style-type: none;
      padding: 0;
    }
    header {
      background-color: #6f90d1;
      padding: 20px;
    }
    header h1 {
      font-size: 60px;
      color: darkblue;
    }
    main {
      padding: 20px 20px 0;
    }
    main h2 {
      color: #6f90d1;
    }
    main #lantern {
      height: 400px;
      width: 200px;
    }
    main img {
      margin: 10px;
    }
    main h2 {
      margin-bottom: 30px;
    }
    main p {
      margin-bottom: 30px;
    }
    footer {
      text-align: center;
      margin: 10px 0;
    }
    .f-right {
      float: right;
      overflow: auto;
    }
    .f-left {
      float: left;
      overflow: auto;
    }
    <div class="wrapper">
      <aside class="left_panel">
        <nav>
          <ul>
            <li>Home</li>
            <li>Manu item 1</li>
            <li>Manu item 2</li>
            <li>Manu item 3</li>
          </ul>
        </nav>
      </aside>
      <div class="right_panel">
        <header>
          <h1>Name of the website</h1>
        </header>
        <main>
          <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
          <h2>Subheading 1</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 2</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
          <h2>Subheading 3</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 4</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>
    
        </main>
        <footer>
          <p>Copyright &copy; 2019</p>
        </footer>
      </div>
    </div>


    Using flexboxes

    Last, but not the least; and since flexboxes have IE11+ and support in all modern browsers, I'd prefer it - thanks to you for letting me brush up on my knowledge too. :)

    html,body {
      height: 100%;
    }
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 16px;
      background-color: blue;
    }
    h1,h2,h3,h4,h5,h6 {
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    }
    .wrapper {
      margin: 10px auto 30px;
      padding: 0;
      width: 80%;
      background-color: white;
      /*height: calc(100% - 40px);*/
      /*max-height: 100%;*/
      display: flex; /* added */
    }
    aside.left_panel {
      /* float: left; */
      min-height: 100%;
      width: 130px;
      background-color: #9eb9f1;
      padding-left: 30px;
      overflow-y: auto; /* changed to overflow-y*/
    }
    .right_panel {
      /*margin-left: 160px;*/
    }
    nav ul {
      list-style-type: none;
      padding: 0;
    }
    header {
      background-color: #6f90d1;
      padding: 20px;
    }
    header h1 {
      font-size: 60px;
      color: darkblue;
    }
    main {
      padding: 20px 20px 0;
    }
    main h2 {
      color: #6f90d1;
    }
    main #lantern {
      height: 400px;
      width: 200px;
    }
    main img {
      margin: 10px;
    }
    main h2 {
      margin-bottom: 30px;
    }
    main p {
      margin-bottom: 30px;
    }
    footer {
      text-align: center;
      margin: 10px 0;
    }
    .f-right {
      float: right;
      overflow: auto;
    }
    .f-left {
      float: left;
      overflow: auto;
    }
    <div class="wrapper">
      <aside class="left_panel">
        <nav>
          <ul>
            <li>Home</li>
            <li>Manu item 1</li>
            <li>Manu item 2</li>
            <li>Manu item 3</li>
          </ul>
        </nav>
      </aside>
      <div class="right_panel">
        <header>
          <h1>Name of the website</h1>
        </header>
        <main>
          <img id="lantern" class="f-right" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" alt="">
          <h2>Subheading 1</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 2</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. </p>
          <h2>Subheading 3</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius. Orci varius natoque penatibus et magnis dis parturient. </p>
          <h2>Subheading 4</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim tristique varius.</p>
    
        </main>
        <footer>
          <p>Copyright &copy; 2019</p>
        </footer>
      </div>
    </div>