HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1 class="parallax">The ‘root element’ parallax technique</h1>
<img src="https://source.unsplash.com/category/nature/600x600" class="parallax skyline">
<img src="https://source.unsplash.com/category/buildings/600x600" class="parallax blimp">
<img src="https://source.unsplash.com/category/food/600x600" class="parallax gherkin">
<img src="https://source.unsplash.com/category/people/600x600" class="parallax dino">
<img src="https://source.unsplash.com/category/technology/600x600" class="parallax bull">
<div class="parallax box">
<p>By combining the power of <code>`rem`</code> and a small JavaScript function to manipulate the <code>`font-size`</code> of the <code>`html`</code> element, we can achieve simple but effective parallaxing.</p>
<p>Setting all our positions and parallax movements in CSS, this technique minimises DOM manipulations to just one – on the <code>`html`</code> element – boosting overall performance, although certainly not better than using 3D transforms.</p>
<p>The speed and direction of each element is set using margins (for this demo I've used <code>`margin-top`</code>). Play around with the numbers yourself to get a better understanding.</p>
<p>This is just a proof of concept and hasn't been fully tested, though it should work everywhere that supports <code>`rem`</code> (<a href="http://caniuse.com/#feat=rem">see here</a>). Using <code>`rem`</code> in this way does have its pitfalls – most notably the loss of its traditional usage – but it's a nice trick.</p>
</div>
<a href="https://codepen.io/amustill/full/aoFIm" target="_blank" class="parallax btn">View full screen</a>
/**
* Parallax styles
*
* All aesthetic styles are at the bottom for the benefit of better understanding the demo.
*/
html {
/* Give our document a fake height for demo purposes. */
height: 1500px;
/* We must set out root `font-size` to 0 to prevent our parallax margins being calculated */
font-size: 0;
}
body {
/* Reset the `font-size` to 16px/1em/100% */
font-size: 16px;
}
/* Fix our parallax elements in position */
.parallax {
position: fixed;
}
/**
* Position our elements
*
* Firstly we set our element position using `top`, `right`, `bottom`, `left`, then set your parallax movement using `margin` and `rem`.
*
* In this demo our 'base factor' is 50 (see JavaScript). Our 'factor' calculation never exceeds this number (when the viewport is fully scrolled), which means our `rem` calculations are based on this number.
*
* Example:
*
* If I set my `top` to `200px` and want this element to shift up by `400px`, our `rem` value is 400/10, or `-40em` (using negative margins to shift upwards).
*/
h1 {
top: 75px;
margin-top: -3rem;
z-index: 5;
}
.box {
top: -1400px;
/* Positive margin, so it appears from above */
margin-top: 29rem;
/* Center and pad */
left: 50%;
margin-left: -27%;
padding: 2%;
width: 50%;
}
.btn {
top: 5em;
right: -800px;
margin-right: 18.5rem;
}
.skyline {
top: 240px;
margin-top: -28rem;
left: 8%;
}
.blimp {
top: 320px;
margin-top: -55rem;
left: 24%;
}
.gherkin {
top: 200px;
margin-top: -20rem;
left: 40%;
z-index: 10;
}
.bull {
top: 360px;
margin-top: -47rem;
left: 56%;
}
.dino {
top: 260px;
margin-top: -33rem;
left: 72%;
}
/**
* Demo aesthetics
*/
html {
background: #233c38 url('https://goo.gl/5wJBu') 50% 50%;
background-attachment: fixed;
background-size: cover;
font-family: sans-serif;
font-weight: 300;
line-height: 1.5;
color: #fff;
}
a {
color: #fff;
text-decoration: none;
border-bottom: 1px dotted;
}
a:hover {
color: magenta;
}
h1 {
width: 100%;
font: 3em/1 'Oswald', sans-serif;
text-align: center;
text-transform: uppercase;
text-shadow: 3px 3px 20px rgba(0, 0, 0, 0.5);
}
p {
margin: 0 0 1.5em;
}
p:last-child {
margin-bottom: 0;
}
img {
width: 20%;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
code {
background-color: #222;
color: magenta;
}
.box {
background-color: rgba(255, 255, 255, 0.1);
}
.btn {
display: inline-block;
padding: 0.5em 1em;
background-color: rgba(0, 0, 0, 0.5);
border-bottom: 0;
}
// Variables
var viewport = $(window),
root = $('html'),
maxScroll;
// Bind events to window
viewport.on({
scroll: function() {
// Grab scroll position
var scrolled = viewport.scrollTop();
/**
* Calculate our factor, setting it as the root `font-size`.
*
* Our factor is calculated by multiplying the ratio of the page scrolled by our base factor. The higher the base factor, the larger the parallax effect.
*/
root.css({ fontSize: (scrolled / maxScroll) * 50 });
},
resize: function() {
// Calculate the maximum scroll position
maxScroll = root.height() - viewport.height();
}
}).trigger('resize');
Also see: Tab Triggers