cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <div class="u-wrapper">
	<div class="row">
		<div class="u-width--full">
			<h1>CSS Grid with variables</h1>
			<p>
				<strong>Short version:</strong> Instead of precompiling math for our grids we can do it live in the browser using CSS variables, thus saving us a LOT of lines in CSS (...in the future. I'm looking at you Explorer)
			</p>
		</div>
	</div>
	<div class="row">
		<div class="u-width--full">
			<p>Here are two elements, lets pretend they're a sidebar and an article element</p>
		</div>
		<div class="u-width sidebar">
			<div class="demo"></div>
		</div>
		<div class="u-width content">
			<div class="demo"></div>
		</div>
		<div class="u-width--full">
			<p>No grid classes classes like <code>col-2</code> or <code>grid-12_lg-8_md-6_sm-4_xs-2</code>. Instead we're using <strong>a variable on each element.</strong> Here's whats going on:</p>
		</div>
	</div>
	<div class="row">
		<div class="u-width--full">

			<p>
				First off we need to define number of columns and the width of our grid gutters.
			</p>
			<pre>
:root{
    --column: 8;
    --gutters: 10;
}
</pre>
			<p>
				For our own sanity we'll use flexbox.
			</p>

			<pre>
.row{
    display: flex;
    flex-wrap: wrap;
    margin: 0 calc(var(--gutter) * -1px) ;
}
</pre>
			<p>
				Flex defaults to 100% width, same as display: block. The negative margins works kind of like calc(100% + 20px) in addition to margin-left: -10px. Just neater. They're crucial to helps us offset the gutters coming from the columns.
			</p>
			<h3>
     The magic ensues.
</h3>

			<p>We'll make a wildcard selector for our necessary helper class. On its own it doesn't really do much.</p>
			<pre>
[class*="u-width"]{
    padding-right: calc(var(--gutter) * 1px);
    padding-left: calc(var(--gutter) * 1px);
    flex-basis: calc((100% / var(--columns)) * var(--width));
}
</pre>
			<p>
				This selector will kinda work as a function for the most part. Padding for gutters and flex-basis for width. The interesting part here is --width. Because of this variable we can set it on any element that matches our wildcard selector. An example:
			</p>

			<pre>
&lt;div class=&quot;u-width sidebar&quot;&gt; ...  &lt;/div&gt;
</pre>

			<p>
				If we needed sidebar to be 2 columns wide we can do it simply by:
			</p>
			<pre>
.sidebar{ --width: 2; }
.content { --width: 4; }
</pre>
			<p>
				This works because variables in CSS are inherited and can be used as a scope. In other words, we just passed the --width to the u-width selector and calc takes care of the rest. Boom. Mic dropped. 14 lines of future vanilla CSS.
			</p>

			<p>
				In addtion, you could extend this to be more like any other stand alone grid, but still with a lot less markup.
			</p>

		</div>
	</div>
	
	<div class="row">
		<div class="u-width--full">
			<h3>Extending the Grid</h3>
		</div>
		<div class="u-width m--2 l--4">
			<div class="demo"></div>
		</div>
		<div class="u-width m--2">
			<div class="demo"></div>
		</div>
		<div class="u-width m--2">
			<div class="demo"></div>
		</div>
		<div class="u-width m--2">
			<div class="demo"></div>
		</div>
		<div class="u-width m--4 l--2">
			<div class="demo"></div>
		</div>
		<div class="u-width m--4 l--4">
			<div class="demo"></div>
		</div>
		<div class="u-width m--6">
			<div class="demo"></div>
		</div>
		<div class="u-width m--2">
			<div class="demo"></div>
		</div>
		<div class="u-width--full">
			<p>Here we've got a few elements with classes that define their width. I've used <code>
				&lt;div class=&quot;u-width m--4 l--4&quot;&gt; ...  &lt;/div&gt;
</code>, but it's really up to you. This is how they're setup: </p>
			<pre>
.m--2{ --width: 2; }
.m--4{ --width: 4; }
.m--6{ --width: 6; }

.l--2{ --width: 2; }
.l--4{ --width: 4; }
.l--6{ --width: 6; }
/* media query are implied */
</pre>
			<p>And that's it really.</p>
		</div>
	</div>

</div>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Roboto:400,700');

:root{
	--color: #0C3934;
	--bg: #F8EBEE;
	
	/* Grid */
	--gutter: 10px;
	--columns: 8;
}

.row{
	display: flex;
	flex-wrap: wrap;
	margin: 0 calc(var(--gutter) - ( var(--gutter) * 2) );
}

[class*="u-width"]{ 
	padding-right: calc(var(--gutter));
	padding-left: calc(var(--gutter));
	flex-basis: calc((100% / var(--columns)) * var(--column-width)); 
}

/* --- Example selectors -- */
/* Not necessary for the grid to work */

.sidebar{
	--column-width: 2;
}

.content{
	--column-width: 6;
}


.u-width--full{
	--column-width: var(--columns);
}


/* --- Extending the grid -- */

.m--2{
	--column-width: 2;
}

.m--4{
	--column-width: 4;
}

.m--6{
	--column-width: 6;
}

@media (min-width: 900px ){
	.l--2{
		--column-width: 2;
	}	
	.l--4{
		--column-width: 4;
	}	
	.l--6{
		--column-width: 6;
	}	
}

/* --- Presentational stuff. -- */

body{
	background-color: var(--bg);
	font: normal 1em/1.5 "Roboto", sans-serif
}
*{
	box-sizing: border-box;
}
.u-wrapper{
	max-width: 600px;
	margin: 3em auto;
	padding: 10px;
}

.demo{
	display: block;
	padding-bottom: 3em;
	background-color: var(--color);
	margin-bottom: 20px;
}

pre, code{
	background-color: rgba(12, 57, 52, 0.1);
	color: rgb(32, 34, 33);
}
code{
	padding: 2px 5px;
}
pre{
	padding: 1em;
	white-space: pre-wrap;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console