Back to templates

Instant unit converter

A two-way length and temperature converter running in vanilla JavaScript.

Source code

1 lines · 1.3 KB
<style>*{box-sizing:border-box}body{margin:0;font-family:Inter,system-ui,sans-serif;background:#f6f7fb;color:#172033}button,a{font:inherit}.wrap{width:min(960px,calc(100% - 32px));margin:auto}</style><style>main{min-height:100vh;display:grid;place-items:center;padding:24px}.box{width:min(440px,100%);background:#fff;border:1px solid #e2e8f0;border-radius:18px;padding:28px}.row{display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-top:16px}label{display:block;font-size:13px;font-weight:700;margin-bottom:6px}input{width:100%;padding:11px;border:1px solid #cbd5e1;border-radius:9px;font:inherit}</style><main><div class="box"><h1 style="margin:0 0 6px">Unit Converter</h1><p style="margin:0;color:#64748b">Live Celsius ↔ Fahrenheit</p><div class="row"><div><label for="c">Celsius (°C)</label><input id="c" type="number" value="25" oninput="fromC()"></div><div><label for="f">Fahrenheit (°F)</label><input id="f" type="number" value="77" oninput="fromF()"></div></div></div></main><script>function fromC(){const v=parseFloat(document.getElementById("c").value);if(!isNaN(v))document.getElementById("f").value=(v*9/5+32).toFixed(1)}function fromF(){const v=parseFloat(document.getElementById("f").value);if(!isNaN(v))document.getElementById("c").value=((v-32)*5/9).toFixed(1)}</script>

When this template fits

A clean two-way converter demonstrating bidirectional input synchronization in under 40 lines of HTML, CSS, and JavaScript.

How the layout works

Two side-by-side numeric inputs update each other immediately on user input while preventing rounding feedback loops.

What to customize

Swap the formula for currency, pixels-to-rem, or length units, and show the exact conversion formula underneath for transparency.

Review before publishing

Test clearing either field, negative values (such as -40 °C = -40 °F), and decimal precision on small viewports.

Keep building with context

Compare a related pattern or read a practical guide before adapting this example in the Playground.