codegurukool.com · Free coding tutorials & online compiler

Home › CSS Tutorial › Units and variables

Units and variables

Size things with relative units and reuse values.

px is fixed. rem scales with the root font size, % with the parent, vw with the screen. Custom properties like --brand store values you reuse.

Example code

<style>
  :root{--brand:#e4572e}
  h2{font-size:2rem;color:var(--brand)}
  .half{width:50%;background:var(--brand);color:#fff;padding:1rem}
</style>
<h2>Units</h2>
<div class="half">50% wide</div>

Run this code in the online editor

← Backgrounds and bordersPosition →