Your current layout is close, but the issue is that the main area (with the three items and the bottom element) is not properly constrained to the viewport height, and the "below the fold" content is not being pushed out of view until scrolled. The key is to ensure that the main area takes up all available space between the header and the bottom of the viewport, and that the "below the fold" content is only visible when scrolled.
Here’s how you can fix it:
Key Changes:
h-screen on the root <div> is correct, but you need to ensure that the main area is the only scrollable part.
overflow-y-auto on the main area will make it scrollable if its content exceeds the available space.
min-h-0 is important for nested flex children to allow shrinking.
- The "below the fold" content should be outside the main area and not constrained by its height.
Updated Code:
<template>
<div class="h-screen flex flex-col">
<!-- HEADER -->
<header class="bg-blue-600 text-white p-4">
<div class="max-w-4xl mx-auto flex justify-between items-center">
<div class="font-bold">MyApp</div>
<nav class="flex space-x-4">
<a>Home</a><a>About</a><a>Contact</a>
</nav>
</div>
</header>
<!-- MAIN: Takes remaining space, scrolls if needed -->
<main class="flex-1 min-h-0 overflow-y-auto">
<!-- Top area: expands to fill available space -->
<section class="flex-1 flex flex-col gap-6 p-6 min-h-0">
<div class="flex-1 bg-white rounded-lg shadow p-4">Item 1</div>
<div class="flex-1 bg-white rounded-lg shadow p-4">Item 2</div>
<div class="flex-1 bg-white rounded-lg shadow p-4">Item 3</div>
</section>
<!-- Bottom element: sticks to bottom of MAIN -->
<div class="p-4 bg-gray-300 sticky bottom-0">
This element is at the bottom (of the main area, under the header)
</div>
</main>
<!-- Extra content below the fold (visible only when scrolled) -->
<div class="p-6 bg-gray-100">
<h2 class="text-lg font-semibold mb-2">More content below</h2>
<p>This text is below the fold. The whole page scrolls, including the bottom element above.</p>
</div>
</div>
</template>
Explanation of Changes:
overflow-y-auto on <main>: Makes the main area scrollable if its content is taller than the available space.
sticky bottom-0 on the bottom element: Keeps it at the bottom of the main area as you scroll.
- The "below the fold" content is now outside the main area and will only appear when the user scrolls the entire page.
Result:
- The three items will expand to fill the available space in the main area.
- The bottom element will stay at the bottom of the main area.
- The "below the fold" content will only appear when the user scrolls down.
Try this out and let me know if it works for your use case! If you want the bottom element to always be visible at the bottom of the viewport (not just the main area), we can adjust further.