Grid system

The grid system is a flexbox based, 12-column system, which can be used to quickly and easily build websites and pages that look good on all screen sizes.

Example #

The grid system uses a series of containers, rows, and columns to layout and align content. It's built with flexbox (opens in new tab) and is fully responsive. Below is an example and an in-depth explanation for how the grid system comes together.

Column
Column
Column
HTML
<div class="container">
  <div class="row">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>

The above example creates three equal-width columns across all devices and viewports using our predefined grid classes. Those columns are centered in the page with the parent .container.

How it works #

Breaking it down, here's how the grid system comes together:

  • Our grid supports six responsive breakpoints. Breakpoints are based on min-width media queries, meaning they affect that breakpoint and all those above it (e.g., .col-sm-4 applies to sm, md, lg, xl, and xxl). This means you can control container and column sizing and behavior by each breakpoint.
  • Containers center and horizontally pad your content. Use .container for a responsive pixel width, .container-fluid for width: 100% across all viewports and devices, or a responsive container (e.g., .container-md) for a combination of fluid and pixel widths.
  • Rows are wrappers for columns. Each column has horizontal padding (called a gutter) for controlling the space between them. This padding is then counteracted on the rows with negative margins to ensure the content in your columns is visually aligned down the left side. Rows also support modifier classes to uniformly apply column sizing and gutter classes to change the spacing of your content.
  • Columns are incredibly flexible. There are 12 template columns available per row, allowing you to create different combinations of elements that span any number of columns. Column classes indicate the number of template columns to span (e.g., .col-4 spans four). The width values are set in percentages so you always have the same relative sizing.
  • Gutters are also responsive and customizable. Gutter classes are available across all breakpoints, with all the same sizes as our margin and padding spacing. Change horizontal gutters with .gx-* classes, vertical gutters with .gy-*, or all gutters with .g-* classes. The .g-0 class is also available to remove gutters.

Be aware of the limitations and bugs around flexbox (opens in new tab), like the inability to use some HTML elements as flex containers. At this time, however, even semi-modern browsers should be able to handle the grid system without any issue whatsoever.

Grid options #

The grid system can adapt across all six default breakpoints. The six default grid tiers are as follows:

  1. Extra small — xs
  2. Small — sm
  3. Medium — md
  4. Large — lg
  5. Extra large — xl
  6. Extra extra large — xxl

As noted above, each of these breakpoints have their own container, unique class prefix, and modifiers. Here's how the grid changes across these breakpoints:

Extra small
< 576px
Small
≥ 576px
Medium
≥ 768px
Large
≥ 992px
X-Large
≥ 1200px
XX-Large
≥ 1400px
Container max-width None (auto) 540px 720px 960px 1140px 1320px
Class prefix .col- .col-sm- .col-md- .col-lg- .col-xl- .col-xxl-
# of columns 12
Gutter width 1.5rem (0.75rem on left and right)
Custom gutters Yes
Nestable Yes
Column ordering Yes

Column classes #

The column classes come in the following formats:

  • .col / .col-{value}
  • .col-{breakpoint} / .col-{breakpoint}-{value}

Without a {value}, e.g., .col, .col-sm, etc., the columns will take up equal-widths. The {value} can be any whole number from 1 to 12, or auto (sizes column based on the natural width of their content). All of these classes are shown in action in the next few sections. You can also learn about alignment, ordering, and offsetting of columns in its own documentation section.

Auto-layout columns #

Utilize breakpoint-specific column classes for easy column sizing without an explicit numbered class like .col-sm-10, .col-md-8, .col-lg-6, etc.

Auto-layout columns Equal-width #

For example, here are two grid layouts that apply to every device and viewport, from xs to xxl. Add any number of unit-less classes for each breakpoint you need and every column will be the same width.

Column
Column
Column
Column
Column
HTML
<div class="container">
  <div class="row mb-3">
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
  <div class="row">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>

Auto-layout columns Setting one column width #

Auto-layout for flexbox grid columns also means you can set the width of one column and have the sibling columns automatically resize around it. You may use predefined grid classes (as shown below), or inline widths. Note that the other columns will resize no matter the width of the center column.

Column
Col-5
Column
HTML
<div class="container">
  <div class="row">
    <div class="col">Column</div>
    <div class="col-5">Col-5</div>
    <div class="col">Column</div>
  </div>
</div>

Auto-layout columns Variable width content #

Use .col-auto / .col-{breakpoint}-auto to size columns based on the natural width of their content.

