Volume, with its unit system
HTML for this example
Select the code and use your copy shortcut. Changes in this preview are temporary.
<!doctype html>
<html lang="en-US"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Volume, with its unit system</title><style>*{box-sizing:border-box}body{margin:0;background:#f3f4f2;color:#202c32;font:16px/1.5 system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}main{max-width:1120px;margin:auto;padding:30px 28px 60px}header{margin-bottom:22px}.eyebrow{font-size:12px;letter-spacing:.14em;text-transform:uppercase;font-weight:750;color:#59676b}h1{font-size:clamp(25px,4vw,36px);line-height:1.15;letter-spacing:-.035em;margin:7px 0 10px}h2{font-size:21px;line-height:1.3;margin:0 0 16px}h3{font-size:17px;margin:0 0 10px}p{margin:8px 0 16px}label{display:block;font-size:14px;font-weight:650;margin:12px 0 5px}input,select,textarea,button{font:inherit}input,select,textarea{width:100%;min-width:0;border:1px solid #b6c0bf;border-radius:7px;background:#fff;color:#202c32;padding:10px 12px}textarea{resize:vertical;min-height:88px}input[type=checkbox]{width:18px;height:18px;vertical-align:middle;margin-right:8px}button{border:1px solid #b8c3c1;border-radius:7px;padding:10px 15px;background:#fff;color:#203632;font-weight:650;cursor:pointer}button.primary{background:#243f3c;color:white;border-color:#243f3c}button:hover{filter:brightness(.95)}button:disabled{opacity:.48;cursor:not-allowed}:focus-visible{outline:3px solid #b05814;outline-offset:3px}.buttons{display:flex;gap:8px;flex-wrap:wrap;margin-top:18px}.panel{background:#fff;border:1px solid #d7ddda;border-radius:12px;padding:24px}.split{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.1fr);gap:22px}.row{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px}.result{background:#e4eee8;border:1px solid #c5d6ca;border-radius:12px;padding:28px}.big{font-size:clamp(32px,5vw,56px);font-weight:740;line-height:1.14;letter-spacing:-.04em;overflow-wrap:anywhere;font-variant-numeric:tabular-nums}.muted{color:#59686a;font-size:14px}.tag{display:inline-block;background:#edf0ed;border-radius:4px;padding:3px 8px;font-size:12px;font-weight:700}.error{color:#9d301b;font-weight:650;min-height:24px;margin-top:10px}.note{border-left:3px solid #9a794a;padding:10px 15px;background:#faf5e9;margin:18px 0}.below{margin-top:28px}.record{white-space:pre-wrap;overflow-wrap:anywhere;font:14px/1.65 ui-monospace,SFMono-Regular,Consolas,monospace;background:#f2f4f1;padding:18px;border-radius:8px}table{width:100%;border-collapse:collapse;font-size:14px}th,td{text-align:left;vertical-align:top;border-bottom:1px solid #dce1dd;padding:12px 10px;overflow-wrap:anywhere}th{color:#59686a;font-size:12px}a{color:#2c5a6a;overflow-wrap:anywhere}details{margin-top:20px}summary{cursor:pointer;font-weight:650}hr{border:0;border-top:1px solid #d6dcd6;margin:22px 0}[hidden]{display:none!important}ol,ul{padding-left:23px}.statline{display:flex;gap:20px;flex-wrap:wrap;margin:18px 0}.statline strong{display:block;font-size:24px}.inline{display:flex;align-items:center;gap:8px}.inline label{margin:0} .blank{min-height:36px;border-bottom:1px dashed #aebbb5;color:#66736d;font-size:13px;padding:8px 0}@media(max-width:720px){main{padding:22px 18px 45px}.split{grid-template-columns:1fr}.panel,.result{padding:20px}.row{gap:12px}th,td{padding:10px 6px}}@media(max-width:400px){main{padding:18px 14px 40px}.big{font-size:34px}button{padding:10px 12px}.row{grid-template-columns:1fr}.panel,.result{padding:17px}}.inline{flex-wrap:wrap}input[type=range]{padding:0}pre{white-space:pre-wrap;overflow-wrap:anywhere;margin:0}.metric{font-size:42px;font-weight:750;line-height:1.15;letter-spacing:-.035em}.caption{font-size:13px;color:#55656b}.readout{font:15px/1.6 ui-monospace,Consolas,monospace;white-space:pre-wrap;overflow-wrap:anywhere}.strip{display:flex;gap:24px;align-items:center;flex-wrap:wrap}.chip{padding:5px 10px;background:#e5eaf0;border-radius:5px;font-size:13px}button[aria-pressed=true]{background:#263b50;color:white}.subtle{background:#f6f7f7}.fileCode{min-height:230px;font:14px/1.6 ui-monospace,Consolas,monospace}.wideButton{width:100%}.visuallyHidden{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}svg{max-width:100%}.nowrap{white-space:nowrap}.volumeFields{display:grid;grid-template-columns:.8fr 1fr 1fr;gap:18px}.gallons{display:grid;grid-template-columns:1fr 1fr;gap:14px 30px;border-top:2px solid #657e8b;padding:20px 0}.gallons p{grid-column:1/-1;margin:0;color:#4d6571}.gallons h2{font-size:28px;margin:8px 0}.result{background:#e2edf1;border-color:#c6d9df}@media(max-width:650px){.volumeFields{grid-template-columns:1fr}.gallons{grid-template-columns:1fr}.gallons h2{font-size:25px}}</style></head><body><main><header><div class="eyebrow">Supplied volume · Five explicit unit identities</div><h1>Volume, with its unit system</h1></header>
<section class="result"><span class="eyebrow">Conversion result</span><div id="answer" class="big">≈ 7.570824 L</div><p id="equation">2 U.S. liquid gallons × 3.785412 L/gallon</p></section>
<form class="panel below" id="form" novalidate><div class="volumeFields"><div><label for="quantity">Supplied volume</label><input id="quantity" value="2" inputmode="decimal"><div class="caption" id="origin">Manually supplied input</div></div><div><label for="from">From</label><select id="from"></select></div><div><label for="to">To</label><select id="to"></select></div></div><div class="buttons"><button type="submit" class="primary">Calculate</button><button type="button" id="swap">Swap using unrounded result</button><button type="button" id="reset">Reset</button></div><p class="error" id="feedback" role="status"></p></form>
<section class="gallons below"><div><span class="eyebrow">2 U.S. liquid gallons</span><h2>7.570824 L</h2></div><div><span class="eyebrow">2 Imperial gallons</span><h2>9.09218 L</h2></div><p>Difference: 1.521356 L from the declared unit definitions.</p></section>
<section class="panel below"><h2>Factor and input record</h2><pre class="record" id="record"></pre><p class="muted">NIST printed factors into L: mL 0.001; L 1; m³ 1000; U.S. liquid gallon 3.785412; Imperial gallon 4.54609. The U.S. factor is rounded as printed. Displays use at most six decimals, with scientific notation for tiny positive values.</p><p class="muted">Manual values: 0–1,000,000, up to six decimal places. Generic “gal” needs clarification. Cups, ounces, dry units and mass are unsupported. This converts supplied volume, not dimensions, contents or certified capacity.</p><a href="https://www.nist.gov/pml/special-publication-811/nist-guide-si-appendix-b-conversion-factors/nist-guide-si-appendix-b9">NIST volume factors</a></section><footer class="muted below">Local working copy · Changes are not saved after this page closes.</footer></main><script data-app>
(()=>{
const $=id=>document.getElementById(id);const text=(id,v)=>$(id).textContent=String(v);const on=(id,type,fn)=>$(id).addEventListener(type,fn);const escapeHTML=s=>String(s).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));const decimal=s=>{const raw=String(s).trim();if(!/^[0-9]+(?:[.][0-9]{1,6})?$/.test(raw)||!Number.isFinite(+raw)||+raw>1000000)throw Error('Use a number from 0 to 1,000,000 with at most six decimal places. Blank is missing.');return +raw};const display=n=>n>0&&n<0.000001?n.toExponential(6):Number(n.toFixed(6)).toString();const download=(name,content,type='text/plain')=>{const url=URL.createObjectURL(new Blob([content],{type})),a=document.createElement('a');a.href=url;a.download=name;document.body.append(a);a.click();a.remove();setTimeout(()=>URL.revokeObjectURL(url),1000)};
const units={ml:{name:'Milliliter (mL)',short:'mL',factor:.001},l:{name:'Liter (L)',short:'L',factor:1},m3:{name:'Cubic meter (m³)',short:'m³',factor:1000},us:{name:'U.S. liquid gallon',short:'U.S. liquid gallons',factor:3.785412},imp:{name:'Imperial gallon',short:'Imperial gallons',factor:4.54609}};let last=null,internal=null;
for(const id of ['from','to'])$(''+id).innerHTML='<option value="">Choose a defined unit</option>'+Object.entries(units).map(([key,u])=>'<option value="'+key+'">'+u.name+'</option>').join('');$('from').value='us';$('to').value='l';
function convert(value,from,to){if(!Object.hasOwn(units,from)||!Object.hasOwn(units,to))throw Error('Select explicit supported units; an unspecified gallon cannot be converted.');if(!Number.isFinite(value)||value<0||value>1000000)throw Error('Source quantity must be finite and between 0 and 1,000,000.');return {value,from,to,result:from===to?value:value*units[from].factor/units[to].factor}}
function clear(){last=null;internal=null;['answer','equation','record'].forEach(id=>text(id,'—'));$('swap').disabled=true;text('origin','Manually supplied input');text('feedback','Inputs changed. Calculate to update.')}
function show(r,raw,origin){last=r;$('swap').disabled=false;text('answer','≈ '+display(r.result)+' '+units[r.to].short);text('equation',r.value+' '+units[r.from].name+' → '+units[r.to].name);text('record','Raw input: '+JSON.stringify(raw)+'\nInput origin: '+origin+'\nParsed quantity: '+r.value+'\nFrom ID: '+r.from+' — '+units[r.from].name+'\nTo ID: '+r.to+' — '+units[r.to].name+'\nFactors into L: '+units[r.from].factor+' and '+units[r.to].factor+'\nCalculation: '+r.value+' × '+units[r.from].factor+' ÷ '+units[r.to].factor+'\nUnrounded result: '+r.result+' '+units[r.to].short+'\nDisplayed: ≈ '+display(r.result)+' '+units[r.to].short+'\nFactors are the NIST printed values; display rounding is separate.');text('feedback','Converted using the declared factor table.')}
function calculate(){const candidate=internal;try{last=null;$('swap').disabled=true;['answer','equation','record'].forEach(id=>text(id,'—'));const q=candidate===null?decimal($('quantity').value):candidate;show(convert(q,$('from').value,$('to').value),$('quantity').value,candidate===null?'manually supplied':'Taken from previous calculation')}catch(e){text('feedback',e.message)}}
function swap(){if(!last)return;const r=last;try{const candidate=convert(r.result,r.to,r.from);internal=r.result;$('quantity').value=String(r.result);$('from').value=r.to;$('to').value=r.from;text('origin','Taken from previous calculation');show(candidate,String(r.result),'Taken from previous calculation')}catch(e){text('feedback','Swap held: '+e.message)}}
on('form','submit',e=>{e.preventDefault();calculate()});['quantity','from','to'].forEach(id=>on(id,'input',clear));on('swap','click',swap);on('reset','click',()=>{['quantity','from','to'].forEach(id=>$(id).value='');clear();text('feedback','');$('quantity').focus()});window.app={convert,calculate,swap,getLast:()=>last};calculate();
})();
</script>
<script>
// Local calculation controls: no native form submission or sandbox permissions required.
(()=>{const form=document.getElementById('form');if(!form)throw Error('Calculation form missing');const dispatch=()=>form.dispatchEvent(new Event('submit',{bubbles:true,cancelable:true}));const buttons=[...form.querySelectorAll('button')].filter(button=>button.type==='submit');if(buttons.length!==1)throw Error('Expected one calculation control');for(const button of buttons){button.type='button';button.addEventListener('click',dispatch);}form.addEventListener('keydown',event=>{if(event.key==='Enter'&&!event.isComposing&&['INPUT','SELECT'].includes(event.target.tagName)){event.preventDefault();dispatch();}});})();
</script>
</body></html>