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.
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
<body>
<div class="container panel panel-default" id="calc">
<div class="row">
<div class="xs-col-12" id="title">FreeCodeCalc</div>
<div class="col-xs-2" id="sign"></div>
<div class="col-xs-10" id="digits"></div>
<div class="col-xs-12" id="input"></div>
<div class="col-xs-3 btn btn-primary raised">7</div>
<div class="col-xs-3 btn btn-primary raised">8</div>
<div class="col-xs-3 btn btn-primary raised">9</div>
<div class="col-xs-3 btn btn-default raised">÷</div>
<div class="col-xs-3 btn btn-primary raised">4</div>
<div class="col-xs-3 btn btn-primary raised">5</div>
<div class="col-xs-3 btn btn-primary raised">6</div>
<div class="col-xs-3 btn btn-default raised">×</div>
<div class="col-xs-3 btn btn-primary raised">1</div>
<div class="col-xs-3 btn btn-primary raised">2</div>
<div class="col-xs-3 btn btn-primary raised">3</div>
<div class="col-xs-3 btn btn-default raised">-</div>
<div class="col-xs-3 btn btn-warning raised">C</div>
<div class="col-xs-3 btn btn-primary raised">0</div>
<div class="col-xs-3 btn btn-default raised">.</div>
<div class="col-xs-3 btn btn-default raised">+</div>
<div class="col-xs-3 btn btn-warning raised">BKSP</div>
<div class="col-xs-9 btn btn-default raised">=</div>
<div> </div>
</div>
</div>
</body>
@media (max-width:320px) {
#calc {
width: 100% !important;
}
}
.btn {
-webkit-border-radius: 25px !important;
-moz-border-radius: 25px !important;
border-radius: 25px !important;
}
body {
font-family: "Press Start 2P";
background: darkgreen;
}
#calc {
width: 340px;
/*margin-top: 30px;*/
border-radius: 10px;
border-width: 10px;
border-style: outset;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#digits {
font-size: 24px;
text-align: right;
height: 30px;
margin-bottom: 10px;
margin-top: 10px;
}
#sign {
font-size: 24px;
text-align: left;
height: 30px;
margin-bottom: 10px;
margin-top: 10px;
}
#input {
font-size: 8px;
height: 16px;
}
#title {
font-size: 8px;
text-align: center;
}
/*button style ideas from
https://bootstrapbay.com/blog/bootstrap-button-styles/
*/
.btn-primary.raised {
box-shadow: 0 3px 0 0 #a6a6a6;
}
.btn-primary.raised:active, .btn-primary.raised.active {
box-shadow: none;
margin-bottom: -3px;
margin-top: 3px;
}
.btn-default.raised {
box-shadow: 0 3px 0 0 #a6a6a6;
}
.btn-default.raised:active, .btn-default.raised.active {
box-shadow: none;
margin-bottom: -3px;
margin-top: 3px;
}
.btn-warning.raised {
box-shadow: 0 3px 0 0 #a6a6a6;
}
.btn-warning.raised:active, .btn-warning.raised.active {
box-shadow: none;
margin-bottom: -3px;
margin-top: 3px;
}
/*The current result is stored in the digits div. The input string since
equals was last pushed is stored in the input div. The last operator
entered is stored in #sign.
Two copies of the current input are stored: the one in #input and the
one stored in toEval. The second one is not displayed. The reason for having
two copies is so that the 'times' and 'division' symbols could be used.
This could probably be simplified by setting the id of those divs to '*'
and '/'.
When a button is pushed, the innerHTML is sent to a switch statement
for the appropriate action.
The exponential notation feature is unfinished.
*/
var lastChar = "";
var decPoint = false;
var operator = false;
var answer = false;
var clear = true;
var num = /[0-9]/;
var ops = ["+", "-", "×", "÷"];
var toEval = ""; //This is what is sent to eval().
var temp = 0.0;
var isExp = false;
$(".btn").click(function () {
lastChar = event.target.innerHTML;
switch (lastChar) {
case "0":
if(document.getElementById("digits").innerHTML == "0") return;
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
if(isExp) CLEAR();
if(testLen() && !operator) return;
if(answer) CLEAR();
if(operator) document.getElementById("digits").innerHTML = "";
document.getElementById("digits").innerHTML += lastChar;
document.getElementById("input").innerHTML += lastChar;
toEval += lastChar;
operator = false;
clear = false;
answer = false;
break;
case "BKSP":
if(isExp) CLEAR();
if(answer) {
document.getElementById("digits").innerHTML = "";
answer = false;
return;
}
if(document.getElementById("digits").innerHTML.length > 0) {
document.getElementById("digits").innerHTML =
document.getElementById("digits").innerHTML.slice(0, -1);
document.getElementById("input").innerHTML = document.getElementById("input").innerHTML.slice(0, -1);
toEval = toEval.slice(0, -1);
} else {
//if(document.getElementById("sign").innerHTML != "") {
//if(ops.test(toEval.slice(-1))) {
//}
if(ops.includes(document.getElementById("input").innerHTML.slice(-1))) {
operator = false;
document.getElementById("sign").innerHTML = "";
}
document.getElementById("input").innerHTML =
document.getElementById("input").innerHTML.slice(0, -1);
toEval = toEval.slice(0, -1);
}
if(!document.getElementById("digits").innerHTML.includes(".")) {
decPoint = false;
}
break;
case ".":
if((decPoint || testLen()) && !answer) return;
if(isExp) CLEAR();
if(answer) {
CLEAR();
}
if(operator) {
document.getElementById("digits").innerHTML = "";
}
document.getElementById("digits").innerHTML += lastChar;
document.getElementById("input").innerHTML += lastChar;
toEval += ".";
operator = false;
decPoint = true;
break;
case "C":
CLEAR();
break;
case "×":
case "÷":
case "+":
case "-":
//need for backspace
if(ops.includes(document.getElementById("input").innerHTML.slice(-1))) return;
if(operator) return;
//todo
if(isExp) CLEAR();
if(!(/[0-9]/.test(document.getElementById("digits").innerHTML)) && document.getElementById("input").innerHTML == "") return;
if(answer) {
document.getElementById("input").innerHTML = temp;
answer = false;
}
document.getElementById("sign").innerHTML = lastChar;
document.getElementById("input").innerHTML += lastChar;
if(lastChar == "×") {
toEval += "*";
} else if(lastChar == "÷") {
toEval += "/";
} else {
toEval += lastChar;
}
operator = true;
clear = false;
decPoint = false;
break;
case "=":
if(clear || operator) return;
if(ops.includes(document.getElementById("input").innerHTML.slice(-1))) return;
document.getElementById("sign").innerHTML = lastChar;
temp = String(eval(toEval));
if(!temp.includes(".") && temp.length > 9) {
temp = toSci(temp);
} else {
temp = temp.slice(0, 9);
}
document.getElementById("digits").innerHTML = temp;
answer = true;
decPoint = false;
break;
}
});
function CLEAR() {
document.getElementById("digits").innerHTML = "";
document.getElementById("sign").innerHTML = "";
document.getElementById("input").innerHTML = "";
toEval = "";
decPoint = false;
operator = false;
answer = false;
clear = true;
temp = 0.0;
isExp = false;
}
//only goes one-way right now
function toSci(big) {
isExp = true;
if(big[0] == ".") {
//todo
}
return big[0] + "." + big.slice(1, 3) + "×10^" + (big.length - 1);
}
function testLen() {
//console.log(lastChar);
if(document.getElementById("digits").innerHTML.length >= 9) return true;
if(document.getElementById("input").innerHTML.length >= 33) return true;
return false;
}
Also see: Tab Triggers