Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="row gap-5">
    <div class="col-12 my-5">
      <h1>CSS Units</h1>
      <pre class="language-css"><code>p {
  font-size: 16px;
}</code></pre>
      <p>Which means that element <code>p</code> has a property <code>font-size</code> with a value of <code>16px</code>, where <code>16</code> is the amount and <code>px</code> the unit.</p>
      <p>It is common to see the unit <code>px</code> but is it the best one for the job?</p>
    </div>
    <div class="col-12 my-5">
      <h2>Absolute vs Relative units</h2>
      <h3>Absolute units</h3>
      <p>Regardless the size of the medium used these units stay as they are in a fixed size. Works perfect for print outputs.</p>

      <table class="table">
        <caption>Examples of absolute units</caption>
        <thead>
          <tr>
            <th>Unit</th>
            <th>Name</th>
            <th>Equivalence</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>px</td>
            <td>pixels</td>
            <td>used to be 1 pixel of your screen. 1/96th of an inch</td>
          </tr>
          <tr>
            <td>pt</td>
            <td>points</td>
            <td>1/72th of an inch</td>
          </tr>
          <tr>
            <td>pc</td>
            <td>picas</td>
            <td>1/6th of an inch</td>
          </tr>
          <tr>
            <td>in</td>
            <td>inch</td>
            <td>equal to 2.54cm</td>
          </tr>
          <tr>
            <td>cm</td>
            <td>centimer</td>
            <td>1/100th of a meter</td>
          </tr>
          <tr>
            <td>mm</td>
            <td>milimeter</td>
            <td>1/1000th of a meter</td>
          </tr>
        </tbody>
      </table>
      <pre class="language-html"><code>&lt;p style="font-size: 20px"&gt;font-size of 20px&lt;p&gt;
