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.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/percentage-change-calculator/style.css">
<meta name="viewport" width="device-width"/>
<meta charset="UTF-8">
</head>
<body>
<header>
<h1>Percentage Change Calculator</h1>
</header>
<section class="intro">
<p>Forget how to calculate the <span class ="tooltip">percentage change<span class="tooltiptext">First, work out the decrease between the two numbers you are comparing. Next, divide that by the original number and multiply it by 100. If the answer is negative, it's a percentage increase.<br><br> Or just use this calculator.</span></span> between two numbers? Don't have a napkin for back-of-the-napkin math? Use this calculator instead to quickly find the percentage increase or decrease between any two numbers.</p>
</section>
<section>
<div class="calculator">
<form>
<div class ="field1">
<label>Before number:</label>
<input id="my-form" type="text">
</div>
<div class ="field2">
<label>After number:</label>
<input id="my-form2" type="text">
</div>
<div class ="submit-button">
<button id="do-math" type="button" onclick="handleClick()">Do the math!</button>
</div>
</form>
</div>
</section>
<section class="result">
<h3 id="output">Percent change:</h3>
<p id="data-output"></p>
</section>
</body>
<script src="/percentage-change-calculator/script.js" type="text/javascript"></script>
</html>
body {
font-family: Lato;
background-color: white;
color: black;
}
header {
font-size: 2rem;
text-align: center;
text-decoration: wavy blue underline 3px;
margin: 3rem;
}
.intro {
font-size: 1.4rem;
text-align: left;
line-height: 2.8rem;
width: 50%;
margin: 4rem auto;
}
.tooltip {
display: inline-block;
position: relative;
border-bottom: 0.1rem solid blue;
cursor: help;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 15rem;
font-size: 0.7rem;
line-height: 1rem;
background-color: black;
color: white;
text-align: left;
padding: 5px 10px;
border-radius: 5px;
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
form {
width: 30%;
margin: 0 auto;
}
.field1, .field2 {
color: black;
font-weight: bold;
margin: 2rem auto;
display: flex;
}
.calculator {
display: block;
margin: 0 auto;
float: none;
}
label {
font-size: 1.4rem;
letter-spacing: 0.04rem;
width: 11rem;
}
input {
font-size: 1.2rem;
border: 0.1rem black solid;
width: 11rem;
}
button {
display: block;
background-color: blue;
margin: 3rem auto;
font-family: "Lato";
font-weight: bold;
padding: 0.8rem 3rem;
color: white;
font-size: 1.3rem;
box-shadow: 3px 3px black;
}
button:hover {
background-color: #000080;
}
.result {
background-color: blue;
color: white;
padding: 2rem 1rem 8rem;
text-align: center;
line-height: 3rem;
width: 100%;
}
#data-output {
display: block;
font-size: 1.5rem;
float: center;
margin: 1rem auto;
}
#output {
display: block;
margin: 1rem auto;
float: center;
font-size: 2rem;
padding-top: 3rem;
}
.highlight {
background-color: yellow;
color: blue;
padding: 0.4rem 0.4rem;
border: 4px black solid;
}
/*Make responsive on mobile*/
@media only screen and (max-width: 600px) {
body {
font-family: Lato;
background-color: white;
color: black;
}
header {
font-size: 0.9rem;
margin-top: 1rem auto;
text-align: center;
text-decoration: wavy blue underline 3px;
margin: 3rem;
}
.intro {
font-size: 1rem;
line-height: 1.7rem;
margin: 2.3rem auto;
width: 80%;
}
.field1, .field2 {
margin: 1rem auto;
}
form {
width: 80%;
}
label {
font-size: 0.9rem;
width: 40%;
letter-spacing: 0;
}
input {
width: 60%;
}
button {
margin-top: 2rem;
margin-bottom: 1rem;
padding: 0.5rem 1rem;
}
#output {
font-size: 1.5rem;
}
#data-output {
font-size: 1rem;
}
.result {
line-height: 2rem;
}
.tooltip .tooltiptext {
width: 120%;
margin: 1rem auto;
}
}
// https://css-tricks.com/snippets/css/a-guide-to-flexbox/
const doMath = document.getElementById("do-math")
const myForm = document.getElementById("my-form");
const myForm2 = document.getElementById("my-form2");
const answer = document.getElementById("data-output");
const handleClick = (e) => {
let originalNumber = myForm.value;
let newNumber = myForm2.value;
//If there was an increase
if ((newNumber - originalNumber) > 0) {
difference = (newNumber - originalNumber);
percentIncrease = Math.round((newNumber - originalNumber) / originalNumber * 100);
answer.innerHTML = `There was a <span class="highlight">${percentIncrease}% increase</span> from ${originalNumber} to ${newNumber}. <br> That's a difference of ${difference}.`;
//If there was a decrease
} else if ((newNumber - originalNumber) < 0) {
difference = (originalNumber - newNumber);
percentDecrease = Math.round((originalNumber - newNumber) / originalNumber * 100);
answer.innerHTML = `There was a <span class="highlight"> ${percentDecrease}% decrease</span> from ${originalNumber} to ${newNumber}. <br> That's a difference of ${difference}.`;
//If there was no change
} else if (newNumber === originalNumber) {
answer.innerText = `There was no change.`
console.log('There was no change.');
//If the inputs are invalid
} else {
answer.innerText = 'Something went wrong. Try again.';
console.log('Something went wrong. Try again.');
}
};
Also see: Tab Triggers