1
2
3
Variable
5
Column
Variable
Column
HTML
<div class="container">
  <div class="row justify-content-md-center mb-3">
    <div class="col">1</div>
    <div class="col">2</div>
    <div class="col">3</div>
    <div class="col-auto">Variable</div>
    <div class="col-2">5</div>
  </div>
  <div class="row justify-content-md-center">
    <div class="col col-md-4 col-lg-3">Column</div>
    <div class="col-md-auto">Variable</div>
    <div class="col col-md-4 col-lg-3">Column</div>
  </div>
</div>

Responsive classes #

Halfmoon's grid includes six tiers of predefined classes for building complex responsive layouts.

Responsive classes All breakpoints #

For grids that are the same from the smallest of devices to the largest, use the .col and .col-* classes. Specify a numbered class when you need a particularly sized column; otherwise, feel free to stick to .col.

Column
Column
Column
Col-8
Col-4
HTML
<div class="container">
  <div class="row mb-3">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
  <div class="row">
    <div class="col-8">Col-8</div>
    <div class="col-4">Col-4</div>
  </div>
</div>

Responsive classes Stacked to horizontal #

Using a single set of .col-md-* classes, you can create a basic grid system that starts out stacked and becomes horizontal at the medium breakpoint (md).

Col-md
Col-md
Col-md
Col-md-8
Col-md-4
HTML
<div class="container">
  <div class="row mb-3">
    <div class="col-md">Col-md</div>
    <div class="col-md">Col-md</div>
    <div class="col-md">Col-md</div>
  </div>
  <div class="row">
    <div class="col-md-8">Col-md-8</div>
    <div class="col-md-4">Col-md-4</div>
  </div>
</div>

Responsive classes Mix and match #

Don't want your columns to simply stack in some grid tiers? Use a combination of different classes for each tier as needed. See the example below for a better idea of how it all works.

Col-6-md-4
Col-6-md-4
Col-6-md-4
Col-6
Col-6
HTML
<div class="container">
  <div class="row justify-content-center mb-3">
    <div class="col-6 col-md-4">Col-6-md-4</div>
    <div class="col-6 col-md-4">Col-6-md-4</div>
    <div class="col-6 col-md-4">Col-6-md-4</div>
  </div>
  <div class="row">
    <div class="col-6">Col-6</div>
    <div class="col-6">Col-6</div>
  </div>
</div>

Responsive classes Row columns #

Use the responsive .row-cols-{columns} / .row-cols-{breakpoint}-{columns} classes to quickly set the number of columns that best render your content and layout, where {columns} is the number of columns per row. Whereas normal .col-* classes apply to the individual columns (e.g., .col-md-4), the row columns classes are set on the parent .row as a shortcut.

Use these row columns classes to quickly create basic grid layouts or to control your card layouts.

Column
Column
Column
Column
Column
Column
Column
Column
Column
Column
Column
HTML
<div class="container">
  <div class="row row-cols-2 mb-3">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
  <div class="row row-cols-3 mb-3">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
  <div class="row row-cols-2 row-cols-md-4">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>

With .row-cols-auto you can give the columns their natural width.

Column
Column
Column
HTML
<div class="container">
  <div class="row row-cols-auto justify-content-center">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>

The number of columns per row constraint may break if you add in a numbered column that takes up too much space. For instance, in the example below, the .col-6 column makes it impossible to accommodate the three other equally sized columns in the same row, therefore, the last one wraps to a new line.

Column
Column
Col-6
Column
HTML
<div class="container">
  <div class="row row-cols-4 justify-content-center">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col-6">Col-6</div>
    <div class="col">Column</div>
  </div>
</div>

Nesting #

To nest your content with the default grid, add a new .row of columns within an existing .col-* / .col-{breakpoint}-* column. Nested rows should include a set of columns that add up to 12 or fewer (it is not required that you use all 12 available columns).

Col-md-3
Col-8-md-6
Col-4-md-6
HTML
<div class="container">
  <div class="row">
    <div class="col-md-3">Col-md-3</div>
    <div class="col-md-9 p-2">
      <div class="row">
        <div class="col-8 col-md-6">Col-8-md-6</div>
        <div class="col-4 col-md-6">Col-4-md-6</div>
      </div>
    </div>
  </div>
</div>
Up next
Columns

Help us grow

Our main goal is to make Halfmoon the go-to framework for building websites, dashboards and tools. If you believe in our mission, consider becoming a sponsor and help us grow.

You can email us directly if you have any queries. We are always happy to answer.

Subscribe for updates

We will notify you when the framework gets a substantial update. No spam ever.

Follow us on Twitter so that you can stay updated that way.