{"route":"/en-US-v0.14.1/reference/layout/relative/","title":"Relative Length","description":"Documentation for the Relative Length type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"relative-to-the-page","name":"Relative to the page","children":[]},{"id":"relative-to-a-container","name":"Relative to a container","children":[]},{"id":"scripting","name":"Scripting","children":[]}],"body":{"kind":"type","content":{"name":"relative","title":"Relative Length","keywords":[],"oneliner":"A length in relation to some known length.","details":"<p>A length in relation to some known length.</p>\n<p>This type is a combination of a <a href=\"/en-US-v0.14.1/reference/layout/length/\" title=\"length\">length</a> with a <a href=\"/en-US-v0.14.1/reference/layout/ratio/\" title=\"ratio\">ratio</a>. It results from\naddition and subtraction of a length and a ratio. Wherever a relative length\nis expected, you can also use a bare length or ratio.</p>\n<h2 id=\"relative-to-the-page\">Relative to the page</h2>\n<p>A common use case is setting the width or height of a layout element (e.g.,\n<a href=\"/en-US-v0.14.1/reference/layout/block/\" title=\"block\">block</a>, <a href=\"/en-US-v0.14.1/reference/visualize/rect/\" title=\"rect\">rect</a>, etc.) as a certain percentage of the width of the page.\nHere, the rectangle's width is set to <code><span class=\"typ-num\">25%</span></code>, so it takes up one fourth of\nthe page's <em>inner</em> width (the width minus margins).</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">rect</span><span class=\"typ-punct\">(</span>width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">25%</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/4d00e48a14d2fa6bbfde5bd85623a199.png\" alt=\"Preview\"></div></div>\n<p>Bare lengths or ratios are always valid where relative lengths are expected,\nbut the two can also be freely mixed:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">rect</span><span class=\"typ-punct\">(</span>width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">25%</span> <span class=\"typ-op\">+</span> <span class=\"typ-num\">1cm</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/9dd66f878a23031069f8f03df39872fa.png\" alt=\"Preview\"></div></div>\n<p>If you're trying to size an element so that it takes up the page's <em>full</em>\nwidth, you have a few options (this highly depends on your exact use case):</p>\n<ol>\n<li>Set page margins to <code><span class=\"typ-num\">0pt</span></code> (<code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">page</span><span class=\"typ-punct\">(</span>margin<span class=\"typ-punct\">:</span> <span class=\"typ-num\">0pt</span><span class=\"typ-punct\">)</span></code>)</li>\n<li>Multiply the ratio by the known full page width (<code><span class=\"typ-num\">21cm</span> <span class=\"typ-op\">*</span> <span class=\"typ-num\">69%</span></code>)</li>\n<li>Use padding which will negate the margins (<code><span class=\"typ-func\">#</span><span class=\"typ-func\">pad</span><span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">:</span> <span class=\"typ-op\">-</span><span class=\"typ-num\">2.5cm</span><span class=\"typ-punct\">,</span> <span class=\"typ-op\">..</span><span class=\"typ-punct\">.</span><span class=\"typ-punct\">)</span></code>)</li>\n<li>Use the page <a href=\"/en-US-v0.14.1/reference/layout/page/#parameters-background\">background</a> or\n<a href=\"/en-US-v0.14.1/reference/layout/page/#parameters-foreground\">foreground</a> field as those don't take margins into\naccount (note that it will render the content outside of the document\nflow, see <a href=\"/en-US-v0.14.1/reference/layout/place/\" title=\"place\">place</a> to control the content position)</li>\n</ol>\n<h2 id=\"relative-to-a-container\">Relative to a container</h2>\n<p>When a layout element (e.g. a <a href=\"/en-US-v0.14.1/reference/visualize/rect/\" title=\"rect\">rect</a>) is nested in another layout container\n(e.g. a <a href=\"/en-US-v0.14.1/reference/layout/block/\" title=\"block\">block</a>) instead of being a direct descendant of the page, relative\nwidths become relative to the container:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">block</span><span class=\"typ-punct\">(</span>\n  width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">100pt</span><span class=\"typ-punct\">,</span>\n  fill<span class=\"typ-punct\">:</span> aqua<span class=\"typ-punct\">,</span>\n  <span class=\"typ-func\">rect</span><span class=\"typ-punct\">(</span>width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">50%</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/402d58027e654e20b23526448886fb40.png\" alt=\"Preview\"></div></div>\n<h2 id=\"scripting\">Scripting</h2>\n<p>You can multiply relative lengths by <a href=\"/en-US-v0.14.1/reference/layout/ratio/\">ratios</a>, <a href=\"/en-US-v0.14.1/reference/foundations/int/\">integers</a>, and\n<a href=\"/en-US-v0.14.1/reference/foundations/float/\">floats</a>.</p>\n<p>A relative length has the following fields:</p>\n<ul>\n<li><code>length</code>: Its <a href=\"/en-US-v0.14.1/reference/layout/length/\" title=\"length\">length</a> component.</li>\n<li><code>ratio</code>: Its <a href=\"/en-US-v0.14.1/reference/layout/ratio/\" title=\"ratio\">ratio</a> component.</li>\n</ul>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">100%</span> <span class=\"typ-op\">-</span> <span class=\"typ-num\">50pt</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">length</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">100%</span> <span class=\"typ-op\">-</span> <span class=\"typ-num\">50pt</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">ratio</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/d770a260705b27902b61e33e8c0a5f28.png\" alt=\"Preview\"></div></div>","constructor":null,"scope":[]}}}