{"id":4908,"date":"2025-01-08T10:22:38","date_gmt":"2025-01-08T15:22:38","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=4908"},"modified":"2025-01-08T10:22:39","modified_gmt":"2025-01-08T15:22:39","slug":"sharing-a-variable-across-html-css-and-javascript","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/sharing-a-variable-across-html-css-and-javascript\/","title":{"rendered":"Sharing a Variable Across HTML, CSS, and JavaScript"},"content":{"rendered":"\n<p>My kid is in a little phase where word search puzzles are really fun. She likes doing them, and then possibly because we share blood, she immediately started to want to <em>make<\/em> them. I figured it would be a fun little recreational coding job to build a maker, so I did that: <a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/MYgbXjv\">Word Search Puzzle Maker<\/a>. It&#8217;s not fancy, you just write in your words then a button click will fill in the un-filled spaces with random letters and you&#8217;re good to print it out.<\/p>\n\n\n\n<p>One of the configuration options for the &#8220;maker&#8221; is how big you want to build the grid. A 10\u00d710 grid is the default, but it&#8217;s settable by just <strong>setting a variable in one place<\/strong>. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"517\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?resize=1024%2C517&#038;ssl=1\" alt=\"\" class=\"wp-image-4944\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?resize=1024%2C517&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?resize=300%2C152&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?resize=768%2C388&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?resize=1536%2C776&amp;ssl=1 1536w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?resize=2048%2C1034&amp;ssl=1 2048w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>It turns out it&#8217;s useful to have this variable in all three of the important front-end technologies at play: the HTML, CSS, and JavaScript. The relevant variable here is <code>size<\/code>, which represents the number of cells across and tall the grid is. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>HTML<\/strong> \u2014 Well, Pug, actually. Pug generates HTML, and having the <code>size<\/code> there means I can write a loop that generates the number of elements in the grid the way I need.<\/li>\n\n\n\n<li><strong>CSS<\/strong> \u2014 Having the <code>size<\/code> there means I can set up the CSS grid with the appropriate columns\/rows. <\/li>\n\n\n\n<li><strong>JavaScript <\/strong>\u2014 By having the <code>size<\/code> variable available there, I was able to implement arrow key navigation fairly easily which helped the experience of setting new words.<\/li>\n<\/ul>\n\n\n\n<p>It all starts with that Pug code, so, ya know, sorry if that&#8217;s cheating. But here&#8217;s the rub:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">- <span class=\"hljs-keyword\">const<\/span> size = <span class=\"hljs-number\">10<\/span>;\nscript \n  | window.size = <span class=\"hljs-comment\">#{size};<\/span>\n  | document.documentElement.style.setProperty(<span class=\"hljs-string\">'--size'<\/span>, <span class=\"hljs-comment\">#{size});<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The dash (<code>-<\/code>) in that Pug code essentially means &#8220;this is JavaScript&#8221;, and the Node process that runs to create the HTML runs it. That means I can use the variable later to create the grid.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">.grid\n  - for (let i = 0; i <span class=\"hljs-tag\">&lt; <span class=\"hljs-attr\">size<\/span>**<span class=\"hljs-attr\">2<\/span>; <span class=\"hljs-attr\">i<\/span>++)\n    <span class=\"hljs-attr\">.letter<\/span>(<span class=\"hljs-attr\">data-index<\/span>=<span class=\"hljs-string\">i)<\/span>\n      <span class=\"hljs-attr\">input<\/span>(<span class=\"hljs-attr\">maxlength<\/span>=<span class=\"hljs-string\">1,<\/span> <span class=\"hljs-attr\">matches<\/span>=<span class=\"hljs-string\">\"&#91;A-Za-z]\"<\/span>)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The variable gets passed from Pug into client-side JavaScript by making a script tag and creating a variable off the <code>window<\/code> object. A little variable interpolation makes that possible. <\/p>\n\n\n\n<p>The variable gets passed to CSS in a similar fashion, using client-side JavaScript to call <code>setProperty<\/code> on the <code>documentElement<\/code>. That CSS custom property will then cascade to wherever we need it. I can use it on another element like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.grid<\/span> {\n  <span class=\"hljs-attribute\">display<\/span>: grid;\n  <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-built_in\">repeat<\/span>(var(--size), <span class=\"hljs-number\">1<\/span>fr);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That&#8217;s it really. I just got a kick out of setting a variable in one place and making use of it in three languages. <\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_MYgbXjv\" src=\"\/\/codepen.io\/anon\/embed\/MYgbXjv?height=450&amp;theme-id=47434&amp;slug-hash=MYgbXjv&amp;default-tab=html,result&amp;editable=true\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed MYgbXjv\" title=\"CodePen Embed MYgbXjv\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Try changing the <code>size<\/code> above. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set a variable in Pug, then create an inline script which sets that variable for using in JavaScript and use setProperty to pass it to CSS.<\/p>\n","protected":false},"author":1,"featured_media":4944,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[7,180,3,279],"class_list":["post-4908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-css","tag-custom-properties","tag-javascript","tag-pug"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/01\/CleanShot-2025-01-08-at-07.18.41%402x.png?fit=2162%2C1092&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/4908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=4908"}],"version-history":[{"count":6,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/4908\/revisions"}],"predecessor-version":[{"id":4945,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/4908\/revisions\/4945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/4944"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=4908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=4908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=4908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}