I’ve laid pages out many different ways, but my favorite is using CSS grid along with a few other tricks. The example below is vanilla HTML/CSS; however, the concept can be adapted to any SPA framework.
Before going into the full example, I’ll point out a few interesting tidbits.
body {
min-height: 100vh;
margin: 0;
display: grid;
}
This ensures the body occupies the full screen height, allowing us to stick the page footer to the bottom if the content isn’t tall enough to push it offscreen. The vh
unit is “view height” and 100vh
means “100% of the view height.”
.app {
display: grid;
grid-template-rows:
[header-start]
min-content
[header-end content-start side-start]
1fr
[content-end side-end footer-start]
min-content
[footer-end];
grid-template-columns:
[header-start content-start footer-start]
auto
[content-end side-start]
300px
[side-end header-end footer-end];
}
The above sets up the page with a grid, naming each line of the grid (names fall nicely between the [ ]
brackets). In the full example, you will see how we restructure the grid through media queries. As long as all of our line names are present, the page will reflow seamlessly, offering a clean entry point into high-level, responsive design.
.header {
// ...
grid-row: header-start / header-end;
grid-column: header-start / header-end;
// ...
}
The above allows us to define what part of the grid the header should occupy using the named lines from the grid definition.
.header {
// ...
position: sticky;
top: 0;
// ...
}
The above approach allows the header to remain at the top of the page when scrolling. You can choose a different value for top to get it to stick to a different point. The benefit of position: sticky
over fixed or absolute is that it allows the header to continue to be a part of the layout of the grid. With something like position: fixed
you would need to add some top margin to the content to compensate for the height of the header. Less than ideal.
.content {
padding: 20px;
grid-row: content-start / content-end;
grid-column: content-start / content-end;
background: green;
display: grid;
grid-template-columns: repeat(auto-fill, 500px);
grid-template-rows: 300px;
grid-auto-columns: 500px;
grid-auto-rows: 300px;
grid-auto-flow: dense;
grid-row-gap: 20px;
grid-column-gap: 20px;
justify-content: center;
}
The above is simply a way to lay out the boxes to give the content the necessary height.
And, finally, the full example is here.
Phone
Wide Screen
Narrow Screen
Looking for more like this?
Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.
Kotlin Multiplatform
July 14, 2022A brief look at Kotlin Multiplatform Mobile, a newer cross-platform mobile application framework.
Read moreWhat to know about the cost of custom app development
January 10, 2024We hear a lot of ideas for apps at MichiganLabs. People from large enterprises and small startups, located all over the world, call us to explore their mobile and web-based application ideas, and one of the first questions they ask is: How much is this app going to cost?
Read more2022 Best and Brightest Winners in West Michigan
May 2, 2022The Best and Brightest Companies to Work For® competition identifies and honors organizations that display a commitment to excellence in operations and employee enrichment that lead to increased productivity and financial performance!
Read more