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 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.
@mixin select($opt)
-webkit-touch-callout: #{$opt}
-webkit-user-select: #{$opt}
-khtml-user-select: #{$opt}
-moz-user-select: #{$opt}
-ms-user-select: #{$opt}
user-select: #{$opt}
*
// border: solid 0.5px
position: relative
box-sizing: border-box
font-family: Dosis
+select(none)
text-align: center
font-weight: 600
font-size: 1.02em
body,html
height: 100vh
margin: 0
overflow: hidden
.app
width: 38%
position: absolute
left: 50%
top: 50%
transform: translate(-50%,-50%)
background-color: rgba(255,255,255,0.1)
box-shadow: 0 0 10px 5px rgba(0,0,0,0.15)
& .input
& .prev
margin: 0
padding-right: 1em
height: 3em
line-height: 3em
text-align: right
& .calc
width: 80%
height: 1.5em
line-height: 1.5em
padding: 0.5em
padding-top: 0
margin: 0 auto
font-size: 3em
overflow: hidden
border-bottom: dashed 2px
font-weight: 500
font-family: Concert One
+select(auto)
& .evals
display: flex
width: 100%
& .key
width: 33.3%
& .box
width: 100%
display: flex
flex-wrap: wrap
& .nums
width: 80%
display: flex
flex-wrap: wrap
& .key
width: 33.3%
& .operators
width: 20%
display: flex
flex-direction: column
& .key
width: 100%
& .color
width: 100%
padding: 0.5em
& .bgc
border: solid 1px
display: inline-block
width: 43%
padding: 0.2em
+select(auto)
& .copy
padding: 0.2em
border: solid 1px
display: inline-block
width: 25%
cursor: pointer
background-color: #fff
& .toggle
display: inline-block
width: 18%
text-align: left
cursor: pointer
// border-left: solid
padding: 0.2em
& span
.key
height: 4em
cursor: pointer
line-height: 4.5em
&:hover
color: SkyBlue
@media screen and (max-width:480px)
body,html
height: 91vh
.app
width: 90%
.key
height: 3.5em
line-height: 3.5em
.color
margin: 0.5em
.app .input .calc
height: 1.7em
line-height: 1.7em
//components
const Input = React.createClass({
render: function(){
return (
<div className="input">
<p className="prev">{this.props.prevString}</p>
<p className="calc">{this.props.calcString}</p>
</div>
);
}
});
const Key = React.createClass({
render: function(){
return <div className="key" onClick={this.props.onClick}>{this.props.content}</div>;
}
});
const Nums = React.createClass({
render: function(){
return (
<div className ="nums">
<Key content = "1" onClick = {this.props.Input}/>
<Key content = "2" onClick = {this.props.Input}/>
<Key content = "3" onClick = {this.props.Input}/>
<Key content = "4" onClick = {this.props.Input}/>
<Key content = "5" onClick ={this.props.Input}/>
<Key content = "6" onClick = {this.props.Input}/>
<Key content = "7" onClick = {this.props.Input}/>
<Key content = "8" onClick = {this.props.Input}/>
<Key content = "9" onClick ={this.props.Input}/>
<Key content = "0" onClick = {this.props.Input}/>
<Key content = "." onClick = {this.props.Input}/>
<Key content = "00" onClick = {this.props.Input}/>
</div>
);
}
});
const Operators = React.createClass({
render: function(){
return (
<div className = "operators">
<Key content = "+" onClick = {this.props.Input}/>
<Key content = "-" onClick = {this.props.Input}/>
<Key content = "*" onClick = {this.props.Input}/>
<Key content = "/" onClick = {this.props.Input}/>
</div>
);
}
});
const Evals = React.createClass({
render: function(){
return (
<div className = "evals">
<Key content = "C" onClick = {this.props.clear}/>
<Key content = "DEL" onClick = {this.props.del}/>
<Key content = "=" onClick = {this.props.calc}/>
</div>
);
}
});
const Toggle = React.createClass({
render: function(){
return (<span className = "toggle" onClick = {this.props.onClick}>{this.props.colorType}</span>);
}
});
const Color = React.createClass({
render: function(){
return (
<p className = "color">
<Toggle colorType = {this.props.colorType} onClick = {this.props.toggle}/>
<span className = "bgc">{this.props.color}</span>
<span className = "copy" style={{color:this.props.color, backgroundColor: this.props.textColor, borderColor: this.props.textColor}} onClick={this.props.copy}>COPY</span>
</p>
);
}
});
const Keyboard = React.createClass({
render: function(){
return (
<div className = "keyborad">
<Evals clear={this.props.clear} del={this.props.del} calc = {this.props.calc}/>
<div className = "box">
<Nums Input = {this.props.Input}/>
<Operators Input = {this.props.Input}/>
<Color colorType = {this.props.colorType} color = {this.props.color} textColor = {this.props.textColor} copy = {this.props.copy} toggle = {this.props.toggle}/>
</div>
</div>
);
}
});
// putting together
const App = React.createClass({
getInitialState: function(){
return {
prevString: "",
calcString: "",
color:"#2abacd",
textColor:"#fff",
colorType: "HEX ▸"
};
},
// Methods
Input: function(e){
let string = this.state.calcString + e.target.innerHTML;
this.setState({calcString : string});
this.shine();
},
clear: function(){
this.setState({
calcString:"",
prevString:""});
}
,
del: function(){
let string = this.state.calcString.slice(0,-1);
this.setState({calcString:string});
},
calc: function(){
let result = eval(this.state.calcString),
prev = this.state.calcString;
this.setState({calcString: result, prevString: prev});
},
hexToRgb: function(hex) {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
,
rgbToHex: function(color) {
if (color.charAt(0) === "#") {
return color; }
let nums = /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/i.exec(color),
r = parseInt(nums[2], 10).toString(16),
g = parseInt(nums[3], 10).toString(16),
b = parseInt(nums[4], 10).toString(16);
return "#"+ ( (r.length == 1 ? "0"+ r : r) + (g.length == 1 ? "0"+ g : g) + (b.length == 1 ? "0"+ b : b) );
},
shine: function(){
let body = document.querySelector('body'),
color = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6),
rgb = this.hexToRgb(color), rgbColor = `rgb(${rgb.r},${rgb.g},${rgb.b})`,
colorType=this.state.colorType;
let o = Math.round(((rgb.r * 299) + (rgb.g * 587) + (rgb.b * 114)) / 1000);
(o > 180) ? this.state.textColor="#594741": this.state.textColor="#fff";
colorType == "HEX ▸" ?
this.setState({color: color}):this.setState({color: rgbColor})
},
copy: function(){
let copyArea = document.querySelector('.bgc'),
range = document.createRange();
range.selectNode(copyArea);
window.getSelection().addRange(range);
document.execCommand("Copy");
},
toggle: function(){
let color = this.state.color,
colorType = this.state.colorType;
if(colorType == "HEX ▸"){
let rgb = this.hexToRgb(color),
rgbColor = `rgb(${rgb.r},${rgb.g},${rgb.b})`;
this.setState({colorType:"◂ RGB",color:rgbColor});
}else{
let hexColor = this.rgbToHex(color);
this.setState({colorType:"HEX ▸",color:hexColor});
}
},
// Render app
render: function(){
document.querySelector('body').style.backgroundColor=this.state.color; document.querySelector('body').style.color=this.state.textColor;
return (
<div className="app">
<Input prevString={this.state.prevString} calcString={this.state.calcString}/>
<Keyboard Input = {this.Input} colorType = {this.state.colorType} color = {this.state.color} textColor = {this.state.textColor} copy = {this.copy} toggle = {this.toggle} calc={this.calc} del={this.del} clear = {this.clear}/>
</div>
);
}
});
ReactDOM.render(<App/>,document.querySelector('body'));
//learning redux to improve code
Also see: Tab Triggers