Skip to content

Instantly share code, notes, and snippets.

@karks88
Last active March 29, 2021 11:46
Show Gist options
  • Save karks88/917ef60b5ddeb3cee467449aed60f5fb to your computer and use it in GitHub Desktop.
Save karks88/917ef60b5ddeb3cee467449aed60f5fb to your computer and use it in GitHub Desktop.
Flexbox Grid with Fallbacks (requires Modernizr)
/* Flexbox Columns */
.flex-columns {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
}
.justify-flexstart {
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
}
.justify-center {
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
}
.justify-spacebetween {
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.align-items-flexstart {
-webkit-align-items: flex-start;
-ms-flex-align: start;
align-items: flex-start;
}
.align-items-center {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.align-items-stretch {
-webkit-align-items: stretch;
-ms-flex-align: stretch;
align-items: stretch;
}
/* align-content is for multiline columns */
.align-content-flexstart {
-webkit-align-content: flex-start;
-ms-flex-line-pack: start;
align-content: flex-start;
}
.align-content-center {
-webkit-align-content: center;
-ms-flex-line-pack: center;
align-content: center;
}
.align-content-spacebetween {
-webkit-align-content: space-between;
-ms-flex-line-pack: justify;
align-content: space-between;
}
/* Individual Column Width */
.flexfallback { } /* ALWAYS include this in each column, plus an individual size below. */
.flexcol-auto {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: auto;
}
.flexcol-half {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 48%;
}
.flexcol-third {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 31%;
}
.flexcol-two-thirds {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 66%;
}
.flexcol-fourth {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 23%;
}
.flexcol-three-fourths {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 75%;
}
.flexcol-fifth {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 18%;
}
/* No Flexbox? No Problem. */
.no-flexbox .flex-columns {
display: block;
clear: both;
}
.no-flexbox .flexfallback {
width:100%; margin-right:4%; position:relative; float:left;
}
.no-flexbox .flexfallback:last-child {
clear:right; margin-right:0;
}
.no-flexbox .flexcol-auto {
display: inline-block;
width: auto;
}
.no-flexbox .flexcol-half {
display: block;
width: 48%;
}
.no-flexbox .flexcol-third {
display: block;
width: 30.66%;
}
/* Revised Version that Adjusts for Visual Enhancements
.no-flexbox .flexcol-third {
display: block;
width: 28.66%;
} */
.no-flexbox .flexcol-two-thirds {
display: block;
width: 65.33%;
}
.no-flexbox .flexcol-fourth {
display: block;
width: 22%;
}
.no-flexbox .flexcol-three-fourths {
display: block;
width: 74%;
}
.no-flexbox .flexcol-fifth {
display: block;
width: 16.8%;
}
@media screen and (min-width: 320px) and (max-width: 768px) {
/* Flexbox Columns */
.flex-columns {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
/* Individual Column Width */
.flexcol-auto {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: auto;
}
.flexcol-half {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 100%;
}
.flexcol-third {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 100%;
}
.flexcol-two-thirds {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 100%;
}
.flexcol-fourth {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 100%;
}
.flexcol-three-fourths {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 100%;
}
.flexcol-fifth {
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: auto;
-ms-flex-item-align: auto;
align-self: auto;
width: 100%;
}
.flexfallback {
margin-bottom: 10px;
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Implementing CSS Fallbacks</title>
<link href="flexgrid.css" rel="stylesheet" type="text/css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
<style type="text/css">
body {
font-family: 'Open Sans',sans-serif;
font-size: 16px;
font-weight: normal;
line-height: 26px;
margin-top: 100px;
}
h2 {
font-size: 20px;
font-weight: bold;
line-height: 30px;
margin-bottom: 6px;
}
/* For demonstration purposes, we'll add some goodies to our layout. */
.flex-columns {
width: 1100px;
margin: 0 auto;
}
.flex-columns div {
border: 1px solid #CCC;
padding: 8px;
}
</style>
<script type="text/javascript" src="modernizr-custom.js"></script>
</head>
<body>
<div class="flex-columns justify-spacebetween align-items-stretch">
<div class="flexfallback flexcol-third">
<h2>Column 1</h2>
The brand illustration is an interesting ingredient that adds some life to the identity and that’s what I keep wanting to see more of.
</div>
<div class="flexfallback flexcol-third">
<h2>Column 2</h2>
Instead of trying to reconcile the globe with the new flying Formula 1 character, jkr boiled down the logo to one graphic action and used that to start the world.
</div>
<div class="flexfallback flexcol-third">
<h2>Column 3</h2>
In application, the identity introduces a dramatically different typeface, the oft-used Graphitu (which looks like the clownish ones), that is, well, graphic.
</div>
</div><!--Flex Columns-->
</body>
</html>
/*! modernizr 3.6.0 (Custom Build) | MIT *
* https://modernizr.com/download/?-flexbox-setclasses !*/
!function(e,n,t){function r(e,n){return typeof e===n}function o(){var e,n,t,o,s,i,l;for(var a in S)if(S.hasOwnProperty(a)){if(e=[],n=S[a],n.name&&(e.push(n.name.toLowerCase()),n.options&&n.options.aliases&&n.options.aliases.length))for(t=0;t<n.options.aliases.length;t++)e.push(n.options.aliases[t].toLowerCase());for(o=r(n.fn,"function")?n.fn():n.fn,s=0;s<e.length;s++)i=e[s],l=i.split("."),1===l.length?Modernizr[l[0]]=o:(!Modernizr[l[0]]||Modernizr[l[0]]instanceof Boolean||(Modernizr[l[0]]=new Boolean(Modernizr[l[0]])),Modernizr[l[0]][l[1]]=o),C.push((o?"":"no-")+l.join("-"))}}function s(e){var n=x.className,t=Modernizr._config.classPrefix||"";if(_&&(n=n.baseVal),Modernizr._config.enableJSClass){var r=new RegExp("(^|\\s)"+t+"no-js(\\s|$)");n=n.replace(r,"$1"+t+"js$2")}Modernizr._config.enableClasses&&(n+=" "+t+e.join(" "+t),_?x.className.baseVal=n:x.className=n)}function i(e,n){return!!~(""+e).indexOf(n)}function l(){return"function"!=typeof n.createElement?n.createElement(arguments[0]):_?n.createElementNS.call(n,"http://www.w3.org/2000/svg",arguments[0]):n.createElement.apply(n,arguments)}function a(e){return e.replace(/([a-z])-([a-z])/g,function(e,n,t){return n+t.toUpperCase()}).replace(/^-/,"")}function f(e,n){return function(){return e.apply(n,arguments)}}function u(e,n,t){var o;for(var s in e)if(e[s]in n)return t===!1?e[s]:(o=n[e[s]],r(o,"function")?f(o,t||n):o);return!1}function c(e){return e.replace(/([A-Z])/g,function(e,n){return"-"+n.toLowerCase()}).replace(/^ms-/,"-ms-")}function p(n,t,r){var o;if("getComputedStyle"in e){o=getComputedStyle.call(e,n,t);var s=e.console;if(null!==o)r&&(o=o.getPropertyValue(r));else if(s){var i=s.error?"error":"log";s[i].call(s,"getComputedStyle returning null, its possible modernizr test results are inaccurate")}}else o=!t&&n.currentStyle&&n.currentStyle[r];return o}function d(){var e=n.body;return e||(e=l(_?"svg":"body"),e.fake=!0),e}function m(e,t,r,o){var s,i,a,f,u="modernizr",c=l("div"),p=d();if(parseInt(r,10))for(;r--;)a=l("div"),a.id=o?o[r]:u+(r+1),c.appendChild(a);return s=l("style"),s.type="text/css",s.id="s"+u,(p.fake?p:c).appendChild(s),p.appendChild(c),s.styleSheet?s.styleSheet.cssText=e:s.appendChild(n.createTextNode(e)),c.id=u,p.fake&&(p.style.background="",p.style.overflow="hidden",f=x.style.overflow,x.style.overflow="hidden",x.appendChild(p)),i=t(c,e),p.fake?(p.parentNode.removeChild(p),x.style.overflow=f,x.offsetHeight):c.parentNode.removeChild(c),!!i}function y(n,r){var o=n.length;if("CSS"in e&&"supports"in e.CSS){for(;o--;)if(e.CSS.supports(c(n[o]),r))return!0;return!1}if("CSSSupportsRule"in e){for(var s=[];o--;)s.push("("+c(n[o])+":"+r+")");return s=s.join(" or "),m("@supports ("+s+") { #modernizr { position: absolute; } }",function(e){return"absolute"==p(e,null,"position")})}return t}function v(e,n,o,s){function f(){c&&(delete N.style,delete N.modElem)}if(s=r(s,"undefined")?!1:s,!r(o,"undefined")){var u=y(e,o);if(!r(u,"undefined"))return u}for(var c,p,d,m,v,g=["modernizr","tspan","samp"];!N.style&&g.length;)c=!0,N.modElem=l(g.shift()),N.style=N.modElem.style;for(d=e.length,p=0;d>p;p++)if(m=e[p],v=N.style[m],i(m,"-")&&(m=a(m)),N.style[m]!==t){if(s||r(o,"undefined"))return f(),"pfx"==n?m:!0;try{N.style[m]=o}catch(h){}if(N.style[m]!=v)return f(),"pfx"==n?m:!0}return f(),!1}function g(e,n,t,o,s){var i=e.charAt(0).toUpperCase()+e.slice(1),l=(e+" "+P.join(i+" ")+i).split(" ");return r(n,"string")||r(n,"undefined")?v(l,n,o,s):(l=(e+" "+z.join(i+" ")+i).split(" "),u(l,n,t))}function h(e,n,r){return g(e,t,t,n,r)}var C=[],S=[],w={_version:"3.6.0",_config:{classPrefix:"",enableClasses:!0,enableJSClass:!0,usePrefixes:!0},_q:[],on:function(e,n){var t=this;setTimeout(function(){n(t[e])},0)},addTest:function(e,n,t){S.push({name:e,fn:n,options:t})},addAsyncTest:function(e){S.push({name:null,fn:e})}},Modernizr=function(){};Modernizr.prototype=w,Modernizr=new Modernizr;var x=n.documentElement,_="svg"===x.nodeName.toLowerCase(),b="Moz O ms Webkit",P=w._config.usePrefixes?b.split(" "):[];w._cssomPrefixes=P;var z=w._config.usePrefixes?b.toLowerCase().split(" "):[];w._domPrefixes=z;var E={elem:l("modernizr")};Modernizr._q.push(function(){delete E.elem});var N={style:E.elem.style};Modernizr._q.unshift(function(){delete N.style}),w.testAllProps=g,w.testAllProps=h,Modernizr.addTest("flexbox",h("flexBasis","1px",!0)),o(),s(C),delete w.addTest,delete w.addAsyncTest;for(var T=0;T<Modernizr._q.length;T++)Modernizr._q[T]();e.Modernizr=Modernizr}(window,document);
@karks88
Copy link
Author

karks88 commented Mar 23, 2021

For fallbacks to work, a copy of the Modernizr script must be present with a detect for Flexbox: https://modernizr.com/download?flexbox-setclasses&q=flexbox

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