Skip to content

Instantly share code, notes, and snippets.

@linebreaker
Created August 6, 2017 09:34
Show Gist options
  • Save linebreaker/f4979497f554d51c833fe630d055f757 to your computer and use it in GitHub Desktop.
Save linebreaker/f4979497f554d51c833fe630d055f757 to your computer and use it in GitHub Desktop.
Responsive Grid with Flexbox
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Flexbox responsive grid</title>
</head>
<body>
<div class="wrapper">
<p>Responsive grid with Flexbox</p>
<h1>Basic Grid</h1>
<p></p>
<div class="Grid Grid--full u-textCenter">
<div class="Grid-cell"><div class="Demo content-1of1"></div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-2 u-textCenter">
<div class="Grid-cell"><div class="Demo content-1of2"></div></div>
<div class="Grid-cell"><div class="Demo content-1of2"></div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-3 u-textCenter">
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-4 u-textCenter">
<div class="Grid-cell"><div class="Demo content-1of4"></div></div>
<div class="Grid-cell"><div class="Demo content-1of4"></div></div>
<div class="Grid-cell"><div class="Demo content-1of4"></div></div>
<div class="Grid-cell"><div class="Demo content-1of4"></div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-6 u-textCenter">
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-12 u-textCenter">
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
<div class="Grid-cell"><div class="Demo content-1of12"></div></div>
</div>
<hr />
<h1>Sidebar and main content</h1>
<div class="Grid Grid--gutters Grid--1of6">
<div class="Grid-cell"><div class="Demo content-1of6"></div></div>
<div class="Grid-cell"><div class="Demo">auto</div></div>
</div>
<div class="Grid Grid--gutters Grid--1of4">
<div class="Grid-cell"><div class="Demo content-1of4"></div></div>
<div class="Grid-cell"><div class="Demo">auto</div></div>
</div>
<div class="Grid Grid--gutters Grid--1of3">
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
<div class="Grid-cell"><div class="Demo">auto</div></div>
</div>
<hr />
<h1>Left / Center / Right content alignment</h1>
<div class="Grid Grid--gutters Grid--1of2">
<div class="Grid-cell"><div class="Demo content-1of2"></div></div>
</div>
<div class="Grid Grid--gutters Grid--1of2 Grid--center">
<div class="Grid-cell"><div class="Demo content-1of2"></div></div>
</div>
<div class="Grid Grid--gutters Grid--1of2 Grid--right">
<div class="Grid-cell"><div class="Demo content-1of2"></div></div>
</div>
<hr />
<h1>Nesting</h1>
<div class="Grid Grid--gutters Grid--1of3 Grid--nested">
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
<div class="Grid-cell"><div class="Demo">
<div class="Grid Grid--gutters Grid--1of3 Grid--nested">
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
<div class="Grid-cell"><div class="Demo">
<div class="Grid Grid--gutters Grid--1of3 Grid--nested">
<div class="Grid-cell"><div class="Demo content-1of3"></div></div>
<div class="Grid-cell"><div class="Demo">auto</div></div>
</div>
</div></div>
</div>
</div>
</div>
</div>
<hr />
<h1>Vertical alignment</h1>
<div class="Grid Grid--gutters Grid--cols-3 Grid--top">
<div class="Grid-cell"><div class="Demo">This cell should be <strong>top-aligned</strong>.</div></div>
<div class="Grid-cell"><div class="Demo">Vice keytar actually DIY Thundercats. Flannel heirloom lo-fi Blue Bottle Intelligentsia pour-over Etsy church-key banh mi Marfa lomo yr messenger bag.Artisan try-hard food truck +1 Cosby sweater.</div></div>
<div class="Grid-cell"><div class="Demo">Letterpress YOLO Austin Pinterest Helvetica. Cliche pug literally, church-key 90's twee you probably haven't heard of them meh fixie cornhole banh mi pork belly mustache. Stumptown bespoke Vice, bitters sustainable hoodie Echo Park letterpress 8-bit pug. Quinoa meh flexitarian roof party, actually selvage Carles put a bird on it umami vegan ethical. </div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-3 Grid--center">
<div class="Grid-cell"><div class="Demo">This cell should be <strong>center-aligned</strong>.</div></div>
<div class="Grid-cell"><div class="Demo">Vice keytar actually DIY Thundercats. Flannel heirloom lo-fi Blue Bottle Intelligentsia pour-over Etsy church-key banh mi Marfa lomo yr messenger bag.Artisan try-hard food truck +1 Cosby sweater.</div></div>
<div class="Grid-cell"><div class="Demo">Letterpress YOLO Austin Pinterest Helvetica. Cliche pug literally, church-key 90's twee you probably haven't heard of them meh fixie cornhole banh mi pork belly mustache. Stumptown bespoke Vice, bitters sustainable hoodie Echo Park letterpress 8-bit pug. Quinoa meh flexitarian roof party, actually selvage Carles put a bird on it umami vegan ethical. </div></div>
</div>
<div class="Grid Grid--gutters Grid--cols-3 Grid--bottom">
<div class="Grid-cell"><div class="Demo">This cell should be <strong>bottom-aligned</strong>.</div></div>
<div class="Grid-cell"><div class="Demo">Vice keytar actually DIY Thundercats. Flannel heirloom lo-fi Blue Bottle Intelligentsia pour-over Etsy church-key banh mi Marfa lomo yr messenger bag.Artisan try-hard food truck +1 Cosby sweater.</div></div>
<div class="Grid-cell"><div class="Demo">Letterpress YOLO Austin Pinterest Helvetica. Cliche pug literally, church-key 90's twee you probably haven't heard of them meh fixie cornhole banh mi pork belly mustache. Stumptown bespoke Vice, bitters sustainable hoodie Echo Park letterpress 8-bit pug. Quinoa meh flexitarian roof party, actually selvage Carles put a bird on it umami vegan ethical. </div></div>
</div>
<hr />
<h1>Holly Grail page layout</h1>
<p>Note: in responsive view Aside 1 and Aside 2 elements stack below the Main content.</p>
<div class="Grid Grid--full Grid--nested header">
<div class="Grid-cell"><div class="Demo Holly">
<img src="http://placehold.it/100x50/CC99CC/ffffff&text=Logo" style="vertical-align: middle;"><strong> Header / Logo</strong>
</div></div>
</div>
<div class="Grid Grid--full">
<div class="Grid-cell">
<ul class="navigation">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<div class="Grid Grid--gutters Grid--holly-grail">
<div class="Grid-cell main">
<div class="Demo Holly">
<strong>Main</strong>
<p>Salvia Helvetica ethical lo-fi, artisan messenger bag put a bird on it. Photo booth taxidermy kale chips, Carles Thundercats retro blog paleo. 8-bit four loko jean shorts, church-key tattooed bespoke normcore art party salvia organic vinyl letterpress distillery. You probably haven't heard of them tofu food truck, Echo Park salvia Schlitz heirloom scenester ennui biodiesel Intelligentsia +1 church-key. Bitters meh actually quinoa, YOLO pug authentic. Kitsch health goth paleo, pop-up tofu authentic gluten-free whatever yr Brooklyn irony. Banjo Tumblr you probably haven't heard of them literally aesthetic, wolf put a bird on it Kickstarter yr.</p>
</div>
</div>
<div class="Grid-cell aside aside-1">
<div class="Demo Holly"><strong>Aside 1</strong><br />
<div class="img-placeholder"></div>
Chocolate cake fruitcake icing muffin applicake chocolate.
</div>
</div>
<div class="Grid-cell aside aside-2">
<div class="Demo Holly">
<strong>Aside 2</strong>
<p>Hashtag deep v PBR, chillwave banjo mlkshk readymade fixie crucifix. Tattooed Tumblr direct trade trust fund swag gluten-free.</p>
</div>
</div>
</div>
<div class="Grid Grid--full">
<div class="Grid-cell"><div class="Demo Holly"><strong>Footer</strong><br /> Copyright &copy; Jelly-o apple pie pie chocolatebar.com</div></div>
</div>
<hr />
<p>Some ideas are borrowed from Phillip Walton's Solved by Flexbox project. </p>
<br />
</div>
</body>
</html>

