<!--


Forum question answer only:

https://www.sitepoint.com/community/t/doesnt-stretch-the-element-to-its-full-content/412536/12

-->

<div class="cont">
  <header class="header" id="header">
    <p>1</p>
  </header>
  <main class="main" id="main"><p>Main</p></main>
  <footer class="footer" id="footer">
    <p>Footer</p>
  </footer>
</div>
html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
html,
body {
  margin: 0;
  padding: 0;
}
p {
  margin: 0 0 1rem;
  background: #666;/* just for testing*/
}
/* dirty reset best avoided except for quick demos
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  outline: none;
  text-decoration: none;
}
*/
.cont {
  display: flex;
  min-height: 100vh; /* use min-height so element can grow*/
  overflow: hidden; /* hide the slide off to the side*/
}

.cont > * {
  background: black;
  color: #fff;
}
.main {
  flex: 1 0 0; /* make the middle item fill whatever space is available*/
}
.header {
  width: 200px;
  /* the clip-path animation happens first and  takes a second to animate and then the margin and width animations follows after a 1s delay*/
  transition: clip-path 1s, margin 1s 1s, width 1s 1s;
  clip-path: inset(0% 0% 0% 0%);
  margin-right: 5px;
}
/* JS adds the active class when clicked*/
.header.active {
  clip-path: inset(100% 0% 0% 0%);
  width: 0;
  margin-right: 0;
  overflow: hidden;
}
.footer {
  width: 200px;
  clip-path: inset(0% 0% 0% 0%);
  margin-left: 5px;
  transition: clip-path 1s, margin 1s 1s;
}
/* JS adds the active class when clicked*/
.footer.active {
  clip-path: inset(100% 0% 0% 0%);
  margin-right: -200px;
  margin-left: 0;
  overflow: hidden;
}
/* Put your Js at the end of the html before the closing body tag rather than hi-jacking the onload event */


//use an iffe to enscapulate the code and protect the global namespace
(function clipMe(d) {
  "use strict";

  // declare the variables for use as constants
  const header = d.querySelector("#header");
  const footer = d.querySelector("#footer");

  // when someone clicks the header element add an active class to the header element
  
  // use eventListeners and not onclick
  header.addEventListener(
    "click",
    () => {
      header.classList.add("active");
    },
    false
  );
 
  // when someone clicks the footer element add an active class to the footer element
  footer.addEventListener(
    "click",
    () => {
      footer.classList.add("active");
    },
    false
  );
  
})(document);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.