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" class="calculator">
<div class="result"><p></p></div>
<span class="clear grey">AC</span>
<span class="grey">+/-</span>
<span class="grey">%</span>
<span class="operator">÷</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span class="operator">x</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span class="operator">-</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span class="operator">+</span>
<span class="double">0</span>
<span>.</span>
<span class="operator">=</span>
</div>
@import url(https://fonts.googleapis.com/css?family=Roboto:100);
html {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
*, *:before, *:after {
-webkit-box-sizing: inherit;
-moz-box-sizing: inherit;
box-sizing: inherit;
}
html {
font-family: 'Roboto';
font-weight: 100;
}
body {
display: flex;
justify-content: center;
margin-top: 1em;
font-size: 2em;
background-color: #bbb;
}
.calculator {
display: flex;
flex-wrap: wrap;
flex-direction: row;
width: 300px;
box-shadow: 0px 3px 10px 3px #aaa;
border-right: 0.01em solid #333;
border-bottom: 0.01em solid #333;
}
.result {
width: 100%;
background-color: #000;
min-height: 2em;
color: white;
}
.result > p {
padding: 0 1em;
text-align: right;
line-height: 2em;
}
span {
box-sizing: border-box;
text-align: center;
cursor: pointer;
width: 25%;
background-color: #ccc;
line-height: 2em;
border-top: 0.01em solid #333;
border-left: 0.01em solid #333;
}
.operator {
background-color: #FF9500;
color: white;
}
.double {
width: 50%;
}
.grey {
background-color: #aaa;
}
.clicked {
background-color: darken(orange, 10%);
}
window.onload = function() {
var buttons = document.getElementsByTagName('span'), // Select all buttons
result = document.querySelectorAll('.result p')[0], // Select the result-field
clear = document.getElementsByClassName('clear')[0], // Select the clearAll-button
equation = [], // create the equation array
operator = false; // helper variable, tracks if operator was last button pushed
for (var i = 0; i < buttons.length; i += 1) { // Add onclick events to buttons
if (buttons[i].innerHTML === '=') {
buttons[i].addEventListener("click", calculate(i));
} else if (buttons[i].innerHTML === '+/-') {
buttons[i].addEventListener("click", invert(i));
} else if (buttons[i].innerHTML === '%') {
buttons[i].addEventListener("click", percent(i));
} else if (buttons[i].innerHTML === 'AC') {
equation = [];
} else {
buttons[i].addEventListener("click", addValue(i));
}
}
/*
* Clear the result screen and equation string and reset operator
*/
clear.onclick = function() {
result.innerHTML = '';
equation = [];
operator = false;
}
/*
* Add a value, number or operator, to the equation string. If an operator than first removes all other clicked classes and adds it to the button just pressed. Than checks if an operator was just pressed, in which case it replaces it with the button pressed. If type is a Number, than value was just calculated so empty the array and start a new equation. Otherwise you would add numbers to the end of your calculated value.
*/
function addValue(i) {
return function() {
if (buttons[i].innerHTML === '÷') {
clicked(this);
ifOperatorThanSwap('/');
} else if (buttons[i].innerHTML === 'x') {
clicked(this);
ifOperatorThanSwap('*');
} else if (buttons[i].innerHTML === '+') {
clicked(this);
ifOperatorThanSwap('+');
} else if (buttons[i].innerHTML === '-') {
clicked(this);
ifOperatorThanSwap('-');
} else {
removeClicked();
if (checkIfNum(equation[equation.length - 1])) {
equation = [];
equation.push(buttons[i].innerHTML);
operator = true;
} else {
equation.push(buttons[i].innerHTML);
}
if (operator) {
result.innerHTML = buttons[i].innerHTML;
} else {
result.innerHTML += buttons[i].innerHTML;
}
operator = false;
}
};
}
/*
* First removes any button with clicked class, then adds .clicked to pressed button
*/
function clicked(i) {
removeClicked(i);
i.classList.add('clicked');
}
/*
* Finds any elements with the clicked class and removes the class.
*/
function removeClicked(i) {
var elems = document.querySelectorAll(".clicked");
[].forEach.call(elems, function(el) {
el.classList.remove("clicked");
});
}
/*
* Calculate and print the equation array. Firsts joins all the elements in the equations array than evals the string of numbers and operators. Than it empties the equation array and puts the result as the first elemnt in the array.
*/
function calculate(i) {
return function() {
if (equation.length == 0) { // If nothing in array, do nothing
return;
} else {
var answer = eval(equation.join(''));
if (answer % 1 === 0) { // check if interger or float
result.innerHTML = answer;
} else { // if float than round to four numbers after decimal
result.innerHTML = answer.toFixed(4);
}
equation = [];
equation.push(answer);
operator = false;
}
};
}
/*
* Invert the current item in the viewer to and from a negative number.
*/
function invert(i) {
return function() {
if (equation.length == 0) {
return;
} else {
var number = result.innerHTML; // Grab number currently typed in
popNumberOfDigits(number); // remove number of digits from equation array
var invert = number * -1; // create inverted number by multiplying by -1
equation.push(invert); // push to equation
result.innerHTML = invert; // push to results display
}
}
}
/*
* Changes the current number entered into a percentage.
*/
function percent(i) {
return function() {
var number = result.innerHTML; // Grab number currently typed in
popNumberOfDigits(number); // remove number of digits from equation array
var percent = number * 0.01; // create percentage
equation.push(percent); // push to equation
result.innerHTML = percent.toFixed(2); // show in results display
}
}
/*
* Checks if the operator value to see if the last button pressed was an operator. If it is than it removed the operator and replaces it with the operator that was just pressed. ie If you first hit plus, but change your mind and hit minus, it removes the plus from the equation array and replaces it with minus.
*/
function ifOperatorThanSwap(str) {
if (!operator) {
equation.push(str);
operator = true;
} else {
equation.pop();
equation.push(str);
}
}
/*
* Checks if a number is of type string or number.
*/
function checkIfNum(v) {
if (typeof v == 'string') {
return false;
} else if (typeof v == 'number') {
return true;
}
}
/*
* Checks the number of digits in the screen and removes them from the equation array.
*/
function popNumberOfDigits(number) {
var arr = number.split(''); // Create an array holding each part of the number (eg. 13 = ['1', '3'] )
for (i = 0; i < arr.length; i++) { // Removes the last few elements from equation array
equation.pop();
}
}
};
Also see: Tab Triggers