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="calculator">
<!-- <p id="output"></p> -->
<input type="text" disabled id="output"/>
<div id="keypad">
<button class="clear">C</button>
<button class="number">1</button>
<button class="number">2</button>
<button class="number">3</button>
<button>/</button>
<button class="number">4</button>
<button class="number">5</button>
<button class="number">6</button>
<button>*</button>
<button class="number">7</button>
<button class="number">8</button>
<button class="number">9</button>
<button>-</button>
<button class="number">0</button> <button class="number">.</button>
<button>=</button>
<button>+</button>
</div>
</div>
body {
height: 100%;
text-align: center;
display: -webkit-box;
display: flex;
align-content: center;
margin-top: 24px;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
flex-flow: column wrap;
background: #f2f2f2;
}
#calculator {
padding: 12px 10px 6px;
border-radius: 2px;
box-shadow: 0px -6px 10px white, 0px 4px 15px rgba(0, 0, 0, 0.15);
width: 350px;
}
#calculator #output {
height: 60px;
font: bold 30px/44px monospace;
margin-bottom: 28px;
text-align: right;
border-radius: 2px;
white-space: nowrap;
overflow: hidden;
margin-right: 8px;
box-shadow: inset 2px 2px 5px #BABECC, inset -5px -5px 10px #FFF;
width: 100%;
box-sizing: border-box;
transition: all 0.2s ease-in-out;
appearance: none;
-webkit-appearance: none;
}
#calculator #keypad {
width: 350px;
height: 410px;
display: grid;
grid-template-columns: repeat(4, 80px);
-webkit-box-pack: justify;
justify-content: space-between;
}
#calculator #keypad button {
position: relative;
width: 70px;
height: 70px;
border-radius: 50%;
background: #f2f2f2;
transition: all 100ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
box-shadow: 0px -6px 10px white, 0px 4px 15px rgba(0, 0, 0, 0.15);
cursor: pointer;
}
#calculator #keypad button:active {
box-shadow: 0 15px 20px rgba(0, 0, 0, 0.02);
}
#calculator #keypad button.clear {
grid-column: 1 / span 4;
grid-row: 1 / span 1;
background: #FFE1D9;
}
button:focus {
border: none;
outline: 0 !important;
outline-style: none;
}
button, #output {
border: 0;
outline: 0;
font-size: 16px;
border-radius: 320px;
padding: 16px;
background-color: #EBECF0;
text-shadow: 1px 1px 0 #FFF;
}
button:active:after {
box-shadow: inset 0px -2px 5px white, inset 0px 2px 5px rgba(0, 0, 0, 0.15);
}
button:after {
content: "";
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
border-radius: 50%;
z-index: 2;
}
(function myCalculator() {
/*Hook up buttons*/
//Save the buttons that make up the calculator keys as an array
const keys = document.getElementsByTagName("button");
//Loop through them and assign a click handler so something happens when they are clicked. Using ECMA6 "for...of" instead of older indexed loop.
for (const key of keys) {
key.onclick = handleClick;
}
//Declare and initialize variables
//Save the output paragraph as a variable because I'm going to use it constantly
const output = document.getElementById("output");
let numOut = "", //number input before operator
numOutNew = "", //number input after operator
op = "", //operator
int = 0; //calculation result;
//Click function for the buttons
function handleClick() {
let num = this.innerText; //when a button is pressed we get its value, which could be a number, a math operator or the "C" key, which clears the calculator
if (this.classList.contains("number")) {//Input is a number or the decimal point
numOut += num; //as long as user is not pressing an operation key we concatenate the input to keep building the number with each digit entered
output.value = numOut; //then send it to the output screen
} else {
//The user has chosen a calculation, so we do run the calculation function
doCalc(num);//"num" here is an operator, like "*" or "C"
}
}
//When the calculation is finished or if the math breaks from bad key input we have to clear the calculator
function doClear() {
output.value = "";
numOut = "";
numOutNew = "";
op = "";
int = 0;
}
//Calculation function
function doCalc(calc) {
//pass in an operator This was the "num" value from the handleClick function
if (calc === "C") {//clear calculator
doClear();
}
// numOutNew is assigned the value of the first number the user inputs AFTER an operator key is pressed and this function runs. If it is empty, then this is the first time we're getting an operator and we don't have enough numbers to do math. If it is not empty we are have received the second number and a second operator has been clicked, "=" for example.
else if (numOutNew !== "") {
numOut = parseFloat(numOut); //convert input from string to number
switch (op) {
case "/":
if (numOut !== 0) {
int = numOutNew / numOut;//divide
} else {
int = "error";//division by zero is not allowed
}
break;
case "*":
int = numOutNew * numOut;//multiply
break;
case "-":
int = numOutNew - numOut;//subtract
break;
case "+":
int = numOutNew + numOut;//add
break;
case "=":
int = parseFloat(output.value);//When "=" is clicked the previous operator's calculation is done and we just have to save that result for next time, in case the user continues to do more operations to their total. The result of the previous operation was put in the output field, so we get it from there.
break;
}
if (isNaN(int)) {//if we tried to do an operation without two numbers, for example ". / 3"
output.value = "error";//show the user "error" instead of garbage output
setTimeout(function() {
doClear();
}, 2000);//does doClear after 2 seconds and the error message disappears
} else {
output.value = int; //Show the result
numOutNew = int; //Save the result as the first number for the next calculation if the user chains calculations
}
} else if (numOut !== "") {
//If I didn't have to doClear for a NaN result I will have a numeric value
numOutNew = parseFloat(numOut); //The user has used an operator for the first time and we need to store the first number input until we get the second one and can use the operator
}
op = calc; //We store the operator that was passed into this function to use after the we get the second number
numOut = ""; //Clear the input variable to reuse for the next number
}
})();
Also see: Tab Triggers