A size chart is one of the most common reasons a Shopify store installs another app. It's also one of the easiest features to build into the theme yourself: a product metafield holds the table, a theme block renders it, and a native <dialog> element opens it without any JavaScript library.
This tutorial builds that block step by step. It works in any Online Store 2.0 theme that supports theme blocks (the blocks/ folder), and the same Liquid works as a section block in older OS 2.0 themes with small changes noted at the end.
What we're building
- A product metafield where merchants paste the size table as plain text, one row per line.
- A theme block (
blocks/size-chart.liquid) that merchants can add to the product page in the theme editor. - Settings for the button label and the table heading, so nobody has to edit code to change wording.
- A fallback, so the button doesn't appear on products that have no size chart.
Step 1: Create the metafield definition
In the Shopify admin go to Settings → Custom data → Products → Add definition and create:
- Name: Size chart
- Namespace and key:
custom.size_chart - Type: Multi-line text
Merchants then fill it in on each product, using commas between columns and one row per line:
Size, Chest (in), Waist (in) S, 34-36, 28-30 M, 38-40, 32-34 L, 42-44, 36-38 Plain text keeps it simple for the people who actually maintain the catalogue. They can copy the table straight from a supplier spreadsheet.
Step 2: Create the theme block
Create blocks/size-chart.liquid:
{%- assign chart = closest.product.metafields.custom.size_chart.value -%} {%- if chart != blank -%} {%- assign dialog_id = 'size-chart-' | append: block.id -%} {%- assign rows = chart | newline_to_br | split: '<br />' -%} <div class="size-chart" {{ block.shopify_attributes }}> <button type="button" class="size-chart__open" aria-haspopup="dialog" onclick="document.getElementById('{{ dialog_id }}').showModal()"> {{ block.settings.button_label | escape }} </button> <dialog id="{{ dialog_id }}" class="size-chart__dialog" aria-labelledby="{{ dialog_id }}-title"> <h2 id="{{ dialog_id }}-title">{{ block.settings.heading | escape }}</h2> <table class="size-chart__table"> {%- for row in rows -%} {%- assign line = row | strip -%} {%- if line == blank -%}{%- continue -%}{%- endif -%} {%- assign cells = line | split: ',' -%} <tr> {%- for cell in cells -%} {%- if forloop.parentloop.first -%} <th scope="col">{{ cell | strip | escape }}</th> {%- else -%} <td>{{ cell | strip | escape }}</td> {%- endif -%} {%- endfor -%} </tr> {%- endfor -%} </table> <form method="dialog"> <button class="size-chart__close">{{ 'Close' }}</button> </form> </dialog> </div> {%- endif -%} {% schema %} { "name": "Size chart", "settings": [ { "type": "text", "id": "button_label", "label": "Button label", "default": "Size chart" }, { "type": "text", "id": "heading", "label": "Heading", "default": "Find your size" } ], "presets": [{ "name": "Size chart" }] } {% endschema %} A few details worth explaining:
-
closest.productgives the block the product from the section it sits in, so the same block works on the product page and in a featured product section. -
newline_to_br | split: '<br />'is the standard Liquid way to split multi-line text into rows, because Liquid has no direct newline literal. -
forloop.parentloop.firstmarks the first row as the header, so screen readers announce column names. -
escapeon every cell matters: the content comes from a merchant-editable field and should never be output as raw HTML. -
block.shopify_attributeslets the theme editor select and highlight the block.
Step 3: Let the product section accept it
Theme blocks only appear where a section accepts them. In most themes that support theme blocks, the product section already includes this in its schema:
"blocks": [{ "type": "@theme" }] and renders child blocks with:
{% content_for 'blocks' %} If your product section lists specific block types instead, add { "type": "size-chart" } to its blocks array.
Step 4: Style it
Keep the styles in the block or the theme's main stylesheet:
.size-chart__open { text-decoration: underline; background: none; border: 0; padding: 0; cursor: pointer; } .size-chart__dialog { max-width: min(640px, 92vw); border: 0; border-radius: 12px; padding: 1.5rem; } .size-chart__dialog::backdrop { background: rgb(0 0 0 / 0.45); } .size-chart__table { width: 100%; border-collapse: collapse; } .size-chart__table th, .size-chart__table td { padding: .5rem; border-bottom: 1px solid #e5e7eb; text-align: left; } The native <dialog> with showModal() gives you focus trapping, the Escape key and a backdrop for free, so there's no modal library to load.
Step 5: Test it
Run shopify theme dev, open a product with the metafield filled in, and check:
- The button appears only on products with a size chart.
- Tab reaches the button, Enter opens the dialog, Escape closes it, and focus returns to the button.
- The table reads correctly with a screen reader (column headers announced).
- A cell containing
<script>shows as text, not code. - The layout fits on a 360px-wide phone.
Older OS 2.0 themes without the blocks folder
If your theme doesn't support theme blocks yet, move the same markup into the product section as a block type inside its schema tag and replace closest.product with the section's product object. The metafield and dialog logic stay the same.
Why build it in the theme instead of an app?
- No monthly app fee for one small feature.
- Less JavaScript on product pages, which helps speed on phones.
- The merchant still edits it without code: metafield for the data, theme editor for the wording.
Apps still make sense for complex features. For small, display-only features like this, a theme block is usually the better trade.
FAQ
Does this work with Dawn? Dawn's product section uses its own block list. Add the markup as a block type in sections/main-product.liquid, or update to a theme version that supports theme blocks.
Can the chart be shared across products? Yes. Make the metafield a metaobject reference instead of text, create one "Size chart" metaobject per garment type, and read closest.product.metafields.custom.size_chart.value fields from it.
Will this affect SEO? The table is in the HTML, so it's crawlable, but it isn't the main content of the page. Its value is fewer returns and fewer "what size am I?" emails.
If you'd rather have a team build and maintain theme features like this for your store, Dzimark's Shopify development team works with brands in the USA, UK, Canada and Australia.