Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Layout Design Philosophy</h1>
<ol>
  <li>
    <code>layout</code>: for top-level layout (page). This aims to accommodate the most common layouts.
  </li>
  <li>
    <code>tiles</code> and <code>list</code>: for low-level layout (content). Can be used inside <code>layout-item</code>.
  </li>
  <li>
    <code>grid</code> <strong>(optional)</strong>: for advanced page and content layouts. Can be used in place of <code>layout</code>, <code>tiles</code>, and <code>list</code>, but only if you must.
  </li>
</ol>

<section>
  <h2>Layout</h2>
  <p>A content container with a single row of items. Optionally to stack vertically at a specific breakpoint.</p>
  <p><em>Note: this is the default top-level layout element.</em></p>
  <layout stack-items="@until-small-bp">
    <layout-item width="25%@from-small-bp">
      <placeholder>
        <ul>
          <li>0px ~ small-bp: auto</li>
          <li>small-bp &amp; up: 25%</li>
        </ul>
      </placeholder>
    </layout-item>
    <layout-item width="50%@from-small-bp">
      <placeholder>
        <ul>
          <li>0px ~ small-bp: auto</li>
          <li>small-bp &amp; up: 50%</li>
        </ul>
      </placeholder>
    </layout-item>
    <layout-item width="25%@from-small-bp">
      <placeholder>
        <ul>
          <li>0px ~ small-bp: auto</li>
          <li>small-bp &amp; up: 25%</li>
        </ul>
      </placeholder>
    </layout-item>
  </layout>
  <hr>
  <layout stack-items="@until-small-bp">
    <layout-item width="33%@from-small-bp">
      <placeholder>
        <ul>
          <li>0px ~ small-bp: auto</li>
          <li>small-bp &amp; up: 33%</li>
        </ul>
      </placeholder>
    </layout-item>
    <layout-item width="67%@from-small-bp">
      <placeholder>
        <ul>
          <li>0px ~ small-bp: auto</li>
          <li>small-bp &amp; up: 67%</li>
        </ul>
      </placeholder>
    </layout-item>
  </layout>
  <hr>
  <layout>
    <layout-item width="50%@from-xsmall-bp 67%@from-small-bp 75%@from-medium-bp">
      <placeholder>
        <h3>Main</h3>
        <ul>
          <li>0px ~ xsmall-bp: auto</li>
          <li>xsmall-bp ~ small-bp: 50%</li>
          <li>small-bp ~ medium-bp: 67%</li>
          <li>medium-bp &amp; up: 75%</li>
        </ul>
      </placeholder>
    </layout-item>
    <layout-item width="50%@from-xsmall-bp 33%@from-small-bp 25%@from-medium-bp">
      <placeholder>
        <h3>Aside</h3>
        <ul>
          <li>0px ~ xsmall-bp: auto</li>
          <li>xsmall-bp ~ small-bp: 50%</li>
          <li>small-bp ~ medium-bp: 33%</li>
          <li>medium-bp &amp; up: 25%</li>
        </ul>
      </placeholder>
    </layout-item>
  </layout>
</section>

<section>
  <h2>Tiles</h2>
  <p>A group of even-width items that flows naturally (must have 2 items to begin with). Optionally to set max items per row.</p>
  <h3>Items per row: 2 starting from xsmall bp (default)</h3>
  <tiles>
    <tiles-item>
      <placeholder>1<br>(with the similar length of content)</placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>2<br>(with the similar length of content)</placeholder>
    </tiles-item>
  </tiles>
  <h3>Items per row: 3 starting from small bp</h3>
  <tiles items-per-row="3@from-small-bp">
    <tiles-item>
      <placeholder>1<br>(with the similar length of content)</placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>2<br>(with the similar length of content)</placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>3<br>(with the similar length of content)</placeholder>
    </tiles-item>
  </tiles>
  <h3>Items per row: 4 starting from medium bp</h3>
  <tiles items-per-row="4@from-medium-bp">
    <tiles-item>
      <placeholder>1<br>(with the similar length of content)</placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>2<br>(with the similar length of content)</placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>3<br>(with the similar length of content)</placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>4<br>(with the similar length of content)</placeholder>
    </tiles-item>
  </tiles>
  <h3>Advanced combo</h3>
  <ul>
    <li>Items per row: 3 starting from small bp</li>
    <li>Items per row: 4 starting from medium bp</li>
    <li>Items per row: 6 starting from large bp</li>
    <li>Spacing: small</li>
  </ul>
  <tiles items-per-row="3@from-small-bp 4@from-medium-bp 6@from-large-bp" spacing="small">
    <tiles-item>
      <placeholder>
        <figure>
          <img src="http://www.fillmurray.com/g/800/800" alt="Bill">
        </figure>
      </placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>
        <figure>
          <img src="https://www.placecage.com/g/800/800" alt="Nic">
        </figure>
      </placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>
        <figure>
          <img src="http://www.fillmurray.com/g/800/800" alt="Bill">
        </figure>
      </placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>
        <figure>
          <img src="https://www.placecage.com/g/800/800" alt="Nic">
        </figure>
      </placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>
        <figure>
          <img src="http://www.fillmurray.com/g/800/800" alt="Bill">
        </figure>
      </placeholder>
    </tiles-item>
    <tiles-item>
      <placeholder>
        <figure>
          <img src="https://www.placecage.com/g/800/800" alt="Nic">
        </figure>
      </placeholder>
    </tiles-item>
  </tiles>
