1

I am trying to create a layout where the page height is fixed to the viewport (100vh), the .sidebar and .content areas scroll independently, and a .content-header inside .content should stay sticky while the .content-body scrolls.

Here is the CSS I am using:

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-header {
  flex: 0 0 auto;
  padding: 1rem;
  background: #333;
  color: white;
}

.layout {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 250px 1fr;
  max-height: 100%;
  overflow: hidden; /* needed so main area does not scroll the whole page */
}

.sidebar {
  overflow-y: auto;
  padding: 1rem;
  background: #f1f1f1;
}

.content {
  overflow-y: auto; /* scroll only this area */
  padding: 1rem;
}

/* Sticky header inside the scrollable content area */
.content-header {
  position: sticky;
  top: 0;
  padding: 0.5rem 0;
  background: white;
  border-bottom: 1px solid #ccc;
  z-index: 1;
}

.content-body {
  padding-top: 1rem;
}

Expected behavior:

  • The whole page height is fixed to the viewport (100vh);

  • Only .sidebar and .content should scroll;

  • Inside .content, the .content-header should remain sticky at the top while .content-body scrolls underneath it.

However, position: sticky is not working inside the grid layout with overflow: hidden.

Why is this happening, and how can I keep the header sticky while allowing the content to scroll?

New contributor
Tiya varshney is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • 1
    It appears this might be an issue to the html structure itself, could you provide the html you made? it would very much help so to debug. thanks Commented yesterday
  • Could you rework your code in the sample in the form of code snippet? This feature supports HTML + CSS + JavaScript. This way, we could play with your code and run the snippet on this page. Please see the menu on top of the post editor, the snippet menu button, Ctrl+M. Yes, without an HTML sample, CSS makes liitle to no sense. Commented yesterday
  • @tiya-varshney The code isn’t complete, but if I understood correctly you should do this : .layout { ... height: 100%; } .content { ... position: relative; } if you need .layout to have overflow: hidden, you must ensure that .content itself is an independent scroll container so that sticky is calculated within it. Commented 16 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.