An object for creating simple flex layouts, with variable column widths and customisable spacing
.o-layout__item.u-is-first { order: 1; }
.o-layout__item.u-is-last { order: 10; }
The layout
object is used to quickly lay items out in flex columns on the page.
By default, each item in the layout will take up 100% width. This can be customised per item to achieve quick layout compositions.
Each layout item is given left and top spacing, to give it some breathing room from the other items.
The parent layout then applies a negative margin on the left and top, matching the value applied to the items. This off any excess margin and pulls the layout flush against the container edge.
This ensures the layout lays out your content, without adding any extra margin on the outsides. Great!
Items are laid out with spacing between them, all taking 100% width
<div class="layout">
<div class="layout__item">A full width item</div>
<div class="layout__item">And another</div>
<div class="layout__item">And a final one</div>
</div>
The Layout object in Captain provides a standardised way to define a layout of items, and then use it effortlessly.
For anything more advanced, we'd recommend looking at CSS Grids instead.
Note that the layout applies a negative margin that chops off the extra margin on the top and left items, pulling the layout flush against its container.
The following example shows the margin between the items, and the thick purple line is the container. Any margin outside the container is completely hidden and will not affect any other elements.
<div class="layout">
<div class="layout__item w-1/3">1</div>
<div class="layout__item w-1/3">2</div>
<div class="layout__item w-1/3">3</div>
<div class="layout__item w-1/3">4</div>
<div class="layout__item w-1/3">5</div>
<div class="layout__item w-1/3">6</div>
</div>
Do try to keep just the layout and width classes on these elements
<div class="layout">
<div class="layout__item">An item</div>
</div>
Don't add other classes that interfere with margin or padding as they'll break the layout
- <div class="layout my-10">
<div class="layout__item">A layout that's will have too much top margin</div>
</div>
When support for Flex Gap (`gap`) becomes wider, we can remove this margin clipping functionality. Support for it in Captain is already available. To make use of it early, add `flexGap` to the `captain.support` object in `tailwind.config.js`. At time of writing it's only recently available in Safari, and for a short while in Chrome.
The layout works with the width utilities to allow the items to take up different sizes.
<div class="layout">
<div class="layout__item w-1/2">
<div class="rounded-md bg-indigo-500 text-white font-semibold py-2 px-3">1/2</div>
</div>
<div class="layout__item w-1/2">
<div class="rounded-md bg-indigo-500 text-white font-semibold py-2 px-3">1/2</div>
</div>
<div class="layout__item w-full">
<div class="rounded-md bg-indigo-500 text-white font-semibold py-2 px-3">Full width</div>
</div>
<div class="layout__item w-1/4">
<div class="rounded-md bg-indigo-500 text-white font-semibold py-2 px-3">1/4</div>
</div>
<div class="layout__item w-1/4">
<div class="rounded-md bg-indigo-500 text-white font-semibold py-2 px-3">1/4</div>
</div>
<div class="layout__item w-1/2">
<div class="rounded-md bg-indigo-500 text-white font-semibold py-2 px-3">1/2</div>
</div>
</div>
The layout
class also includes spacing modifiers out of the box.
The default config creates these classes
from Tailwind's space
config, which in turn defaults to
Tailwind's global spacing config.
A layout without a modifier defaults to a spacing of 1rem on both the top and left side.
<div class="layout layout--gap-10">
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
</div>
<div class="layout layout--gap-32">
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
</div>
You can also size x and y spacing independently, using classes like layout--gap-x-8
and layout--gap-y-10
.
<div class="layout layout--gap-x-10 layout--gap-y-32">
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
</div>
<div class="layout layout--gap-10 md:layout--gap-24 xl:layout--gap-32">
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
<div class="layout__item w-1/2">1/2</div>
</div>
To change the gaps available to the layout
class, specify the name and gap size using the gap
option in the theme.layout
section of your config file:
// tailwind.config.js
module.exports = {
theme: {
layout: (theme) => ({
+ gap: {
+ DEFAULT: '1rem',
+ ...theme('space'),
+ },
}),
},
}
If you only have a single layout gap size, you can specify a single size as a shorthand:
// tailwind.config.js
module.exports = {
theme: {
layout: {
+ gap: '1rem',
},
},
}
By default, only responsive variants are generated for layout utilities.
You can control which variants are generated for the layout utilities by modifying the layout
property in the variants
section of your tailwind.config.js
file.
For example, this config will disable all variants:
// tailwind.config.js
module.exports = {
variants: {
// ...
+ layout: [],
}
}
If you don't plan to use the layout utilities in your project, you can disable them entirely by setting the layout
property to false
in the captain.plugins
section of your config file:
// tailwind.config.js
module.exports = {
captain: {
plugins: {
// ...
+ layout: false,
}
}
}