Responsive Grid with Flexbox

This grid is based on Flexbox element. Fully responsive for desktop, tablet and mobile viewports. Featuring basic grid, sidebar, alignment and nesting, as well as the Holly Grail layout. Some ideas are borrowed from Phillip Walton's Solved by Flexbox project.

A Pen by gdg on CodePen.

License.

@import "compass/css3";
// VARIALBLES
// Demo colors:
$demoMain: rgba(51, 153, 204,.2);
$demoMainDark: rgba(51, 153, 204,.6);
$demoBorder: #33cccc;
$demoHolly: rgba(102, 51, 255, 0.1);
$demoHollyDark: rgba(102, 51, 255, 0.25);
body {
color: #404040;
font: 100 1em/150% "proxima-nova", Helvetica, sans-serif;
}
.wrapper{
max-width: 1200px;
margin: auto;
//border: 1px solid red;
}
h1, h2, h3, h4 {
font-weight: 900;
color: #333;
margin: 1.5em 0 0.5em;
}
h1 {font-size: 2em;}
h2 {font-size: 1.8em;}
h3 {font-size: 1.5em;}
h4 {font-size: 1.3em;}
strong {font-weight: 600;}
hr {
border: none;
height: 1px;
background-color: $demoMain;
}
.img-placeholder {
background-image: url("http://placehold.it/200x100/CC99CC/ffffff&text=Feature");
background-size: cover;
min-height: 100px; // need to specify min-height and min-width
min-width: 100px;
}
/*Basic Grid Styles*/
.Grid {
display: flex;
flex-flow: row;
flex-wrap: wrap;
}
.u-textCenter {
text-align: center;
}
.Grid-cell {
flex: 1;
}
.Demo {
padding: .8em 1em 0;
margin-bottom: 1em;
background: $demoMain;
transition: background-color 0.3s ease;
border: 1px solid $demoBorder;
border-radius: 3px;
&:after {
content: "";
display: block;
margin-top: .8em;
height: 1px;
}
&:hover {
background: $demoMainDark;
}
}
.Demo.Holly {
background: $demoHolly;
&:hover {
background: $demoHollyDark;
}
}
// Grid Style Modifiers
/* With gutters*/
$gutter: 1em;
.Grid--gutters {
margin-left: -$gutter;
.Grid-cell {
padding-left: $gutter;
}
.Grid--nested {
.Grid-cell:first-of-type {
.Demo {
margin-right: 1em;
}
}
}
}
/* Justify per row*/
.Grid--right {
justify-content: flex-end;
}
.Grid--center {
justify-content: center;
}
/* Alignment per row */
.Grid--top {
align-items: flex-start;
}
.Grid--bottom {
align-items: flex-end;
}
.Grid--center {
align-items: center;
}
/* Alignment per cell */
.Grid-cell--top {
align-self: flex-start;
}
.Grid-cell--bottom {
align-self: flex-end;
}
.Grid-cell--center {
align-self: center;
}
// Menu
.navigation {
list-style: none;
/*background: deepskyblue;*/
background: $demoHolly;
margin: 0 0 1em;
border: 1px solid $demoBorder;
border-radius: 3px;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: flex-end;
a {
text-decoration: none;
display: block;
padding: 1em;
color: #333;
&:hover {
background: darken($demoHolly, 10%);
border-radius:3px;
}
}
&:hover {
background: $demoHollyDark;
}
}
@media all and (max-width: 800px) {
.navigation {
justify-content: space-around;
}
}
@media all and (max-width: 600px) {
.navigation {
-webkit-flex-flow: column wrap;
flex-flow: column wrap;
padding: 0;
a {
text-align: center;
padding: 10px;
border-top: 1px solid rgba(255,255,255,0.3);
border-bottom: 1px solid rgba(0,0,0,0.1);
}
li:last-of-type a {
border-bottom: none;
}
}
}
/*===========================================*/
/* Base classes for all media - Mobile first */
.Grid--cols-2 > .Grid-cell {
flex: 0 0 100%;
}
.Grid--cols-3 > .Grid-cell {
flex: 0 0 100%;
}
.Grid--cols-4 > .Grid-cell {
flex: 0 0 100%;
}
.Grid--cols-6 > .Grid-cell {
flex: 0 0 calc(50% - #{$gutter});
}
.Grid--cols-12 > .Grid-cell {
flex: 0 0 calc(33.3333% - #{$gutter});
}
.Grid--holly-grail {
.aside, .main {
flex: 1 100%;
}
}
/* One of -- columns*/
.Grid--1of2 > .Grid-cell,
.Grid--1of4 > .Grid-cell:first-of-type,
.Grid--1of3 > .Grid-cell:first-of-type{
flex: 0 0 100%;
}
.Grid--1of6 > .Grid-cell:first-of-type {
flex: 0 0 50%;
}
.Grid--fit > .Grid-cell {
flex: 1;
}
.Grid--full > .Grid-cell {
flex: 0 0 100%;
}
/* Tablet (medium) screens */
@media (min-width: 30em) {
.Grid--cols-4 > .Grid-cell {
flex: 0 0 calc(50% - #{$gutter});
}
.Grid--cols-6 > .Grid-cell {
flex: 0 0 calc(33.3333% - #{$gutter});
}
.Grid--cols-12 > .Grid-cell {
flex: 0 0 calc(16.6666% - #{$gutter});
}
.Grid--holly-grail {
.aside {
flex: 1 calc(25% - #{$gutter});
}
}
.Grid--1of2 > .Grid-cell {
flex: 0 0 50%;
}
.Grid--1of6 > .Grid-cell:first-of-type {
flex: 0 0 30%;
}
.Grid--1of4 > .Grid-cell:first-of-type {
flex: 0 0 50%;
}
.Grid--1of3 > .Grid-cell:first-of-type {
flex: 0 0 100%;
}
}
/* Large screens */
@media (min-width: 48em) {
.Grid--cols-2 > .Grid-cell,
.Grid--cols-3 > .Grid-cell,
.Grid--cols-4 > .Grid-cell,
.Grid--cols-6 > .Grid-cell,
.Grid--cols-12 > .Grid-cell{
flex: 1;
}
.Grid--holly-grail {
.main { flex: 2; }
.aside { flex: 1; }
.aside-1 { order: 1; }
.main { order: 2; }
.aside-2 { order: 3; }
}
.Grid--1of2 > .Grid-cell {
flex: 0 0 50%;
}
.Grid--1of6 > .Grid-cell:first-of-type {
flex: 0 0 16.6666%;
}
.Grid--1of4 > .Grid-cell:first-of-type {
flex: 0 0 25%;
}
.Grid--1of3 > .Grid-cell:first-of-type {
flex: 0 0 30%;
}
.Grid--gutters.Grid--nested {
.Grid-cell:first-of-type {
.Demo {
margin-right: 0;
}
}
}
}
// Contents classes
.content-1of1::before { content: "1"; }
.content-1of2::before { content: "1/2"; }
.content-1of3::before { content: "1/3"; }
.content-1of4::before { content: "1/4"; }
.content-1of6::before { content: "1/6"; }
.content-1of12::before { content: "1/12"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment