Fixed header, footer with scrollable content
To create a fixed header and footer with a scrollable content section in between using CSS, set your header and footer to position: fixed;
, positioning them to top: 0;
and bottom: 0;
respectively. Apply overflow-y: scroll;
to your content and use height: calc(100vh - headerHeight - footerHeight);
to adjust its size. Remember to add the appropriate padding
to the content to ensure it doesn't overlap with the header/footer:
Now wrap your page content within a <div class="content">
. This layout ensures that the header and footer stay in place while the content scrolls.
When Flexbox meets scroll
Flexbox presents another flexible solution to this layout challenge. Handling dynamic heights more effectively, and responding well to screen size changes.
No need to worry about manually calculating the content height using calc()
, the browser does this for you!
Structured layouts with Grid
If you fancy more granular control over your grid items, CSS Grid is the perfect candidate for complex layouts.
Ensure margin: 0
on the body, apply 100% height
, and don't forget to set box-sizing: border-box
.
Tricky considerations and tips
- Cross-browser support: Remember to check CSS features compatibility on sites like caniuse.com.
- Test through variety: Check compatibility across different screen sizes and devices.
- Accessibility first: Make content accessible with screen readers and keyboard navigability.
- Performance matters: Keep your layout simple to render pages faster.
Responsiveness with media queries
Utilizing media queries, you can ensure your design stays flexible across different devices.
Debugging common issues
- Overlapping content: Ensure equal padding and respective heights for fixed elements.
- Scroll area: Set overflow on the content, not the parent.
- Responsive woes: Make use of media queries, percentages, and viewport units.
Common pitfalls
- Avoid complex styles and layouts, simplicity wins.
- Never neglect to reset default body margins.
- Watch your z-index values to keep layers in order.
Was this article helpful?