css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Heading level 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis eum, distinctio vero dignissimos eveniet accusamus, sit accusantium amet at ipsa ab culpa adipisci doloribus possimus officiis ea voluptatem debitis! Repellendus.</p>

<h2>Heading level 2</h2>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis eum, distinctio vero dignissimos eveniet accusamus, sit accusantium amet at ipsa ab culpa adipisci doloribus possimus officiis ea voluptatem debitis! Repellendus.</p>


<h3>Heading level 3</h3>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis eum, distinctio vero dignissimos eveniet accusamus, sit accusantium amet at ipsa ab culpa adipisci doloribus possimus officiis ea voluptatem debitis! Repellendus.</p>


<h4>Heading level 4</h4>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis eum, distinctio vero dignissimos eveniet accusamus, sit accusantium amet at ipsa ab culpa adipisci doloribus possimus officiis ea voluptatem debitis! Repellendus.</p>


<h5>Heading level 5</h5>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis eum, distinctio vero dignissimos eveniet accusamus, sit accusantium amet at ipsa ab culpa adipisci doloribus possimus officiis ea voluptatem debitis! Repellendus.</p>

            
          
!
            
              // I've seen a few examples recently of CSS variables and I thought 
// I'd do a quick demo of some good and bad ways to use them.


// I'm going to use modular scales as an example.
// I'm going to use a scale 1.2 for smalls screens and 1.33 for large: 

//     1.2           1.33
//  ___________________________
//  |  2.488rem 	|  4.209rem  |
//  |  2.074rem 	|  3.157rem  |
//  |  1.728rem 	|  2.369rem  |
//  |  1.44rem 	  |  1.777rem  |
//  |  1.2rem 	  |  1.333rem  |
//  |  1rem 	    |  1rem      |
//  ---------------------------

// This is a perfect situation to use CSS variables and the way 
// I've seen most people it is something like this:

:root {
	
	// scale for 1.2:
	--ms-small-1: 1em;
	--ms-small-2: 1.2em;
	--ms-small-3: 1.44rem;
	--ms-small-4: 1.728rem;
	--ms-small-5: 2.074rem;
	--ms-small-6: 2.488rem;
	
	// scale for 1.33:
	--ms-large-1: 1rem;     
	--ms-large-2: 1.333rem; 
	--ms-large-3: 1.777rem; 	
	--ms-large-4: 2.369rem; 
	--ms-large-5: 3.157rem;
	--ms-large-6: 4.209rem;
	
}

// It seems like a fairly logical start to define variables for different
// values in the scale. Next I'd expect to see this:

// Small scale:
h1 {
  font-size: var(--ms-small-6);
}
h2 {
  font-size: var(--ms-small-5);
}
h3 {
  font-size: var(--ms-small-4);
}
h4 {
  font-size: var(--ms-small-3);
}
h5 {
  font-size: var(--ms-small-2);
}
h6 {
  font-size: var(--ms-small-1);
}

// And then large scale above 800px

@media screen and (min-width: 800px) {
	h1 {
		font-size: var(--ms-large-6);
	}
	h2 {
		font-size: var(--ms-large-5);
	}
	h3 {
		font-size: var(--ms-large-4);
	}
	h4 {
		font-size: var(--ms-large-3);
	}
	h5 {
		font-size: var(--ms-large-2);
	}
	h6 {
		font-size: var(--ms-large-1);
	}
}

// This is now working pretty good! 
// It might seem like the most logical way to do this because this is
// exactly what I might do with Sass, but this is not taking advantage 
// of how CSS variables work.

// Let's try again, remembering that CSS variables are scoped to the DOM
// therefore subject to inheritance and the cascade. 


:root {
	
	// Rather than changing which variables we use, the value of the 
	// variable itself is going to change. So let's name them accordingly: 
	
	// scale for 1.2:
	--font-size-1: 1em;
	--font-size-2: 1.2em;
	--font-size-3: 1.44rem;
	--font-size-4: 1.728rem;
	--font-size-5: 2.074rem;
	--font-size-6: 2.488rem;

}

@media screen and (min-width: 800px) {
	:root {
			// scale for 1.33:
		--font-size-1: 1rem;     
		--font-size-2: 1.333rem; 
		--font-size-3: 1.777rem; 	
		--font-size-4: 2.369rem; 
		--font-size-5: 3.157rem;
		--font-size-6: 4.209rem;
	}
}

h1 {
	font-size: var(--font-size-6);
}
h2 {
	font-size: var(--font-size-5);
}
h3 {
	font-size: var(--font-size-4);
}
h4 {
	font-size: var(--font-size-3);
}
h5 {
	font-size: var(--font-size-2);
}
h6 {
	font-size: var(--font-size-1);
}

// This is the exact same result!!! 
// The difference is we're taking advantage of how CSS variables work
// so that we can store all of our responsive logic in the variable.
// The code is also far more readable.

// I can imagine most sites that use CSS variables well, will not have any
// media queries other than for changing values in CSS variables.


// In short if a media query swaps variables, there is a good chance
// you're doing it wrong. Change the values of variables not which 
// variables are used. This should also result in more samentic naming 
// conventions.







// Not related to demo

body {
	width: 90%;
	max-width: 700px;
}
            
          
!
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