Layout - Overview
Often designs call for complex layout structures, we use a grid system to provide consistency.
Breakpoints
We use a set of breakpoints to define the width of the viewport and the corresponding grid system. Here's a summary of the breakpoints and how they relate to our grid:
Name | Viewport width | Margin/Gutter | Spacing Unit |
---|---|---|---|
XS | 0-479px | 16px | 4 |
SM | 480-639px | 16px | 4 |
MD | 640-799px | 16px | 8 |
LG | 800-959px | 24px | 8 |
XL | 960-1247px | 24px | 12 |
XXL | ≥ 1248 | 24px | 12 |
Layouts in React
When we build UI we take a slightly different approach in development, compared to Design. Elements are positioned relative to parent containers rather than the page.
Sky UI provides a number of layout-centric components to simplify the process:
Box
- an abstraction of the CSS Box Model.Container
- a width constrained container as a shorthand to achieve different layouts.Flex
- an abstraction of the CSS Flexbox api.Grid
- an abstraction of the CSS Grid api.Position
- an abstraction on the CSSposition
property and related values.
Let's take a look at how we can use them in practice.
For the most accurate representation of the examples, view in fullscreen.
Constraining content
Often, content won't take up the full 12 columns of available horizontal space, many layouts will have content sat within a 6 or 8 column width.
Rather than creating complex Flex or Grid layouts to achieve this, we can use the Container component as a shorthand:
The Container
and container()
values account for the {{xs: 4, lg: 5}}
gutter sizes. Containers should never be nested as this can result in stacked gutters etc.
Adding a Grid to layout content
Now we have the outer most we can use a nested Grid to create more complex layouts:
Utilising grid-template-areas
We can use CSS grid-template-areas
to create more complex compositions of content.
In this example we use $gridTemplateAreas
to set the following template:
a cb c
This approach can often allow for more intuitive Grid layout with semantically named grid-areas
.
Flowing content with Flex
CSS flexbox is an incredibly useful tool for laying out content. Where Grid allows us to create content layouts aligning to rows and columns, Flex allows us to create more fluid content structures, focusing on a single axis.
At it's most simple, we can use Flex to wrap stacked content and apply consistent spacing using $gap
as well as adding some alignment to horizontally center content.
Flex is handy when we have a list of elements which we want to flow based on the container size rather than aligning to a preset grid.
Here we set the Flex element to have our standard gutter gap between children, we also set them to wrap onto the next row if they don't fit.
Read more on MDNBeyond the basics
For more complex layouts, take a look that how some of our components are constructed.
If you don't find what you're looking for, please reach out to the #sky-ui-development channel and we can help you out.