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.
This is the math supporting the concept of [this other Pen](https://codepen.io/CrocoDillon/pen/fBJxu) of mine ;-)
On the horizontal axis you have the ratio between 0 and 4, on the vertical axis you have the text area relative to the viewport area between 0 and 120%.
The green line is the ideal situation if you could just do `font-size: calc(10 * 1vw * 1vh)` and the text area is always 100% of the viewport area. Sadly you can’t do this.
The blue lines are poor attempts to match the green line as close as possible (`font-size: calc(5vw + 5vh)` and `font-size: 10vmin`) but they only match 100% for `ratio = 1`.
The red line is a combination of the two (`font-size: calc(4vw + 4vh + 2vmin)`) and is the best approach so far. This one even allows for tweaking of the actual numbers depending of what minimum and maximum ratio you want to support.
In those examples all scalars add up to 10 and that is the magic number that depends on how much how much text you want to fit, line height and the area relative to the viewport you want to fill.
canvas {
display: block;
margin: 1em auto;
}
var Graph = (function(document, undefined) {
function Graph(width, height) {
this.canvas = document.createElement('canvas');
this.canvas.width = width;
this.canvas.height = height;
this.context = this.canvas.getContext('2d');
this.functions = [];
this.domain = {lo: -10, hi: 10, range: 20};
this.codomain = {lo: -10, hi: 10, range: 20};
}
Graph.prototype.setDomain = function(x1, x2) {
this.domain.lo = x1;
this.domain.hi = x2;
this.domain.range = x2 - x1;
}
Graph.prototype.setCodomain = function(y1, y2) {
this.codomain.lo = y1;
this.codomain.hi = y2;
this.codomain.range = y2 - y1;
}
/**
* Adds a function definition to the graph, which can either be a
* literal function or an object with `function` defined, and
* optionally `color`, `thickness` and `step` defined too.
*
* @param f literal function or function definition object with
* optional parameters
* @return added function definition
*/
Graph.prototype.addFunction = function(f, color) {
if (typeof f === 'function') {
f = {
function: f,
color: color
};
}
f.color = f.color || '#000';
f.thickness = f.thickness || 1;
f.step = f.step > 0 ? f.step : this.domain.range / this.canvas.width;
this.functions.push(f);
return f;
};
Graph.prototype.removeFunction = function(f) {
var i = this.functions.indexOf(f);
if (i !== -1)
this.functions.splice(i, 1);
};
Graph.prototype.draw = function() {
var canvas = this.canvas,
context = this.context;
// clear canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// draw axes
var zero = this.pointToCanvas({x: 0, y: 0});
context.beginPath();
context.moveTo(0, zero.y);
context.lineTo(canvas.width, zero.y);
context.moveTo(zero.x, 0);
context.lineTo(zero.x, canvas.height);
context.lineWidth = 1;
context.strokeStyle = '#000';
context.stroke();
// draw functions
for (var i = 0; i < this.functions.length; i++) {
var functionDefinition = this.functions[i];
f = functionDefinition.function,
x = this.domain.lo,
y = f(x),
pt = this.pointToCanvas({x: x, y: y});
context.beginPath();
context.moveTo(pt.x, pt.y);
do {
x = x + functionDefinition.step;
y = f(x);
pt = this.pointToCanvas({x: x, y: y});
context.lineTo(pt.x, pt.y);
} while (x < this.domain.hi);
context.lineWidth = functionDefinition.thickness;
context.strokeStyle = functionDefinition.color;
context.stroke();
}
};
Graph.prototype.pointToCanvas = function(pt) {
return {
x: (pt.x - this.domain.lo) * this.canvas.width / this.domain.range,
y: (this.codomain.hi - pt.y) * this.canvas.height / this.codomain.range
};
};
return Graph;
})(document);
// assumptions
// screen has 10k pixels
// for ratio = 1, font-size: 10vw fills the screen
// graphs
function ideal(ratio) {
// this would be perfect but you can't use
// multiplication in calc with 2 units
// calc(10 * 1vw * 1vh)
var vw = Math.sqrt(ratio),
vh = Math.sqrt(1/ratio);
return Math.pow(10 * vw * vh, 2);
}
function onlyVmin(ratio) {
var vw = Math.sqrt(ratio),
vh = Math.sqrt(1/ratio),
vmin = Math.min(vw, vh);
return Math.pow(10 * vmin, 2);
}
function vwAndVh(ratio) {
var vw = Math.sqrt(ratio),
vh = Math.sqrt(1/ratio);
return Math.pow(5 * vw + 5 * vh, 2);
}
function all(ratio) {
var vw = Math.sqrt(ratio),
vh = Math.sqrt(1/ratio),
vmin = Math.min(vw, vh);
return Math.pow(4 * vw + 4 * vh + 2 * vmin, 2);
}
// draw them to a canvas graph!
var graph = new Graph(640, 640);
graph.setDomain(0, 4);
graph.setCodomain(0, 120);
graph.addFunction(ideal, '#2ecc71');
graph.addFunction(onlyVmin, '#3498db');
graph.addFunction(vwAndVh, '#3498db');
graph.addFunction({
function: all,
thickness: 2,
color: '#c0392b'
});
graph.draw();
document.body.appendChild(graph.canvas);
Also see: Tab Triggers