</section>

<section>
  <h2>List</h2>
  <p>A group of items that flows either horizontally or vertically. Optionally to set horizontal and vertical alignment of items.</p>
  <h3>Inline list</h3>
  <list role="list">
    <list-item role="listitem"><placeholder>start</placeholder></list-item>
    <list-item role="listitem"><placeholder>start</placeholder></list-item>
    <list-item role="listitem"><placeholder>start</placeholder></list-item>
  </list>
  <list x-align-items="center" role="list">
    <list-item role="listitem"><placeholder>center</placeholder></list-item>
    <list-item role="listitem"><placeholder>center</placeholder></list-item>
    <list-item role="listitem"><placeholder>center</placeholder></list-item>
  </list>
  <list x-align-items="end" role="list">
    <list-item role="listitem"><placeholder>end</placeholder></list-item>
    <list-item role="listitem"><placeholder>end</placeholder></list-item>
    <list-item role="listitem"><placeholder>end</placeholder></list-item>
  </list>
  <list x-align-items="justify" role="list">
    <list-item role="listitem"><placeholder>justify</placeholder></list-item>
    <list-item role="listitem"><placeholder>justify</placeholder></list-item>
    <list-item role="listitem"><placeholder>justify</placeholder></list-item>
  </list>
  <h3>Block list</h3>
  <list block role="list">
    <list-item role="listitem"><placeholder>1</placeholder></list-item>
    <list-item role="listitem"><placeholder>2</placeholder></list-item>
    <list-item role="listitem"><placeholder>3</placeholder></list-item>
  </list>
</section>

<hr>

<section>
  <p><mark>&hellip;and if you really insist:</mark></p>
  <h2>Grid</h2>
  <p>A content container with a group of items that flows naturally within a traditional grid. Optionally to set column count and individual item&rsquo;s column span at specific breakpoints.</p>
  <grid cols="2@from-xsmall-bp 4@from-medium-bp 6@from-large-bp 12@from-xlarge-bp">
    <grid-item span="2@from-medium-bp">
      <placeholder>
        <ul>
          <li>0px ~ medium-bp: auto</li>
          <li>medium-bp &amp; up: 2 cols</li>
        </ul>
      </placeholder>
    </grid-item>
    <grid-item>
      <placeholder>auto width always</placeholder>
    </grid-item>
    <grid-item span="3@from-large-bp">
      <placeholder>
        <ul>
          <li>0px ~ large-bp: auto</li>
          <li>large-bp &amp; up: 3 cols</li>
        </ul>
      </placeholder>
    </grid-item>
    <grid-item span="4@from-medium-bp">
      <placeholder>
        <ul>
          <li>0px ~ medium-bp: auto</li>
          <li>medium-bp &amp; up: 4 cols</li>
        </ul>
      </placeholder>
    </grid-item>
    <grid-item>
      <placeholder>auto width always</placeholder>
    </grid-item>
    <grid-item>
      <placeholder>auto width always</placeholder>
    </grid-item>
    <grid-item span="2@from-medium-bp 6@from-large-bp 12@from-xlarge-bp">
      <placeholder>
        <ul>
          <li>0px ~ medium-bp: auto</li>
          <li>medium-bp ~ large-bp: 2 cols</li>
          <li>large-bp ~ xlarge-bp: 6 cols</li>
          <li>xlarge-bp &amp; up: 12 cols</li>
        </ul>
      </placeholder>
    </grid-item>
  </grid>
</section>
              
            
!

CSS

              
                $columns: 1, 2, 3, 4, 6, 12;
$alignments: start, center, end, justify;

$breakpoints: (
  xsmall: 400px,
  small: 600px,
  medium: 900px,
  large: 1200px,
  xlarge: 1440px
);

@function breakpoint($name) {
  @return map-get($breakpoints, $name);
}

