{"id":45762,"date":"2022-07-18T10:22:05","date_gmt":"2022-07-18T10:22:05","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45762"},"modified":"2026-10-10T20:57:26","modified_gmt":"2026-10-10T20:57:26","slug":"fluid-type-preview-shortcode","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/fluid-type-preview-shortcode\/","title":{"rendered":"Preview the Tailwind Type Scale in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When you fine-tune a Tailwind type scale in WordPress, you want to see every size at once: how <code>text-9xl<\/code> down to <code>text-xs<\/code> look in your font, and what pixel size each class really ends up with on this screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet prints a preview page for that. Each row shows a sample sentence in one size, the class name to copy, and the measured font size in px and rem. The numbers update live when you resize the window, so fluid or responsive sizes are easy to check. Put it on a private page and keep it for every project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Tailwind type scale preview: every text-* size with its class name and the\n * font size the browser really uses at the current screen width.\n * Scripts Organizer: Script location \"Shortcode\", output via &#91;scorg_shortcode id=\"\u2026\"&#93;.\n *\/\n\n\/\/ Written out in full, so Tailwind (e.g. Winden) finds the classes and generates them.\n$dpc_type_classes = array( 'text-9xl', 'text-8xl', 'text-7xl', 'text-6xl', 'text-5xl', 'text-4xl', 'text-3xl', 'text-2xl', 'text-xl', 'text-lg', 'text-base', 'text-sm', 'text-xs' );\n$dpc_type_sample  = __( 'The quick brown fox jumps over the lazy dog', 'dpc' );\n?&gt;\n&lt;div class=\"dpc-type-preview\" data-dpc-type-preview&gt;\n\t&lt;?php foreach ( $dpc_type_classes as $dpc_type_class ) : ?&gt;\n\t\t&lt;div class=\"dpc-type-preview__row\"&gt;\n\t\t\t&lt;p class=\"dpc-type-preview__sample &lt;?php echo esc_attr( $dpc_type_class ); ?&gt;\"&gt;&lt;?php echo esc_html( $dpc_type_sample ); ?&gt;&lt;\/p&gt;\n\t\t\t&lt;p class=\"dpc-type-preview__legend\"&gt;&lt;code&gt;&lt;?php echo esc_html( $dpc_type_class ); ?&gt;&lt;\/code&gt; &lt;span data-dpc-size&gt;&lt;\/span&gt;&lt;\/p&gt;\n\t\t&lt;\/div&gt;\n\t&lt;?php endforeach; ?&gt;\n&lt;\/div&gt;\n&lt;?php\n\/\/ CSS and JS once per page, however often the shortcode is used.\nif ( ! wp_script_is( 'dpc-type-preview', 'enqueued' ) ) {\n\twp_register_style( 'dpc-type-preview', false, array(), null );\n\twp_add_inline_style(\n\t\t'dpc-type-preview',\n\t\t'.dpc-type-preview{display:grid;gap:1.5rem;padding:2rem;background:#f1f5f9;color:#0f172a}'\n\t\t. '.dpc-type-preview__sample{margin:0;overflow-wrap:anywhere}'\n\t\t. '.dpc-type-preview__legend{margin:.75rem 0 0;padding-top:.5rem;border-top:1px solid #cbd5e1;font-size:.75rem;line-height:1.5;text-transform:uppercase;color:#475569}'\n\t\t. '.dpc-type-preview__legend code{text-transform:none}'\n\t);\n\twp_enqueue_style( 'dpc-type-preview' );\n\n\t$dpc_type_js = &lt;&lt;&lt;'JS'\n(function () {\n\tfunction show(sample) {\n\t\tvar output = sample.parentElement.querySelector('&#91;data-dpc-size&#93;');\n\t\tvar px = parseFloat(window.getComputedStyle(sample).fontSize);\n\t\tvar root = parseFloat(window.getComputedStyle(document.documentElement).fontSize) || 16;\n\t\toutput.textContent = '\u00b7 ' + Math.round(px * 100) \/ 100 + 'px \/ ' + Math.round((px \/ root) * 1000) \/ 1000 + 'rem';\n\t}\n\n\tfunction init() {\n\t\tvar samples = document.querySelectorAll('&#91;data-dpc-type-preview&#93; .dpc-type-preview__sample');\n\t\tsamples.forEach(show);\n\t\t\/\/ A new font size changes the element's size: covers window resizes and late-loading CSS.\n\t\tif ('ResizeObserver' in window) {\n\t\t\tvar observer = new ResizeObserver(function (entries) {\n\t\t\t\tentries.forEach(function (entry) {\n\t\t\t\t\tshow(entry.target);\n\t\t\t\t});\n\t\t\t});\n\t\t\tsamples.forEach(function (sample) {\n\t\t\t\tobserver.observe(sample);\n\t\t\t});\n\t\t}\n\t}\n\n\tif (document.readyState === 'loading') {\n\t\tdocument.addEventListener('DOMContentLoaded', init);\n\t} else {\n\t\tinit();\n\t}\n})();\nJS;\n\twp_register_script( 'dpc-type-preview', false, array(), null, array( 'in_footer' =&gt; true ) );\n\twp_add_inline_script( 'dpc-type-preview', $dpc_type_js );\n\twp_enqueue_script( 'dpc-type-preview' );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The preview uses Scripts Organizer&#8217;s own <strong>Shortcode<\/strong> script location: markup, CSS and JavaScript sit in one code block and only load where you place it.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name.<\/li>\n\n\n<li>Set <strong>Trigger location<\/strong> to <strong>Conditions<\/strong> and leave <strong>Template<\/strong> on <strong>All<\/strong>. The Shortcode location is only offered with the Conditions trigger.<\/li>\n\n\n<li>Under <strong>Script location<\/strong> tick <strong>Shortcode<\/strong>, paste the code into the PHP editor and publish.<\/li>\n\n\n<li>Scripts Organizer now shows a shortcode such as <code>&#91;scorg_shortcode id=\"123\"&#93;<\/code> for this block. Copy it, create a page (draft or private is fine), add a <strong>Shortcode<\/strong> block and paste it there. In Oxygen, Bricks or another builder use its shortcode element.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The Shortcode location fits because the preview has no settings and lives on one page. Scripts Organizer includes the file each time the shortcode renders, so the code declares no functions and adds its CSS and JS only once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sizes themselves come from Tailwind, for example compiled by <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/winden\/\">Winden<\/a>. Tailwind is only needed for that: without it the preview still renders, and every row reports the same size, which tells you the <code>text-*<\/code> classes are not loaded on the page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The class names are written out in full in <code>$dpc_type_classes<\/code>. Tailwind only generates classes it can find as complete strings, so building them like <code>'text-' . $size<\/code> would leave them out.<\/li>\n\n\n<li><code>getComputedStyle()<\/code> reads the font size the browser really applies, whatever unit the class uses (px, rem, <code>clamp()<\/code>), and the script prints it in px and in rem of the root font size.<\/li>\n\n\n<li>A <code>ResizeObserver<\/code> watches each sample. When the window is resized or the Tailwind CSS loads late, the sample changes size and its number is measured again. The older version waited a fixed five seconds and needed jQuery.<\/li>\n\n\n<li>The sample text and class names are escaped, the small layout CSS is added once per page as an inline style, and the script once in the footer.<\/li>\n\n\n<li>The legend of <code>text-xl<\/code> said <code>text-xlf<\/code> in the older version; the class and the legend now come from the same value.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Your own scale names: edit <code>$dpc_type_classes<\/code>, for example add <code>text-10xl<\/code> or fluid classes. For fluid sizes see the <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/tailwind-fluid-type-calculator\/\">Tailwind fluid type calculator<\/a>.<\/li>\n\n\n<li>Your own sample text: change <code>$dpc_type_sample<\/code>, for example to a heading from the design.<\/li>\n\n\n<li>Preview a font family too: add a class like <code>font-serif<\/code> next to <code>dpc-type-preview__sample<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If Winden scans only some files or post types for classes, add the <code>text-*<\/code> sizes to your safelist so they are generated for this page.<\/li>\n\n\n<li>Theme styles for <code>p<\/code> (margins, <code>font-size<\/code> on <code>.entry-content p<\/code>) can win over Tailwind utilities with lower specificity. The measured number shows what really applies.<\/li>\n\n\n<li>Keep the page out of search engines: make it private or a draft you preview.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Preview your Tailwind type scale on a WordPress page: every text-* class with its class name and the real font size in px and rem.<\/p>\n","protected":false},"author":1,"featured_media":46862,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Preview the Tailwind Type Scale in WordPress - Code DPlugins","description":"Preview your Tailwind type scale on a WordPress page: every text-* class with its class name and the real font size in px and rem."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[78],"tags":[],"dpc_library":[204],"class_list":["post-45762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tailwind"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45762"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45762\/revisions"}],"predecessor-version":[{"id":46861,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45762\/revisions\/46861"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46862"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=45762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45762"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=45762"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}