Multiple sticky headers

Is there another way I can get multiple sticky headers to stack under each other than setting the top offset as the height of the previous sticky headers?

In the code snippet if I set top: 50px in .inner-header it works fine but I am looking for some other solution.

.container {
  overflow: auto;
  height: 300px
}

.header {
  height: 50px;
  background-color: pink;
  position: sticky;
  top: 0;
  z-index: 1;
}

.content {
  height: 1000px;
}

.section {
  height: 150px;
  border: 1px solid black;
  margin-top: 40px;
}

.inner-header {
  height: 30px;
  border-bottom: 1px solid black;
  position: sticky;
  top: 0;
  background-color: gray;
}
<div class="container">
  <div class="header">
    Main sticky header
  </div>
  <div class="content"> 
    <div class="section">
      <div class="inner-header">
        Section sticky header
      </div>
    </div>
  </div>
</div>