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.
:root {
--font-code: ui-monospace, "SF Mono", Menlo, Monaco, "Cascadia Mono",
"Segoe UI Mono", "Roboto Mono", "Consolas", "Fira Code", "Oxygen Mono",
"Ubuntu Monospace", "Source Code Pro", "Fira Mono", "Droid Sans Mono",
"Courier New", monospace;
--side-padding: max(0px, calc((100vw - 432px) / 256));
--casing: #000;
--gray: #e8eaed;
--cyan: #0ff;
--magenta: #f0f;
--yellow: #ff0;
--red: #f00;
--green: #0f0;
--blue: #00f;
@supports (color: color(display-p3 1 1 1)) {
--cyan: color(display-p3 0 1 1);
--magenta: color(display-p3 1 0 1);
--yellow: color(display-p3 1 1 0);
--red: color(display-p3 1 0 0);
--green: color(display-p3 0 1 0);
--blue: color(display-p3 0 0 1);
}
}
* {
box-sizing: border-box;
}
html {
font-size: 14px;
@media only screen and (min-width: 832px) {
font-size: unset;
}
}
html,
body {
height: 100%;
width: 100%;
background: #fff;
@media (prefers-color-scheme: dark) {
background: #0f0f0f;
}
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
@media screen and (max-width: 832px) {
align-items: unset;
}
}
.widget {
position: relative;
font-family: var(--font-code);
border: 0.25rem solid var(--casing);
border-bottom-width: 0;
background: var(--casing);
color: #fff;
line-height: 2rem;
min-width: fit-content;
font-size: 1.25rem;
@media screen and (max-width: 832px) {
width: 100%;
}
}
.precision {
position: absolute;
top: 0.25rem;
right: 0.25rem;
background: var(--gray);
color: #000;
padding: 0 var(--side-padding);
height: 1.5rem;
font-size: 1rem;
font-weight: 500;
line-height: 1.5rem;
}
input {
border: none;
outline: none;
width: 100%;
height: 2rem;
font-family: var(--font-code);
padding: 0 max(0.25rem, var(--side-padding));
}
.scientific-notation {
line-height: 2.25rem;
white-space: nowrap;
}
.bit-array {
display: flex;
}
.bit {
transition: background 200ms ease, color 200ms ease;
flex-grow: 1;
font-family: var(--font-code);
height: 2rem;
line-height: 2rem;
border: none;
border-bottom: 0.375rem solid;
padding: 0 var(--side-padding);
cursor: pointer;
box-sizing: content-box;
@media screen and (max-width: 832px) {
padding: 0;
}
&--0 {
background: var(--casing);
color: #fff;
&::after {
content: "0";
}
&:hover {
background: #555;
}
&:active {
background: #666;
}
}
&--1 {
background: #fff;
color: var(--casing);
&::after {
content: "1";
}
&:hover {
background: #ddd;
}
&:active {
background: #ccc;
}
}
&--sign {
border-color: var(--cyan);
}
&--exponent {
border-color: var(--magenta);
}
&--significand {
border-color: var(--yellow);
}
}
.sign {
color: var(--cyan);
}
.exponent {
color: var(--magenta);
}
.significand {
cursor: pointer;
}
.significand-fraction {
color: var(--yellow);
}
.legend {
display: flex;
height: 1.625rem;
line-height: 1.5rem;
> * {
display: inline-block;
color: var(--casing);
padding: 0 var(--side-padding);
}
.sign {
background: var(--cyan);
}
.exponent {
background: var(--magenta);
}
.significand {
flex-grow: 1;
background: var(--yellow);
}
}
.suggestions {
height: 2.25rem;
}
.shortcut {
transition: background 200ms ease, color 200ms ease;
font-family: var(--font-code);
height: inherit;
color: #fff;
text-decoration: underline;
background: none;
border: none;
padding: 0;
margin: 0;
cursor: pointer;
}
import * as React from "https://cdn.skypack.dev/[email protected]";
import * as ReactDOM from "https://cdn.skypack.dev/[email protected]";
import { Decimal } from "https://cdn.skypack.dev/[email protected]";
const INITIAL_VALUE = 10;
const SUGGESTIONS = [1, 0.2, Infinity, NaN, -0, 0.3333333333333333, 8e-323];
function toggleBit(buffer, position) {
const newValue = buffer.slice(0);
const bytes = new Uint8Array(newValue);
const reversePosition = 7 - (position % 8);
bytes[bytes.byteLength - 1 - Math.floor(position / 8)] ^=
1 << reversePosition;
return newValue;
}
function updateFloat(buffer, value) {
const newValue = buffer.slice(0);
const floats = new Float64Array(newValue);
floats[0] = value;
return newValue;
}
function extractBit(bytes, position) {
const byte = bytes[bytes.byteLength - 1 - Math.floor(position / 8)];
const reversePosition = 7 - (position % 8);
return byte & (1 << reversePosition) ? 1 : 0;
}
function extractBitString(bytes, position, length) {
let integerString = "";
for (let i = bytes.byteLength - 1; i >= 0; i--) {
integerString += bytes[i].toString(2).padStart(8, "0");
}
return integerString.slice(position, position + length);
}
function Float() {
const [buffer, setBuffer] = React.useState(new ArrayBuffer(8));
const [fractionMode, setFractionMode] = React.useState(false);
const [showRawSignificand, setShowRawSignificand] = React.useState(false);
const bytes = new Uint8Array(buffer);
const floats = new Float64Array(buffer);
const float = floats[0];
const floatRepresentation =
(Object.is(float, -0) ? "-0" : float) +
(isFinite(float) && fractionMode ? "." : "");
const significandBitString = extractBitString(bytes, 12, 52);
let significandFractionRepresentation;
if (showRawSignificand) {
significandFractionRepresentation = significandBitString.replace(
/0+$/,
""
);
} else {
const significandFractionDecimal = new Decimal(
`0b0.${significandBitString}`
);
significandFractionRepresentation = significandFractionDecimal.isZero()
? "0"
: significandFractionDecimal
.toFixed(21)
.slice(2)
.replace(/0+$/, "");
}
const rawBiasedExponent = parseInt(extractBitString(bytes, 1, 11), 2);
const biasedExponent = rawBiasedExponent || 1;
React.useEffect(() => setBuffer(updateFloat(buffer, INITIAL_VALUE)), []);
return (
<div className="widget">
<input
type="text"
value={floatRepresentation}
onChange={(e) => {
const { value } = e.target;
if (!isNaN(value)) {
setFractionMode(value.endsWith("."));
setBuffer(updateFloat(buffer, value));
}
}}
/>
<div className="precision">double precision</div>
<div className="scientific-notation">
(-1)^
<span className="sign">
{extractBitString(bytes, 0, 1)}
</span> *{" "}
<span
className="significand"
onClick={() => setShowRawSignificand((state) => !state)}
title={`Click to show the significand's ${
showRawSignificand
? "decimal representation"
: "raw binary value"
}`}
>
{rawBiasedExponent ? (
<>1</>
) : (
<span className="exponent">0</span>
)}
.
<span className="significand-fraction">
{significandFractionRepresentation}
{showRawSignificand ? <sub>2</sub> : ""}
</span>
</span>{" "}
* 2^(
<span className="exponent">{biasedExponent}</span>
-1023)
</div>
<div className="bit-array">
{Object.keys(Array(64).fill()).map((i) => (
<button
className={`bit bit--${extractBit(bytes, i)} bit--${
i == 0
? "sign"
: i < 12
? "exponent"
: "significand"
}`}
type="button"
onClick={() => setBuffer(toggleBit(buffer, i))}
title={`Bit ${i}`}
/>
))}
</div>
<div className="legend">
<span className="sign">sign</span>
<span className="exponent">exponent</span>
<span className="significand">significand/mantissa</span>
</div>
<div className="suggestions">
<i>try these:</i>
{SUGGESTIONS.map((suggestedValue) => (
<>
{" "}
<button
key={suggestedValue}
className="shortcut"
onClick={() =>
setBuffer(updateFloat(buffer, suggestedValue))
}
>
{Object.is(suggestedValue, -0)
? "-0"
: suggestedValue}
</button>
</>
))}
</div>
</div>
);
}
ReactDOM.render(<Float />, document.body);
Also see: Tab Triggers