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.
<div class="container">
<h2 class="title">Javascript Calculator</h2>
<div class="result-container">
<h1 class="result"></h1>
<p class="history"></p>
</div>
<div class="input-buttons">
<button value="AC" class="clear-button">AC</button>
<button value="/" class="operator-button">/</button>
<button value="*" class="operator-button">*</button>
<button value="7" class="number-button">7</button>
<button value="8" class="number-button">8</button>
<button value="9" class="number-button">9</button>
<button value="-" class="operator-button">-</button>
<button value="4" class="number-button">4</button>
<button value="5" class="number-button">5</button>
<button value="6" class="number-button">6</button>
<button value="+" class="operator-button">+</button>
<button value="1" class="third-row number-button">1</button>
<button value="2" class="third-row number-button">2</button>
<button value="3" class="third-row number-button">3</button>
<button value="0" class="zero-button">0</button>
<button value="." class="decimal-button">.</button>
<button value="=" class="equals-button">=</button>
</div>
</div>
html {
background-color: #BABABA;
}
body,
button {
font-family: 'Oxygen Mono', monospace;
}
button {
width: 62px;
-webkit-border-radius: 7px;
border-radius: 7px;
border-style: none;
font-size: 30px;
margin: 10px 0 5px 0;
padding: 10px 5px;
cursor: pointer;
background-color: #818181;
color: #fff;
outline: none;
-webkit-box-shadow: 0 5px #5E5353;
box-shadow: 0 5px #5E5353;
vertical-align: top;
}
button:active {
-webkit-transform: translateY(4px);
-ms-transform: translateY(4px);
transform: translateY(4px);
-webkit-box-shadow: none;
box-shadow: none;
}
.container {
margin: 75px auto;
width: 350px;
height: 525px;
padding: 15px 0 0 0;
-webkit-border-radius: 10px;
border-radius: 10px;
text-align: center;
background-color: #E3A62D;
-webkit-box-shadow: 10px 10px 19px 4px #42403a, inset 0px -2px 16px 6px #b38227;
box-shadow: 10px 10px 19px 4px #42403a, inset 0px -2px 16px 6px #b38227;
}
.clear-button {
background-color: #D24848;
-webkit-box-shadow: 0 5px #A82424;
box-shadow: 0 5px #A82424;
}
.third-row {
float: left;
margin-right: 7px;
margin-left: 2px;
}
.zero-button,
.clear-button {
width: 132px;
}
.operator-button {
background-color: #32ac22;
-webkit-box-shadow: 0 5px #1d8510;
box-shadow: 0 5px #1d8510;
}
.equals-button {
position: relative;
bottom: 69px;
height: 123px;
background-color: #478dff;
-webkit-box-shadow: 0 5px #125CD5;
box-shadow: 0 5px #125CD5;
}
.input-buttons {
width: 80%;
margin: 15px auto;
}
.title {
font-size: 1.5em;
margin: 10px 0;
}
.result-container {
font-family: 'Orbitron', sans-serif;
-webkit-border-radius: 5px;
border-radius: 5px;
border: 1px solid #82817F;
width: 270px;
background-color: #D4D4AA;
-webkit-box-shadow: inset 0px 0px 16px 0px #8c8c42;
box-shadow: inset 0px 0px 16px 0px #8c8c42;
height: 60px;
text-align: right;
margin: 0 auto;
padding: 0 5px 0 0;
}
.result{
font-size: 25px;
margin: 5px 0 0 0;
}
.history {
font-size: 10px;
margin: 10px 0 0 0;
}
(function () {
const display = document.querySelector(".result");
const history = document.querySelector(".history");
const number = document.getElementsByClassName("number-button");
const operator = document.getElementsByClassName("operator-button");
const zero = document.querySelector(".zero-button");
const equals = document.querySelector(".equals-button");
const clear = document.querySelector(".clear-button");
const decimal = document.querySelector(".decimal-button");
let decimalState = true;
let zeroState = false;
let evaluation = false;
init();
function init() {
display.textContent = "0";
history.textContent = "0";
decimalState = true;
zeroState = false;
evaluation = false;
}
function digitLimit() {
// Sets digit limit on display to 10 characters. Function discounts decimal from
// number limit
if (display.textContent.includes(".")) {
if (display.textContent.length > 13) {
display.textContent = "Digit Limit Met";
history.textContent = "";
}
} else {
if (display.textContent.length > 12) {
display.textContent = "Digit Limit Met";
history.textContent = "";
}
}
// Sets digit limit on history to 32 characters.
if (history.textContent.length > 32) {
display.textContent = "Digit Limit Met";
history.textContent = "";
}
}
// Iterates through number elements and sets event listeners
for (var i = 0; i < number.length; i++) {
number[i].addEventListener("click", function () {
const prevDigit = display.textContent.slice(-1);
if (evaluation || display.textContent === "0") {
display.textContent = this.value;
history.textContent = this.value;
} else if (isNaN(prevDigit) && prevDigit !== ".") {
display.textContent = this.value;
history.textContent += this.value;
} else {
display.textContent += this.value;
history.textContent += this.value;
}
zeroState = true;
evaluation = false;
digitLimit();
});
}
// Iterates through operator elements and sets event listeners
for (var i = 0; i < operator.length; i++) {
operator[i].addEventListener("click", function () {
const prevIndex = display.textContent.slice(-1);
// if previous value is an operator, a new clicked operator will replace it
if (prevIndex === "*" || prevIndex === "+" || prevIndex === "-" || prevIndex === "/") {
display.textContent = display.textContent.slice(0, -1);
history.textContent = history.textContent.slice(0, -1);
display.textContent += this.value;
history.textContent += this.value;
} else if (history.textContent.length > 0) {
display.textContent = this.value;
history.textContent += this.value;
}
decimalState = true;
zeroState = false;
evaluation = false;
digitLimit();
});
}
// clears display value
clear.addEventListener("click", function () {
init();
});
// adds decimal to display
decimal.addEventListener("click", function () {
const lastDigit = display.textContent.slice(-1);
// if decimal button is pressed, then it is disabled unless decimal in current integer is deleted
if (decimalState && isNaN(lastDigit)) {
display.textContent = "0" + this.value;
history.textContent += "0" + this.value;
decimalState = false;
zeroState = true;
} else if (decimalState && !evaluation) {
display.textContent += this.value;
history.textContent += this.value;
decimalState = false;
zeroState = true;
} else if (evaluation && decimalState) {
display.textContent = "0" + this.value;
history.textContent = "0" + this.value;
decimalState = false;
} else {
display.textContent += "";
}
evaluation = false;
digitLimit();
});
zero.addEventListener("click", function () {
const prevItem = display.textContent.slice(-1);
// Prevents first number between operators from being zero
if (!zeroState && prevItem === "0") {
display.textContent += "";
decimalState = true
} else if (evaluation) {
display.textContent = this.value;
history.textContent = display.textContent;
} else {
display.textContent += this.value;
history.textContent += this.value;
}
digitLimit();
})
// Evaluates history.textContent on display and rounds to the nearest hundredths
// if a decimal
equals.addEventListener("click", function () {
const lastIndex = display.textContent.slice(-1);
// If last digit is not a number then "Bad Syntax!" will display otherwise it
// will evaluate history.textContent
if (isNaN(lastIndex)) {
display.textContent = "Bad Syntax!";
} else {
// Rounds any decimal total to nearest hundredth
display.textContent = parseFloat(eval(history.textContent));
history.textContent = parseFloat(eval(history.textContent));
}
decimalState = true;
evaluation = true;
digitLimit();
});
})()
Also see: Tab Triggers