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.
<!-- in a wrapping container include a div for the card and a button to refresh the contents of the card itself -->
<div class="container">
<!-- in the div.card include yet another container with the actual content of the display -->
<div class="card">
<!-- include a title, subtitle and text regarding a hypothetical blog post -->
<div class="card__content">
<h1 class="title">CSS Animation</h1>
<h3 class="subtitle">Animate HTML elements in your stylesheet</h3>
<p class="description">CSS allows to animate HTML elements through the animation property and the keyword @keyframes. The former describes the pace of the animation while...</p>
</div>
</div>
<!-- below the card, include a button to refresh the contents of the card -->
<button>give us another one</button>
</div>
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
/*
include css variables for the text included in pseudo elements and colors of the cards
adding these values as custom properties allows to update their values in JS, and immediately refresh the page
*/
:root {
--card: "♦ 6";
--suit: #DD5964;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
min-height: 100vh;
width: 100%;
font-family: 'Open Sans', sans-serif;
color: #123034;
background-color: #123034;
}
/* center the card and button in the screen, in a single column layout */
.container {
display: flex;
justify-content: center;
min-height: 100vh;
align-items: center;
flex-direction: column;
}
/* style for the button */
.container button {
border: 2px solid #f8f4f3;
padding: 1rem 2rem;
background: none;
text-transform: uppercase;
font-size: 1.05rem;
color: #f8f4f3;
/* transition to alter the button on hover and when it is pressed */
transition: all 0.2s ease;
}
.container button:hover {
color: #123034;
background: #f8f4f3;
}
.container button:active {
transform: scale(1.05);
}
/* style for the div.card */
.container .card {
margin: 0 1rem 2rem;
padding: 2.2rem 2.5rem;
max-width: 300px;
background: #f8f4f3;
border-radius: 12px;
/* transition to alter properties when the card is refreshed (in JS) */
transition: all 0.2s ease;
/* position relative to absolute position pseudo elements */
position: relative;
/* cursor pointer to redirect to the hypothetical article */
cursor: pointer;
}
.container .card:after,
.container .card:before {
position: absolute;
font-weight: bold;
font-size: 1.6rem;
padding: 5px;
/* include the color and content of the pseudo element as specified by the custom properties */
content: var(--card);
color: var(--suit);
}
/* position the pseudo elements at either end of the card */
.container .card:after {
top: 0;
left: 0;
}
.container .card:before {
bottom: 0;
right: 0;
}
.container .card .card__content h3 {
margin: 1rem 0 1.5rem;
line-height: 1.4;
}
.container .card .card__content p {
line-height: 2;
}
/* include a class to be added and remove to the card, as its content is refreshed */
.card--refresh {
transform: translateX(20px) rotate(2deg);
}
.card--refresh:after,
.card--refresh:before,
.card--refresh .card__content {
opacity: 0;
}
// include variables for the cards: the suit, value and color
const cardSuit = ["♦","♥","♣","♠"];
const cardValue = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];
const cardColor = ["#DD5964", "#483F50"];
// include an object for the hypothetical blog posts, with keys for the title, subtitle and short description
const blogPosts = {
title: [
"Markdown",
"Pseudo CSS",
"CSS Animation",
"CSS Variables",
"CSS Gradients",
"CSS Flex",
"CSS Grid",
"Gulp",
"Stylus"
],
subtitle: [
"Write readable docs with markdown syntax",
"Access another dimension of HTML elements",
"Animate HTML elements in your stylesheet",
"Custom properties to the resque",
"More choices than a single solid color",
"Easily position elements in rows and columns.",
"Display HTML elements with the help of a grid",
"Automate tedious and time-consuming tasks",
"Empower CSS syntax with a preprocessor"
],
description: [
"By saving a file with a markdown extension, or md, it is possible to access very useful features. With the inclusion of few commands, these features allow to write text which is equally read-able and easy to write and edit.",
"With every HTML element comes a pair of pseudo-element, namely :after and :before. These allow to include content directly with CSS, be it text or a CSS-crafted visual. A few examples may clear the advantage of this feature.",
"CSS allows to animate HTML elements through the animation property and the keyword @keyframes. The former describes the pace of the animation while the latter describes the behavior taken by the animated element.",
"If you have ever used CSS preprocessors, such as stylus or SASS, you might have enjoyed the inclusion of variables. In its latest version, CSS include the same construct, with the addition of markedly useful features.",
"The properties of background and background-image allow to include gradients. Be it linear, radial, of different colors, of the same hue, these provide plenty of opportunities when it comes to style",
"Flex properties allow to easily place HTML elements in the viewport of a webpage. By including the property of display set to flex, it is already possible to obtain a flex container, which itself nests flex items.",
"CSS grid provides the latest and most comprehensive set of properties to place HTML elements around the page. Simply adding the property of display and changing its value to grid allows to include grid containers and grid items.",
"Gulp is an extremely helpful task runner, which allows to automatically run tasks. Think of preprocessors like stylus or sass: Gulp can help to directly compile their syntax to CSS. Think of images: Gulp can optimize their size.",
"Preprocessors allow to take what good is achieved by CSS syntax and include mightily useful features, such as variables, mixins, functions and extra operators. This article tries to summarise some of these features, regarding the preprocessors Stylus."
]
};
// target the button which allows to refresh the contents of the card
const button = document.querySelector(".container button");
// listen for a click event on the button, at which point refresh the contents of the card
button.addEventListener("click", showNewBlogPost);
// target the HTML elements affected when the card is refreshed
// card, to be animated with the addition/removal of a class
const cardElement = document.querySelector(".container .card");
// title, subtitle, description, to be changed in text
const titleElement = document.querySelector(".container .card .card__content h1");
const subtitleElement = document.querySelector(".container .card .card__content h3");
const descriptionElement = document.querySelector(".container .card .card__content p");
// describe a function which refreshes the contents of the card
function showNewBlogPost() {
// create random numbers to access a random item in the arrays for the card suit, value and color
let randomSuit = Math.floor(Math.random()*cardSuit.length);
let randomNumber = Math.floor(Math.random()*cardValue.length);
let randomSuitColor = Math.floor(Math.random()*cardColor.length);
// change the custom properties to describe the item selected with the random number
// --card needs to nest values in between quotes, as to include the value for the content property
cardElement.style.setProperty("--card", `\"${cardSuit[randomSuit]} ${cardValue[randomNumber]}\"`);
cardElement.style.setProperty("--suit", cardColor[randomSuitColor]);
// create a random number to access a random (hypothetical) article
let randomBlogPost = Math.floor(Math.random()*blogPosts.title.length);
// create variables which store the title, subtitle, description of the randomly selected article
let title = blogPosts.title[randomBlogPost];
let subtitle = blogPosts.subtitle[randomBlogPost];
// include a subset of the description
let description = blogPosts.description[randomBlogPost].substr(0, 147) + "...";
// include in the selected HTML elements the values stored in the variables
titleElement.textContent = title;
subtitleElement.textContent = subtitle;
descriptionElement.textContent = description;
// add a class to the card element which animates the card as it is refreshed
// after 0.2s remove it
cardElement.classList.add("card--refresh");
let timeoutID = setTimeout(function() {
cardElement.classList.remove("card--refresh");
clearTimeout(timeoutID);
}, 200);
}
Also see: Tab Triggers