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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<body>
<div class="container center-align">
<div id="calculator">
<div class="card grey lighten-2">
<div class="card-content center-align">
<div id="solar" class="right-align">
<div class="solarcell"> </div>
<div class="solarcell"> </div>
<div class="solarcell"> </div>
<div class="solarcell"> </div>
<div class="solarcell"> </div>
</div>
<h5 class="left-align white-text">Cakkalate</h5>
<div class="row">
<div id=displayContainer class="col s12 right-align">
<h3 id="display" class="lime lighten-4">0</h3>
</div>
</div>
<div class="row">
<div class="col s3">
<a id="Escape" class="btn waves-effect yellow lighten-5 grey-text text-darken-4">ac</a>
</div>
<div class="col s3">
<a id="Backspace" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">c</a>
</div>
<div class="col s3">
<a id="percent" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">%</a>
</div>
<div class="col s3">
<a id="divide" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">÷</a>
</div>
</div>
<div class="row">
<div class="col s3">
<a id="7" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">7</a>
</div>
<div class="col s3">
<a id="8" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">8</a>
</div>
<div class="col s3">
<a id="9" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">9</a>
</div>
<div class="col s3">
<a id="multiply" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">x</a>
</div>
</div>
<div class="row">
<div class="col s3">
<a id="4" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">4</a>
</div>
<div class="col s3">
<a id="5" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">5</a>
</div>
<div class="col s3">
<a id="6" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">6</a>
</div>
<div class="col s3">
<a id="subtract" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">-</a>
</div>
</div>
<div class="row">
<div class="col s3">
<a id="1" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">1</a>
</div>
<div class="col s3">
<a id="2" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">2</a>
</div>
<div class="col s3">
<a id="3" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">3</a>
</div>
<div class="col s3">
<a id="add" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">+</a>
</div>
</div>
<div class="row">
<div class="col s3">
<a id="0" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">0</a>
</div>
<div class="col s3">
<a id="dot" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">.</a>
</div>
<div class="col s6">
<a id="Enter" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">=</a>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
/* To do: alter the position of the decimal, or try another font */
body {
background-color: darkslategray;
}
.container {
width: 320px;
max-height: 470px;
}
.card {
/* border: 2px outset gray; */
width: 320px;
max-height: 470px;
margin-bottom: 0px;
border-radius: 5px;
box-shadow:
inset 0px 0px 8px rgba(0,0,0,.7),
3px 3px 10px rgba(0,0,0,0.2);
}
.card-content {
height: 470px;
}
#solar {
width: 280px;
height: 40px;
padding: 5px;
margin-bottom: 0px;
background-color: #996e00;
}
.solarcell {
display: inline-block;
height: 25px;
width: 20px;
/* margin: 0px;
padding: 0px; */
background-color: #4d3333;
border: 2px solid #555;
}
h3 {
font-family: 'Segment7Standard', monospace;
font-weight: normal;
font-style: italic;
border: 6px inset #CCC;
padding-right: 2px;
margin-top: 0px;
}
h5 {
width: 100%;
margin-bottom: 0px !important;
margin-top: 0px;
background-color: #e6a400;
padding: 5px;
}
.btn {
width: 100% !important;
/* background-color: lightyellow;
color: black; */
}
window.onkeydown = function(e) {
var keyID;
switch (e.key) { // .key returns the "printed" character, so top-row and num pad digits are the same. In the html, the IDs of number buttons are the number in question. However, for clarity and because jQuery can't handle non-alphanumeric IDs, the operators and function buttons are given word IDs, assigned below.
case "%":
keyID = "percent";
break;
case ".":
keyID = "dot";
break;
case "/":
keyID = "divide";
break;
case "*":
keyID = "multiply";
break;
case "-":
keyID = "subtract";
break;
case "+":
keyID = "add";
break;
default:
keyID = e.key;
}
$("#" + keyID).click();
}
var valDone = true; //This, set to false, will translate to "we're in the middle of inputting a number here!"
var equationDone = true; //This, set to false, will translate to "we're in the middle of an equation here!"
var currentVal = 0;
var holdingVal = 0;
var currentOperator = "";
function operation(a, b) {
if (currentOperator === "divide") {
if (b === 0) { //throw error on divide by 0
$("#display").html("E ");
setTimeout(escape, 1500); //escape is the function activated by the "AC" key, defined below
} else {
return +(a / b).toFixed(10); //fixing the result to 10 decimal places (max display can accomodate) fixes peculiarities of 64-bit representation of floating point decimals. The leading "+" turns the string back into a number.
}
} else if (currentOperator === "multiply") {
return +(a * b).toFixed(10);
} else if (currentOperator === "subtract") {
return +(a - b).toFixed(10);
} else { //if the current operator "", meaning the start of an equation, we still want to add the currentVal to holdingVal
return +(a + b).toFixed(10);
}
}
// To emulate the action of a physical calculator, and to give some feedback on pressing an operator button for the first time (not changing the displayed number) I introduced this function to momentarily hide and then show the text. Rather than use a .hide, .show, or add and remove a visibility or opacity class (I wanted to keep the element's bgcolor), I just made the text color match the background.
var blink = function() {
$("#display").addClass("lime-text text-lighten-4");
setTimeout("$('#display').removeClass('lime-text text-lighten-4');", 100);
}
// I want different actions depending on whether the display is FULL vs an equation returning an overflowing value
var displayFull = function() {
// The decimal doesn't count in the "character count"
if ($("#display").text().replace('.', '').length === 11) {
return true;
} else {
return false;
}
}
function checkOverflow() {
if ($("#display").text().replace('.', '').length <= 11) {
return;
} else {
// display "E" at the far left of the display, like early calculators
$("#display").html("E ");
setTimeout(escape, 1500);
}
}
var checkSpelling = function() {
switch ($("#display").text()) {
case "0.7734":
Materialize.toast("Well hello to you to!", 4000);
break;
case "5318008":
Materialize.toast("Seriously? Classy.", 4000);
break;
case "53045.3080": // http://hrwiki.org/wiki/Cakkalate
Materialize.toast("\"Look! I can make mine say... oboe shoes!\"", 4000);
break;
default:
return;
}
}
$(".num").click(function() {
// are we starting a number from scratch, or adding digits to one?
if (valDone === true || $("#display").text() === "0") { //Why both? You could have just finished an operation, leaving a number on the screen, and be starting to input a new number, or you could be showing "0" from having backspaced. OR you could be a moron and trying to start your number by pressing 0 a bunch of times. Moron. Saw you comin.
blink();
$("#display").text(this.id);
} else if (displayFull()) {
blink(); // and do nuttin
} else {
blink();
$("#display").append(this.id);
}
currentVal = Number($("#display").text());
valDone = false;
checkSpelling();
});
var escape = function() { //a named function, so the overflow error function can use it too
currentVal = 0;
holdingVal = 0;
blink();
$("#display").text(currentVal);
valDone = true;
equationDone = true;
}
$("#Escape").click(escape);
$("#Backspace").click(function() {
currentVal = Number($("#display").text().slice(0, -1));
blink();
$("#display").text(currentVal);
if (currentVal === 0) {
valDone = true;
}
});
$("#dot").click(function() {
if (valDone === true) {
blink();
$("#display").text("0.");
} else if (displayFull()) {
blink();
} else {
currentVal = $("#display").text();
currentVal += ".";
blink();
$("#display").text(currentVal);
}
valDone = false;
});
$(".operator").click(function() {
if (equationDone === true) {
var result = currentVal;
} else {
result = operation(holdingVal, currentVal);
}
currentVal = result;
holdingVal = result;
currentOperator = this.id;
blink();
$("#display").text(currentVal);
checkOverflow();
equationDone = false;
valDone = true;
});
$("#Enter").click(function() {
var result = operation(holdingVal, currentVal);
currentVal = result;
holdingVal = result;
currentOperator = "";
blink();
$("#display").text(currentVal);
checkOverflow();
valDone = true;
equationDone = true;
});
// What does the percent key DO anyway? see https://blogs.msdn.microsoft.com/oldnewthing/20080110-00/?p=23853
$("#percent").click(function() {
var percentage = holdingVal * currentVal / 100;
currentVal = percentage;
blink();
$("#display").text(currentVal);
checkOverflow();
});
Also see: Tab Triggers