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.
<header>
<h1>Why loops, functions, objects, and classes?</h1>
<hr>
</header>
<main>
<h2>The life lines:</h2>
<div class="lifeline-container">
<div id="ada">
<h3>Ada</h3>
</div>
<div id="grace">
<h3>Grace</h3>
</div>
<div id="jane">
<h3>Jane</h3>
</div>
</div>
<button id="advance-one-year">+ 1 year</button>
<button id="advance-ten-years">+ 10 years</button>
<button id="start-counting">count yerself!</button>
<button id="stop-counting">stop it!</button>
</main>
<script src="main.js"></script>
html {
background-color: #222;
color: #ccc;
}
body {
max-width: 1200px;
margin: 1em auto;
padding: 2em;
border: 1px dotted gray;
border-radius: 10px;
}
header {
text-align: center;
}
.lifeline-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 1fr;
}
.lifeline-container div {
padding: 0 1em;
}
.lifeline-container div:nth-child(1) {
border-left: 3px solid rgb(199, 21, 190);
}
.lifeline-container div:nth-child(2) {
border-left: 3px solid rgb(22, 160, 160);
}
.lifeline-container div:nth-child(3) {
border-left: 3px solid rgb(132, 24, 219);
}
button {
font-size: 2em;
margin-top: 1em;
margin-right: 0.5em;
}
/*
This code was incrementally developed, refactored and improved, starting
out with a long list of spaghetti code. To see the whole progression, which
was intended to elaborate on why using loops, functions, and objects makes
a lot of sense, check out the accompanying recordings on
https://tantemalkah.at/artful-coding/2022wt/#xtra
*/
const btnAdvanceOneYear = document.getElementById('advance-one-year')
const btnAdvanceTenYears = document.getElementById('advance-ten-years')
const btnStart = document.getElementById('start-counting')
const btnStop = document.getElementById('stop-counting')
const ages = {
ada: -1,
grace: -1,
jane: -1,
lifelines: {
ada: document.getElementById('ada'),
grace: document.getElementById('grace'),
jane: document.getElementById('jane'),
}
}
function increasePersonAge (person) {
let message = ''
ages[person] = ages[person] + 1
if (person === 'ada') {
if (ages[person] === 0) { message = 'Hey there, nice to be here!' }
else if (ages[person] === 8) { message = 'Lord Byron, Ada\'s father dies' }
else if (ages[person] === 13) { message = 'Designed a flying horse' }
else if (ages[person] === 14) { message = 'Darn! Got the measles!' }
else if (ages[person] === 18) { message = 'Ada meets Charles Babbage' }
else if (ages[person] === 21) { message = 'Ada\'s first child was born' }
else if (ages[person] === 23) { message = 'Ada become a Countess, of Lovelace' }
else if (ages[person] === 28) { message = 'Created translations and notes on the analytical engine (contained the firs algorithms/programmes)' }
else if (ages[person] === 36) { message = 'Ada dies.' }
} else if (person === 'grace') {
if (ages[person] === 0) { message = 'Yeah, what adventure awaits?' }
else if (ages[person] === 19) { message = 'Begins to study maths and physics.'}
else if (ages[person] === 22) { message = 'Moves to Yale fo a master in maths.'}
else if (ages[person] === 24) { message = 'Master in Mathematics'}
else if (ages[person] === 28) { message = 'PhD in Mathematics'}
else if (ages[person] === 37) { message = 'Joins the Navy.'}
else if (ages[person] === 38) { message = 'Becomes lieutenant, assigned to the Bureau of Ordnance Project at Harvard, where she learns to program a Mark I computer.'}
else if (ages[person] === 46) { message = 'Creates the first compiler for computer languages.'}
else if (ages[person] === 60) { message = 'Retires from the Naval Reserve, but stays in the Navy to tackle standardising communication between different computer languages.'}
else if (ages[person] === 79) { message = 'Retires from the Navy as rear admiral and oldest serving.'}
else if (ages[person] === 84) { message = 'Receives the National Medal of Technology (as the first female individual to do so ... in 1991 btw.).'}
else if (ages[person] === 85) { message = 'Grace dies.'}
} else if (person === 'jane') {
if (ages[person] % 6 === 0 && ages[person] % 8 === 0) { message = 'Something <i><b>awesomely weird</b></i> is happening.' }
else if (ages[person] % 6 === 0) { message = 'Something <i>weird</i> is happening.' }
else if (ages[person] % 8 === 0) { message = 'Something <b>awesome</b> is happening.' }
}
ages.lifelines[person].innerHTML += `<p>${ages[person]}: ${message}</p>`
}
function advanceTenYears () {
for (let x=0; x < 10; x++) {
advanceOneYear()
}
}
function advanceOneYear() {
if (ages.ada < 36) increasePersonAge('ada')
if (ages.grace < 85) increasePersonAge('grace')
if (ages.jane < 100) increasePersonAge('jane')
}
btnAdvanceOneYear.onclick = advanceOneYear
btnAdvanceTenYears.onclick = advanceTenYears
let intervalId
btnStart.onclick = function () {
intervalId = setInterval(advanceOneYear, 1000)
}
btnStop.onclick = function () {
clearInterval(intervalId)
}
Also see: Tab Triggers