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="root" style="height: 100%"> hello div</div>
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
@import url('https://fonts.googleapis.com/css?family=Quicksand:500|Kaushan+Script|Source+Code+Pro:600|Abril+Fatface|Audiowide|Comfortaa:700|Kelly+Slab&display=swap');
body,
html {
height: 100%;
margin: 0px;
font-family: "Raleway", sans-serif;
}
.themeOne {
--shade1: #f4f1af;
--shade2: #e7e283;
--shade3: #dbd55b;
--shade4: #dedede;
}
.themeTwo {
--shade1: #aadff6;
--shade2: #7ec9e9;
--shade3: #57b5dd;
--shade4: #32a1d0;
--grey1: #dedede;
}
.themeThree {
--shade1: #c3cacf;
--shade2: #9ab1c2;
--shade3: #759bb6;
--shade4: #5385a9;
--grey1: #dedede;
}
.themeFour {
--shade1: #e6e6e6;
--shade2: #dedede;
--shade3: #d1d1d1;
--shade4: #c7c7c7;
--grey1: #dedede;
}
.themeFive {
--shade1: #466b8c;
--shade2: #8c5f46;
--shade3: #8c8346;
--shade4: #79468c;
--grey1: #dedede;
}
@mixin border-radius($a, $b, $c, $d) {
-webkit-border-radius: $a $b $c $d;
-moz-border-radius: $a $b $c $d;
-ms-border-radius: $a $b $c $d;
border-radius: $a $b $c $d;
}
@mixin height-calc($percentage, $value) {
height: -moz-calc(#{$percentage} - #{$value});
/* WebKit */
height: -webkit-calc(#{$percentage} - #{$value});
/* Opera */
height: -o-calc(#{$percentage} - #{$value});
/* Standard */
height: calc(#{$percentage} - #{$value});
}
.outerDiv {
height: 100%;
width: 100%;
background-color: var(--shade1);
transition: all .3s ease-in-out;
}
.innerDiv {
background: white;
padding: 0px;
position: relative;
min-width: 200px;
min-height: 200px;
@include border-radius(9px, 9px, 7px, 7px)
}
.header {
position: relative;
top: 0;
left: 0;
width: 100%;
background-color: var(--shade3);
@include border-radius(7px, 7px, 0px, 0px);
transition: all .3s ease-in-out;
}
.title {
width: 100%;
padding-left: 10px;
font-size: 1.7em;
font-family: 'Kaushan Script', cursive;
color: var(--shade2)
}
.textField {
box-shadow: inset 0px 0px 7px 7px rgba(0, 0, 0, 0.1);
height: 1.7em;
background-color: var(--grey1);
text-align: right;
padding-right: 5px;
@include border-radius(3px, 3px, 3px, 3px);
margin: 8px 5px 8px 5px;
border: 1px solid black;
transition: all .3s ease-in-out;
}
.button {
width: 2.1em;
padding: 0 0 0 3;
}
.button:focus {
outline: none;
}
.button:hover{
box-shadow: inset 0 0 1em rgba(255, 255, 255, .4);
cursor: default;
}
.input {
background-color: var(--shade2);
}
.evaluate {
background-color: var(--shade4);
}
.clear {
background-color: var(--shade4);
}
const {render} = ReactDOM
const {Provider, connect} = ReactRedux;
const {createStore} = Redux;
const themes = ["themeOne", "themeTwo", "themeThree", "themeFour", "themeFive"];
// Section for setting up state
const defaultState = {
theme: 4,
register: "0",
input: "0",
lastAction: ""
}
const INPUT = 0;
const OPERATION = 1;
const EQUALS = 2;
const CLEAR = 3;
const MULTIPLY = 4;
const DIVIDE = 5;
const ADD = 6;
const SUBTRACT = 7;
const BUTTON_CLASSES = ["input", "evaluate", "evaluate", "clear"];
const messageReducer = (state=defaultState, action) => {
let myState = {...state};
switch(action.type){
case INPUT:
if(action.integer =='.' && myState.input.includes('.'))
break;
if(myState.input != "0")
myState.input = myState.input + action.integer;
else if(action.integer != "0")
myState.input = action.integer;
break;
case CLEAR:
if(myState.input =="0"){
myState.lastAction = "";
myState.register = "0";
}
myState.input = "0";
break;
case EQUALS:
if(myState.lastAction != ""){
let toEval = myState.register + myState.lastAction + myState.input;
// console.log("going to evaluate the following: " + toEval);
myState.input = eval(toEval);
myState.register = "0";
myState.lastAction = "";
}
break;
case OPERATION:
if(myState.input == "0" && myState.register != "0"){
if(myState.lastAction != "" && !myState.lastAction.includes("-") && action.operation == "-"){
myState.lastAction = myState.lastAction + " " + action.operation;
}else{
myState.lastAction = action.operation;
}
// console.log("still deciding what action to take: " + myState.lastAction);
break;
}
if(myState.register == "0"){
myState.register = myState.input;
myState.lastAction = action.operation;
myState.input = "0";
}else{
let toEval = myState.register + myState.lastAction + myState.input;
// console.log("going to evaluate the following: " + toEval);
myState.register = eval(toEval);
myState.lastAction = action.operation;
myState.input = "0";
}
break;
default:
break;
}
return myState;
}
const store = createStore(messageReducer);
// end section for setting up redux
/////// setup for Header
const Header = (props) =>{
return(
<div className="header">
<div className="title">{props.title}</div>
</div>
)
}
/////// end setup for header
const tempFunction = (event) => {
console.log("keypressed " + event.target.id);
}
const keys = [
{symbol: 'AC', id: 'clear', eventKey: 'Backspace', type: CLEAR, width: 2},
{type: "HOLDER"},
{symbol: '=', id: 'equals', eventKey: '=', type: EQUALS, width: 2},
{type: "HOLDER"},
{symbol: '7', id: 'seven', eventKey: '7', type: INPUT},
{symbol: '8', id: 'eight', eventKey: '8', type: INPUT},
{symbol: '9', id: 'nine', eventKey: '9', type: INPUT},
{symbol: 'X', id: 'multiply', eventKey: '*', type: OPERATION, subtype: MULTIPLY},
{symbol: '4', id: 'four', eventKey: '4', type: INPUT},
{symbol: '5', id: 'five', eventKey: '5', type: INPUT},
{symbol: '6', id: 'six', eventKey: '6', type: INPUT},
{symbol: '/', id: 'divide', eventKey: '/', type: OPERATION, subtype: DIVIDE},
{symbol: '1', id: 'one', eventKey: '1', type: INPUT},
{symbol: '2', id: 'two', eventKey: '2', type: INPUT},
{symbol: '3', id: 'three', eventKey: '3', type: INPUT},
{symbol: '-', id: 'subtract', eventKey: '-', type: OPERATION, subtype: SUBTRACT},
{symbol: '0', id: 'zero', eventKey: '0', type: INPUT, width: 2},
{type: 'HOLDER'},
{symbol: '.', id: 'decimal', eventKey: '.', type: INPUT},
{symbol: '+', id: 'add', eventKey: '+', type: OPERATION, subtype: ADD}
];
////// keypad class
class KeyPad extends React.Component {
constructor(props){
super(props);
this.state ={
}
this.handleButton = this.handleButton.bind(this);
}
handleButton(event){
let key = {
id: event.target.id,
eventkey: event.target.getAttribute('eventkey'),
type: event.target.getAttribute('type'),
subtype: event.target.getAttribute('subtype'),
symbol: event.target.innerText
}
if(key.type == INPUT){
this.props.input(key.symbol);
}
else if(key.type == OPERATION ){
this.props.operation(key.eventkey);
}else if(key.type == EQUALS){
this.props.equals();
}else {
this.props.clear();
}
// console.log('saw button click, heres the values ' + JSON.stringify(key));
}
render(){
let rows = [];
let buttons = [];
for(let i=0; i<5;i++){
buttons = [];
for(let j=0;j <4 && i*4 + j < keys.length;j++){
let key = keys[(i*4)+j];
let col = key.width ? "-" + key.width*3: "-3";
if(key.type == "HOLDER")
continue;
buttons.push(
<button className={"button col" + col + " " + BUTTON_CLASSES[key.type]}
id={key.id}
eventkey={key.eventKey}
onClick={this.handleButton}
type={key.type}
subtype={key.subtype}>
{key.symbol}
</button>
);
}
rows.push(
<div className="row">{buttons}</div>
);
}
return(
<div className="keyPad container">
{rows}
</div>
)
}
}
KeyPad = connect(
(state, ownState) => {return state},
(dispatch, ownState)=>{
return{
input: (integer)=> {
dispatch({type: INPUT, integer: integer});
},
operation: (operation)=>{
dispatch({type: OPERATION, operation: operation});
},
equals: () => {
dispatch({type: EQUALS});
},
clear: () => {
dispatch({type: CLEAR});
}
}
}
)(KeyPad)
////// end keypad class
// App component section
class App extends React.Component{
constructor(props){
super(props)
this.state ={
}
this.inputHandler = this.inputHandler.bind(this)
}
inputHandler(event){
// console.log("observed change in parent input handler. " )
}
componentDidMount(prevProp, prevState){
// console.log("componentDidMount")
}
render(){
return (
<div className={"d-flex justify-content-center align-items-center outerDiv " + themes[this.props.theme]} >
<div className="innerDiv" >
<Header title="Calculator" />
<div className="row" >
<div className="col"><div className="textField">{this.props.register}</div></div>
</div>
<div className="row" >
<div className="col"><div className="textField" id="display">{this.props.input}</div></div>
</div>
<KeyPad />
</div>
</div>
)
}
}
App = connect(
function mapStateToProps(state, ownProps){
return state;
},
function mapDispatchToProps(dispatch){
return {
testFunction: (testInput) => {
}
}
}
)(App);
// End App component
render(
<Provider store={store}>
<App />
</Provider>
, document.getElementById('root'));
Also see: Tab Triggers