&lt;p style="font-size: 20pt"&gt;font-size of 20pt&lt;/p&gt;
&lt;p style="font-size: 1pc"&gt;font-size of 1pc&lt;/p&gt;
&lt;p style="font-size: 0.5in"&gt;font-size of 0.5in&lt;/p>
&lt;p style="font-size: 1cm"&gt;font-size of 1cm&lt;/p&gt;
&lt;p style="font-size: 5mm"&gt;font-size of 5mm&lt;/p&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <p style="font-size: 20px">font-size of 20px</p>
        <p style="font-size: 20pt">font-size of 20pt</p>
        <p style="font-size: 1pc">font-size of 1pc</p>
        <p style="font-size: 0.5in">font-size of 0.5in</p>
        <p style="font-size: 1cm">font-size of 1cm</p>
        <p style="font-size: 5mm">font-size of 5mm</p>
      </div>
    </div>
    <div class="col-12 my-5">
      <h3>Relative units</h3>
      <p>Like the name states these units are relative to value of something else. </p>

      <table class="table">
        <caption>Examples of absolute units</caption>
        <thead>
          <tr>
            <th>Unit</th>
            <th>Relative to</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>%</td>
            <td>Relative to the parent element’s value for that property</td>
          </tr>
          <tr>
            <td>em</td>
            <td>Relative to the current font-size of the element</td>
          </tr>
          <tr>
            <td>rem</td>
            <td>Relative to the font-size of the root element, <code>HTML</code>. <em>root em<em></td>
          </tr>
          <tr>
            <td>ex</td>
            <td>Relative to the height of the lowercase letter `x` of the font family currently used.</td>
          </tr>
          <tr>
            <td>fr</td>
            <td>Fraction pf the available space in the grid container.</td>
          </tr>
          <tr>
            <td>ch</td>
            <td>Relative to the width of the number 0 of the font family currently used.</td>
          </tr>
          <tr>
            <td>vw</td>
            <td>Relative to the width of the viewport. <code>1vw</code> = 1% of viewport width.</td>
          </tr>
          <tr>
            <td>vh</td>
            <td>Relative to the height of the viewport. <code>1vh</code> = 1% of viewport height.</td>
          </tr>
          <tr>
            <td>vmin</td>
            <td>Relative to the smaller dimension of the viewport. On portrait orientation the width is smaller than the height. So width is used. <code>1vmin</code> = 1% of viewport smaller dimension.</td>
          </tr>
          <tr>
            <td>vmax</td>
            <td>Relative to the larger dimension of the viewport. On portrait orientation the height is larger than the width. So height is used. <code>1vmax</code> = 1% of viewport larger dimension.</td>
          </tr>
          <tr>
            <td>cqw</td>
            <td>Relative to the container query width. <code>1cqw</code> = 1% of container width.</td>
          </tr>
          <tr>
            <td>cqh</td>
            <td>Relative to the container query height. <code>1cqh</code> = 1% of container height.</td>
          </tr>
          <tr>
            <td>cqi</td>
            <td>Relative to the container query inline size. <code>1cqh</code> = 1% of container inline size.</td>
          </tr>
          <tr>
            <td>cqb</td>
            <td>Relative to the container query block size. <code>1cqh</code> = 1% of container block size.</td>
          </tr>
          <tr>
            <td>cqmin</td>
            <td>Relative to the container query smaller dimension of either <code>cqi</code> of <code>cqb</code>.</td>
          </tr>
          <tr>
            <td>cqmax</td>
            <td>Relative to the container query larger dimension of either <code>cqi</code> of <code>cqb</code>.</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="col-12 mt4">
      <h4>Unit: <code>%</code></h4>
      <pre class="language-html"><code>&lt;div class="parent" style="width: 400px;"&gt;
  &lt;div class="child" style="width: 25%;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="width: 400px;">
          <div class="child" style="width: 25%;">
            Sample text
          </div>
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="child" style="width: 25%;"&gt;
  Sample text
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="child" style="width: 25%;">
          Sample text
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>em</code></h4>
      <pre class="language-html"><code>&lt;div class="parent" style="font-size:30px"&gt;
  &lt;div class="child" style="width: 4em;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-size:30px">
          <div class="child" style="width: 4em;">
            Sample text
          </div>
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="parent" style="font-size:80px"&gt;
  &lt;div class="child" style="width: 4em;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-size:80px">
          <div class="child" style="width: 4em;">
            Sample text
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>rem</code></h4>
      <pre class="language-html"><code>&lt;div class="parent" style="font-size:40px"&gt;
  &lt;div class="child" style="width: 4rem;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-size:40px">
          <div class="child" style="width: 4rem;">
            Sample text
          </div>
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="parent" style="font-size:80px"&gt;
  &lt;div class="child" style="width: 4rem;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-size:80px">
          <div class="child" style="width: 4rem;">
            Sample text
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>ex</code></h4>
      <pre class="language-html"><code>&lt;div class="parent style="font-family: Sans-serif"&gt;
  &lt;div class="child" style="width: 40ex;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-family: Sans-serif">
          <div class="child" style="width: 40ex;">
            Sample text
          </div>
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="parent" style="font-family: monospace"&gt;
  &lt;div class="child" style="width: 40ex;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-family: monospace">
          <div class="child" style="width: 40ex;">
            Sample text
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>fr</code></h4>
      <pre class="language-html"><code>&lt;div class="parent style="display: grid; grid-template-columns: 1fr 1fr; grid-gap: 1em;&gt;
  &lt;div class="child"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="display: grid; grid-template-columns: 1fr 1fr; grid-gap: 1em;">
          <div class="child">
            Sample text
          </div>
          <div class="child">
            Sample text
          </div>
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="parent" style="display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 1em;"&gt;
  &lt;div class="child&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 1em;">
          <div class="child">
            Sample text
          </div>
          <div class="child">
            Sample text
          </div>
          <div class="child">
            Sample text
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>ch</code></h4>
      <p><a href="https://meyerweb.com/eric/thoughts/2012/05/15/defining-ch/">Eric Meyer about <code>ch</code></a></p>
      <pre class="language-html"><code>&lt;div class="parent style="font-family: Sans-serif"&gt;
  &lt;div class="child" style="width: 40ch;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-family: Sans-serif">
          <div class="child" style="width: 40ch;">
            Sample text
          </div>
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="parent" style="font-family: monospace"&gt;
  &lt;div class="child" style="width: 40ch;"&gt;
    Sample text
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="parent" style="font-family: monospace">
          <div class="child" style="width: 40ch;">
            Sample text
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>vw</code></h4>
      <pre class="language-html"><code>&lt;div class="child" style="width: 10vw;"&gt;
  Sample text
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="child" style="width: 10vw;">
          Sample text
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>vh</code></h4>
      <pre class="language-html"><code>&lt;div class="child" style="width: 50vh;"&gt;
  Sample text
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="child" style="width: 50vh;">
          Sample text
        </div>
      </div>
      <pre class="language-html"><code>&lt;div class="child" style="height: 50vh;"&gt;
  Sample text
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="child" style="height: 50vh;">
          Sample text
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>vmin</code></h4>
      <p><a href="https://css-tricks.com/simple-little-use-case-vmin/">Use case of <code>vmin</code> on CSS Tricks</a></p>
      <pre class="language-html"><code>&lt;div class="child" style="width: 40vmin;"&gt;
  Sample text
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="child" style="width: 40vmin;">
          Sample text
        </div>
      </div>
    </div>

    <div class="col-12 my-5">
      <h4>Unit: <code>vmax</code></h4>
      <pre class="language-html"><code>&lt;div class="child" style="width: 40vmax;"&gt;
  Sample text
&lt;/div&gt;</code></pre>
      <div data-sample="demo" class="sample">
        <div class="child" style="width: 40vmax;">
          Sample text
        </div>
      </div>
    </div>

  </div>
</div>
              
            
!

CSS

              
                /** 
 * Sample block
 */
[data-sample],
.sample {
  border: 4px solid var(--bs-blue);
  padding: 0.5rem;
  margin-block-start: 1rem;
  position: relative;
  overflow: hidden;
  resize: horizontal;
}

.parent {
  background-color: var(--bs-primary);
}
.child {
  background-color: var(--bs-warning);
}

              
            
!

JS

              
                
              
            
!
999px

Console