:root {
  --xspacing: 2rem;
  --yspacing: 1.5rem;
  --layoutcolor: navy;
  --placeholdercolor: mediumblue;
  
  --pagemaxwidth: #{breakpoint(xlarge)};
  --pagexspacing: var(--xspacing);
  
  @media (min-width: (breakpoint(medium) + 0.5px)) {    
    --pagexspacing: calc(var(--xspacing) * 2);
  }
  
  @media (min-width: (breakpoint(xlarge) + 0.5px)) {    
    --pagexspacing: calc(var(--xspacing) * 2 + (100vw - var(--pagemaxwidth)) / 2);
  }
}

body {
  padding: var(--yspacing) var(--pagexspacing);
}

layout {
  display: flex;
  justify-content: center;
  
  @media (max-width: breakpoint(xsmall)) {
    flex-wrap: wrap;
    align-content: flex-start;

    layout-item:not(:first-of-type) {
      margin-top: var(--yspacing);
      margin-left: 0;
    }
  }
  
  @each $bp in $breakpoints {
    $bp-name: nth($bp, 1);

    @if (
      $bp-name != 'xsmall' and
      $bp-name != 'large' and
      $bp-name != 'xlarge'
    ) {
      @media (max-width: breakpoint(#{$bp-name})) {
        &[stack-items='@until-#{$bp-name}-bp'] {
          flex-wrap: wrap;
          align-content: flex-start;

          layout-item:not(:first-of-type) {
            margin-top: var(--yspacing);
            margin-left: 0;
          }
        }
      }
    }
  }
}

layout-item {
  display: flex;
  flex: 0 1 100%;
  flex-direction: column;

  &:not(:first-of-type) {
    margin-left: var(--xspacing);
  }

  @media (min-width: (breakpoint(xsmall) + 0.5px)) {    
    &[width*='50%@from-xsmall-bp'] {
      max-width: 50%;
    }
  }
  
  @each $bp in $breakpoints {
    $bp-name: nth($bp, 1);

    @if (
      $bp-name != 'xsmall' and
      $bp-name != 'large' and
      $bp-name != 'xlarge'
    ) {
      @media (min-width: (breakpoint(#{$bp-name}) + 0.5px)) {
        &[width*='25%@from-#{$bp-name}-bp'] {
          max-width: calc(3 / 12 * (100% - var(--xspacing) * 3));
        }

        &[width*='33%@from-#{$bp-name}-bp'] {
          max-width: calc(4 / 12 * (100% - var(--xspacing) * 2));
        }

        &[width*='50%@from-#{$bp-name}-bp'] {
          max-width: 50%;
        }

        &[width*='67%@from-#{$bp-name}-bp'] {
          max-width: calc(8 / 12 * (100% + var(--xspacing) * 2));
        }

        &[width*='75%@from-#{$bp-name}-bp'] {
          max-width: calc(9 / 12 * (100% + var(--xspacing) * 3));
        }
      }
    }
  }
}

tiles {
  --placeholdercolor: green;
  display: grid;
  grid-gap: var(--tilesxspacing, var(--xspacing));
  grid-row-gap: var(--tilesyspacing, var(--yspacing));
  grid-template-columns: repeat(1, 1fr);
  
  &:not(:last-child) {
    margin-bottom: var(--yspacing)
  }
  
  // Spacing
  &[spacing='small'] {
    --tilesxspacing: calc(var(--xspacing) / 2);
    --tilesyspacing: calc(var(--yspacing) / 2);
  }
  
  &[spacing='large'] {
    --tilesxspacing: calc(var(--xspacing) * 2);
    --tilesyspacing: calc(var(--yspacing) * 2);
  }
  
  // Items per row
  @media (min-width: (breakpoint(xsmall) + 0.5px)) {
    grid-template-columns: repeat(2, 1fr);
  }
  
  @each $bp in $breakpoints {
    $bp-name: nth($bp, 1);

    @if (
      $bp-name != 'xsmall' and
      $bp-name != 'xlarge'
    ) {
      @each $col in $columns {
        @if (
          $col != 1 and
          $col != 12
        ) {
          @media (min-width: (breakpoint(#{$bp-name}) + 0.5px)) {
            // Precise options
            &[items-per-row*='#{$col}@from-#{$bp-name}-bp'] {
              grid-template-columns: repeat(#{$col}, 1fr);
            }
          }
        }
      }
    }
  }

  // Minimalist but ambiguous options (do not use this if precise options are implemented)
  &[max-per-row='3'] {
    @media (min-width: (breakpoint(small) + 0.5px)) {
      grid-template-columns: repeat(3, 1fr);
    }
  }

  &[max-per-row='4'] {
    @media (min-width: (breakpoint(small) + 0.5px)) {
      grid-template-columns: repeat(3, 1fr);
    }
    
    @media (min-width: (breakpoint(medium) + 0.5px)) {
      grid-template-columns: repeat(4, 1fr);
    }
  }

  &[max-per-row='6'] {
    @media (min-width: (breakpoint(small) + 0.5px)) {
      grid-template-columns: repeat(3, 1fr);
    }
    
    @media (min-width: (breakpoint(medium) + 0.5px)) {
      grid-template-columns: repeat(4, 1fr);
    }
    
    @media (min-width: (breakpoint(large) + 0.5px)) {
      grid-template-columns: repeat(6, 1fr);
    }
  }
}

list {
  --placeholdercolor: purple;
  display: flex;
  flex-wrap: wrap;
  margin-top: calc(var(--yspacing) / -2);
  margin-left: calc(var(--xspacing) / -2);
  
  &:not(:last-child) {
    margin-bottom: var(--yspacing)
  }
}

list-item {
  flex: 0 0 auto;
  margin-top: calc(var(--yspacing) / 2);
  margin-left: calc(var(--xspacing) / 2);
}

list[block] {
  display: block;
  margin-top: 0;
  margin-left: 0;
  
  list-item {
    display: block;
    margin-left: 0;
    
    &:first-of-type {
      margin-top: 0;
    }
  }
}

@each $alignment in $alignments {
  list[x-align-items='#{$alignment}'] {
    @if $alignment == start {
      $alignment: flex-start;
    }
    @else if $alignment == end {
      $alignment: flex-end;
    }
    @else if $alignment == justify {
      $alignment: space-between;
    }
    justify-content: #{$alignment};
  }
}

grid {
  display: grid;
  grid-gap: var(--xspacing);
  grid-row-gap: var(--yspacing);
  grid-template-columns: repeat(1, 1fr);

  @media (min-width: (breakpoint(xsmall) + 0.5px)) {
    @each $col in $columns {
      @if (
        $col != 12 and
        $col != 6 and
        $col != 4 and
        $col != 3 
      ) {
        &[cols*='#{$col}@from-xsmall-bp'] {
          grid-template-columns: repeat(#{$col}, 1fr);
        }
          
        grid-item[span*='#{$col}@from-xsmall-bp'] {
          grid-column: span #{$col};
        }
      }
    }
  }

  @media (min-width: (breakpoint(small) + 0.5px)) {
    @each $col in $columns {
      @if (
        $col != 12 and
        $col != 6 and
        $col != 4
      ) {
        &[cols*='#{$col}@from-small-bp'] {
          grid-template-columns: repeat(#{$col}, 1fr);
        }
          
        grid-item[span*='#{$col}@from-small-bp'] {
          grid-column: span #{$col};
        }
      }
    }
  }

  @media (min-width: (breakpoint(medium) + 0.5px)) {
    @each $col in $columns {
      @if (
        $col != 12 and
        $col != 6
      ) {
        &[cols*='#{$col}@from-medium-bp'] {
          grid-template-columns: repeat(#{$col}, 1fr);
        }
          
        grid-item[span*='#{$col}@from-medium-bp'] {
          grid-column: span #{$col};
        }
      }
    }
  }

  @media (min-width: (breakpoint(large) + 0.5px)) {
    @each $col in $columns {
      @if (
        $col != 12
      ) {
        &[cols*='#{$col}@from-large-bp'] {
          grid-template-columns: repeat(#{$col}, 1fr);
        }
          
        grid-item[span*='#{$col}@from-large-bp'] {
          grid-column: span #{$col};
        }
      }
    }
  }

  @media (min-width: (breakpoint(xlarge) + 0.5px)) {
    @each $col in $columns {
      &[cols*='#{$col}@from-xlarge-bp'] {
        grid-template-columns: repeat(#{$col}, 1fr);
      }
          
      grid-item[span*='#{$col}@from-xlarge-bp'] {
        grid-column: span #{$col};
      }
    }
  }
}

grid-item {
  grid-column: span 1;
}

placeholder {
  display: block;
  width: 100%;
  height: 100%;
  padding: calc(var(--yspacing) / 2) var(--xspacing);
  color: white;
  border-radius: 1em;
  background-color: var(--placeholdercolor);
}

section {
  margin-top: calc(var(--yspacing) * 2);
  margin-bottom: calc(var(--yspacing) * 2);
  
  hr {
    margin-top: calc(var(--yspacing) / 2);
    margin-bottom: calc(var(--yspacing) / 2);
  }
}

              
            
!

JS

              
                
              
            
!
999px

Console