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.
<div id="cbwrap">
<!-- (X) TITLE -->
<h1 id="cbtitle">Javascript Typewriter</h1>
<!-- (A) THE PROPER DEMO -->
<div id="demo"></div>
Theme: <select id="theme">
<option value="">None</option>
<option value="retro">Retro</option>
<option value="impact">Impact</option>
<option value="rose">Rose</option>
<option value="code">Code</option>
<option value="banana">Banana</option>
</select>
<!-- (X) INFO SNIPPET -->
<div id="cbinfo">
Visit <a href="https://code-boxx.com/simple-javascript-typewriter-effect/" target="_blank">Code Boxx</a> for more details.
</div>
</div>
/* (A) BLINKING CURSOR EFFECT */
@keyframes blink {
0% { opacity: 0; }
100% { opacity: 1; }
}
.cursor::after {
content: "|";
font-size: 1.5em;
font-weight: bold;
animation: blink infinite alternate 0.4s;
}
/* (B) "THEMES" */
.retro {
font-family: Courier, monospace;
font-weight: 700;
color: #333;
background: #d7ffe7;
padding: 5px;
}
.impact {
font-family: Impact, sans-serif;
text-transform: uppercase;
color: #fff;
background: #993535;
padding: 5px;
}
.rose {
font-family: 'Brush Script MT', cursive;
color: #a52e2e;
background: #ffeeee;
padding: 5px;
}
.code {
font-family: "Lucida Console", monospace;
color: #0dff00;
background: #141414;
padding: 5px;
}
.banana {
font-family: "Arial Black",sans-serif;
color: #83700e;
background: #f9ff00;
padding: 5px;
}
/* (X) DOES NOT MATTER */
/* (X1) LAYOUT COSMETICS */
* { box-sizing: border-box; }
body {
font-family: arial, sans-serif;
padding: 0; margin: 0; border: 0;
min-height: 100vh;
display: flex; justify-content: center; align-items: center;
background: url(https://images.unsplash.com/photo-1520096021341-b6742a955b1f?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODg3OTQ2OTl8&ixlib=rb-4.0.3&q=85);
background-size: cover;
background-position: center;
backdrop-filter: blur(10px);
}
#cbwrap {
background: rgba(255, 255, 255, 0.9);
width: 600px; padding: 20px;
border-radius: 10px;
}
#cbtitle {
margin-bottom: 40px;
text-transform: uppercase;
}
#cbinfo {
padding: 10px; margin-top: 40px;
font-weight: 700; text-align: center;
}
#cbinfo a {
text-decoration: none; padding: 5px;
color: #fff; background: #a91616;
}
/* (X2) FORM COSMETICS */
select {
padding: 5px;
margin-top: 10px;
}
function tw (instance) {
// (A) SET DEFAULT OPTIONS
if (instance.forward === undefined) { instance.forward = 100; }
if (instance.backward === undefined) { instance.backward = 50; }
if (instance.pause === undefined) { instance.pause = 1000; }
if (instance.loop === undefined) { instance.loop = true; }
if (instance.cursor === undefined) { instance.cursor = true; }
if (typeof instance.text != "object") { instance.text = [instance.text]; }
// (B) PROPERTIES & FLAGS
instance.current = 0; // current block of text
instance.char = 0; // current character
instance.direction = true; // true forward, false backward
instance.draw = true; // continue to "type text"?
// (C) ATTACH FAKE CURSOR
if (instance.cursor) { instance.target.classList.add("cursor"); }
// (D) TYPEWRITER EFFECT
instance.typist = () => {
// (D1) NEXT CHARACTER
if (instance.direction) {
instance.char++;
instance.draw = instance.char <= instance.text[instance.current].length;
} else {
instance.char--;
instance.draw = instance.char >= 0;
}
// (D2) DRAW HTML
if (instance.draw) {
instance.target.innerHTML = instance.char==0 ? " " : instance.text[instance.current].substring(0, instance.char);
}
// (D3) PAUSE & LOOP?
else {
// (D3-1) CLEAR TIMER + REVERSE DIRECTION
clearInterval(instance.timer);
instance.direction = !instance.direction;
// (D3-2) NEXT BLOCK OF TEXT
if (instance.direction) {
instance.current++;
if (instance.loop && instance.current == instance.text.length) {
instance.current = 0;
}
if (instance.current <= instance.text.length) {
instance.timer = setTimeout(() => {
instance.timer = setInterval(instance.typist, instance.forward);
}, instance.pause);
}
}
// (D3-3) PAUSE THEN CLEAR TEXT
else {
instance.timer = setTimeout(() => {
instance.timer = setInterval(instance.typist, instance.backward);
}, instance.pause);
}
}
};
// (E) START
instance.timer = setInterval(instance.typist, instance.forward);
}
// (F) ATTACH TYPEWRITER & THEME SWITCHER
window.onload = () => {
// (F1) GET DEMO <DIV>
var demo = document.getElementById("demo");
// (F2) THEME SWITCHER
document.getElementById("theme").onchange = function () {
let cur = demo.classList.contains("cursor");
demo.className = this.value;
if (cur) { demo.classList.add("cursor"); }
};
// (F3) ATTACH TYPEWRITER
tw({
// (F3-1) REQUIRED
target : demo,
text : [
"Wow. Much text. Very paragraph. Such contents.",
"Foo! It works!",
"Another random paragraph here."
],
// (F3-2) OPTIONAL
forward : 80, // delay between each character, default 100 ms
backward : 40, // delay between each character, default 50 ms
pause : 1500, // pause before next block of text, default 1 sec
loop : true, // loop text blocks, default true
cursor : true // add fake cursor, default true
});
};
Also see: Tab Triggers