Drupal Layout With Susy

If you knew Susy, like we know Susy.
Drupal

Instead of working under a rigid 12 column framework, Susy gives you a tool to create your own grid system. You have the flexibility to roll any kind of grid including fluid, adaptive and fixed layouts with as many or as few columns as you like. Layout settings are all defined within a single variable, giving you the power to quickly access and manage values such as column and gutter widths. This makes your grid so much easier to maintain. Susy will also help you produce builds faster, simply because you don’t have to do any of the maths. Every measurement is calculated and managed for you by Susy’s API. It even supports different kinds of units such as em, px and percentages!

Variables

Generally we prefer to use multiple layouts for different breakpoints. We define them like so:


$susy: (
  columns: 12,
  gutter-position: after,
  global-box-sizing: border-box
);

$susy-medium: (
  container: 43.75em // 700px
);

$susy-large: (
  container: 60em // 960px
);

This defines three different layouts, a default (mobile), medium and large grid. Changing grid settings on the fly becomes simple and makes them extremely flexible. For example, we could change our gutter widths for our $susy-large layout:


$susy-large: (
  container: 60em // 960px,
  gutters: 20/60
);

This would effect the gutter width for all of our $susy-large grids throughout our stylesheets.

Mixins & Functions

We interact with layouts through the Susy API. There is a lot you can do with the Susy, but for now its best to keep it simple. We frequently use:


// Outputs a layout's container
@include container($layout);
 
// Outputs layout specific column styles.
@include span($columns, $layout);

// Outputs layout specific gallery styles.
@include gallery($columns, $layout);

// Returns a layout specific column width.
.selector {
  width: span($columns, $layout);
}

// Returns a layout specific container width.
.selector {
  width: container($layout);
}

You can find everything you need to learn Susy from interacting with column sizing to complex isolated grids from the documentation.

Drupal Selector Variables

We also use helpful selector variables to quickly target common selectors:


$select-block: unquote('> .block-content');
$select-view: unquote('> .view-content > .views-row');
$select-field: unquote('> .field__items > .field__item');

They are very powerful for creating a collapse grid of columns, such a view:


// view-id-dg_posts: 3 column grid layout
.view-id-dg_posts #{$select-view} {
  @include gallery(4);
}

These selector variables allow you to apply the styles with the right specificity while avoiding writing the same code over and over again.

Compile Times

Some of our compile times have become rather long (up to 20 seconds) while trying to compile our stylesheets. When a build gets bigger and bigger, we have to use Susy to create more and more layouts. This snowball effect can cause a large amount of wait time because of the amount of things it has to process. We are hoping that this will dramatically decrease with the integration of Susy & Libsass. Until then, try to optimise your stylesheets with @extend and %placeholders when you can to reduce processing times:


// 3 Column Layout Placeholder
%grid-layout-3-column {
  @media (min-width: 768px) {
    @include gallery(3);
  }
}

// Apply 3 column layout to a view
.view-id-dg_posts #{$select-view} {
  @extend %grid-layout-3-column
}

// Apply 3 column layout to a view
.view-id-dg_gallery #{$select-view} {
  @extend %grid-layout-3-column
}

Final Thoughts

Using Sass has definitely saved us a lot of development time for our Drupal builds. We don't have to calculate the width of 3 columns in a 12 column grid any more. We can quickly adapt a grid system for any kind of design without having to any complex maths. If you are not using Susy for your grids or haven’t at least tried it, you are missing out.