Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Last active March 22, 2018 14:45
Show Gist options
  • Save amelieykw/84086bc340bef3c331e697fbd36a0104 to your computer and use it in GitHub Desktop.
Save amelieykw/84086bc340bef3c331e697fbd36a0104 to your computer and use it in GitHub Desktop.
[Bootstrap - 01 Layout - 01 Grid]#Bootstrap #CSS #Grid #Layout #Content #Component

Grid System

  • 12 column system
  • 5 default responsive tiers
  • Sass variables and mixins
  • dozens of predefined classes

How it works

  • uses a series of containers, rows and columns to layout and align content
  • fully reponsive
EX.

create 3 equal-width columns on small, medium, large and extra large devices using predefined grid classes (cols are centered in the page with the parent .container) Grid System - 3 cols

<div class="container">
  <div class="row">
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
  </div>
</div>

# 1. .container 
# - provide a means to center and horizontally pad your site's contents
# - .container : for a reponsive pixel width 
# - .container-fluid : width: 100% across all viewport and device sizes

# 2. .row 
# - Rows are wrappers for columns.

# 3. .col
# - content must be placed within columns 
# - only columns may be immediate children of rows.
# - grid columns without a specified width will automatically layout as equal width columns.
# - Column classes indicate the number of columns you’d like to use out of the possible 12 per row. (e.g. .col-4 : 3 equal-width cols per row)

# flexible to parent 
# - Column widths are set in percentages, so they’re always fluid and sized relative to their parent element.
# - Columns have horizontal padding to create the gutters between individual column
# - you can remove the margin from rows and padding from columns with .no-gutters on the .row.

# responsive layout
# - five grid breakpoints, one for each responsive breakpoint: all breakpoints (extra small), small, medium, large, and extra large.
# - Grid breakpoints are based on minimum width media queries, meaning apply to that one breakpoint and all those above it 
# - (e.g., .col-sm-4 applies to small, medium, large, and extra large devices, but not the first xs breakpoint).

# - You can use predefined grid classes (like .col-4) or Sass mixins for more semantic markup.

Utilize breakpoint-specific column classes for easy column sizing without an explicit numbered class like .col-sm-6.

Equal-width

Add any number of unit-less classes for each breakpoint you need and every column will be the same width.

<div class="container">
  <div class="row">
    <div class="col">
      1 of 2
    </div>
    <div class="col">
      2 of 2
    </div>
  </div>
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col">
      2 of 3
    </div>
    <div class="col">
      3 of 3
    </div>
  </div>
</div>

Equal-width multi-row

Equal-width columns can be broken into multiple lines.

Create equal-width columns that span multiple rows by inserting a .w-100 where you want the columns to break to a new line.

Make the breaks responsive by mixing the .w-100 with some responsive display utilities.

<div class="row">
  <div class="col">col</div>
  <div class="col">col</div>
  <div class="w-100"></div>
  <div class="col">col</div>
  <div class="col">col</div>
</div>

Setting one column width

Auto-layout for flexbox grid columns :

  • can set the width of one column
  • have the sibling columns automatically resize around it.

the other columns will resize no matter the width of the center column.

<div class="container">
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col-6">
      2 of 3 (wider)
    </div>
    <div class="col">
      3 of 3
    </div>
  </div>
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col-5">
      2 of 3 (wider)
    </div>
    <div class="col">
      3 of 3
    </div>
  </div>
</div>

Variable width content

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

<div class="container">
  <div class="row justify-content-md-center">
    <div class="col col-lg-2">
      1 of 3
    </div>
    <div class="col-md-auto">
      Variable width content
    </div>
    <div class="col col-lg-2">
      3 of 3
    </div>
  </div>
  <div class="row">
    <div class="col">
      1 of 3
    </div>
    <div class="col-md-auto">
      Variable width content
    </div>
    <div class="col col-lg-2">
      3 of 3
    </div>
  </div>
</div>

five tiers of predefined classes for building complex responsive layouts.

Customize the size of your columns on :

  • extra small
  • small
  • medium
  • large
  • extra large

devices however you see fit.

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.

<div class="row">
  <div class="col">col</div>
  <div class="col">col</div>
  <div class="col">col</div>
  <div class="col">col</div>
</div>
<div class="row">
  <div class="col-8">col-8</div>
  <div class="col-4">col-4</div>
</div>

Figure - Even when it's extra-small, it stays the same.

Stacked to horizontal

Using a single set of .col-sm-* classes, you can create a basic grid system that starts out stacked before becoming horizontal with at the small breakpoint (sm).

<div class="row">
  <div class="col-sm-8">col-sm-8</div>
  <div class="col-sm-4">col-sm-4</div>
</div>
<div class="row">
  <div class="col-sm">col-sm</div>
  <div class="col-sm">col-sm</div>
  <div class="col-sm">col-sm</div>
</div>

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.

<!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="row">
  <div class="col-12 col-md-8">.col-12 .col-md-8</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>

<!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
<div class="row">
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
  <div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>

<!-- Columns are always 50% wide, on mobile and desktop -->
<div class="row">
  <div class="col-6">.col-6</div>
  <div class="col-6">.col-6</div>
</div>

Figure - original Figure - (Col 01)Stack the columns on mobile by making one full-width and the other half-width / (Col 02)Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop Figure - (Col 03)Columns are always 50% wide, on mobile and desktop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment