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.
<section class="centered">
<h1>500 Server Error</h1>
<div class="container">
<span class="message" id="js-whoops"></span> <span class="message" id="js-appears"></span> <span class="message" id="js-error"></span> <span class="message" id="js-apology"></span>
<div><span class="hidden" id="js-hidden">Message Here</span></div>
</div>
</section>
@import url('https://fonts.googleapis.com/css?family=Lato|Roboto+Slab');
* {
position: relative;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.centered {
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1 {
margin-bottom: 50px;
font-family: 'Lato', sans-serif;
font-size: 50px;
}
.message {
display: inline-block;
line-height: 1.2;
transition: line-height .2s, width .2s;
overflow: hidden;
}
.message,
.hidden {
font-family: 'Roboto Slab', serif;
font-size: 18px;
}
.hidden {
color: #FFF;
}
// Here are the different messages we'll use for creating the 500 displayable message
const messages = [
['Whoops.', 'Oops.', 'Excuse me.', 'Oh Dear.', 'Well poo.', 'Hm...', 'This is awkward.', 'Well gosh!'],
['It appears', 'Looks like', 'Unfortunately,', 'It just so happens', 'Sadly,', 'Seemingly from nowhere'],
['there was an error.', 'we goofed up.', 'a bad thing happend.', 'the server crashed.', 'a bug appeared.', 'someone did a naughty.', 'pixies got into the server!', 'the server threw a tantrum.', 'the website had a bad day.', 'our code pooped out.'],
['Sorry.', 'Apologies.', 'Our bad.', 'Sad day.', 'We are quite contrite.', 'Beg pardon.']
];
// These are the different elements we'll be populating. They are in the same order as the messages array
const messageElements = [
document.querySelector('#js-whoops'),
document.querySelector('#js-appears'),
document.querySelector('#js-error'),
document.querySelector('#js-apology')
];
// we'll use this element for width calculations
const widthElement = document.querySelector('#js-hidden');
// keeping track of the message we just displayed last
let lastMessageType = -1;
// How often the page should swap messages
let messageTimer = 4000;
// on document load, setup the initial messages AND set a timer for setting messages
document.addEventListener('DOMContentLoaded', (event) => {
setupMessages();
setInterval(() => {
swapMessage();
}, messageTimer);
});
// Get initial messages for each message element
function setupMessages() {
messageElements.forEach((element, index) => {
let newMessage = getNewMessage(index);
element.innerText = newMessage;
});
}
// set the width of a given element to match its text's width
function calculateWidth(element, message) {
// use our dummy hidden element to get the text's width. Then use that to set the real element's width
widthElement.innerText = message;
let newWidth = widthElement.getBoundingClientRect().width;
element.style.width = `${newWidth}px`;
}
// swap a message for one of the message types
function swapMessage() {
let toSwapIndex = getNewSwapIndex();
let newMessage = getNewMessage(toSwapIndex);
// Animate the disappearing, setting width, and reappearing
messageElements[toSwapIndex].style.lineHeight = '0';
// once line height is done transitioning, set element width & message
setTimeout(() => {
// make sure the element has a width set for transitions
checkWidthSet(toSwapIndex, messageElements[toSwapIndex].innerText);
// set the new text
messageElements[toSwapIndex].innerText = newMessage;
// set the new width
calculateWidth(messageElements[toSwapIndex], newMessage);
}, 200);
// once width is done, transition the lineheight back to 1 so we can view the message
setTimeout(() => {
messageElements[toSwapIndex].style.lineHeight = '1.2';
}, 400);
}
// We need to make sure that the element at the passed index has a width set so we can use transitions
function checkWidthSet(index, message) {
if (false == messageElements[index].style.width) {
messageElements[index].style.width = `${messageElements[index].clientWidth}px`;
}
}
// Return a new index to swap message in. Should not be the same as the last message type swapped
function getNewSwapIndex() {
let newMessageIndex = Math.floor(Math.random() * messages.length);
while (lastMessageType == newMessageIndex) {
newMessageIndex = Math.floor(Math.random() * messages.length);
}
return newMessageIndex;
}
// Get a new message for the message element.
function getNewMessage(toSwapIndex) {
const messagesArray = messages[toSwapIndex];
const previousMessage = messageElements[toSwapIndex].innerText;
// Get a new random index and the message at that index
let newMessageIndex = Math.floor(Math.random() * messagesArray.length);
let newMessage = messagesArray[newMessageIndex];
// let's make sure they aren't the same as the message already there
while (newMessage == previousMessage) {
newMessageIndex = Math.floor(Math.random() * messagesArray.length);
newMessage = messagesArray[newMessageIndex];
}
return newMessage;
}
Also see: Tab Triggers