This guide explains how to add a custom CSS classes setting to Shopify theme sections and blocks, allowing merchants to add their own CSS classes through the theme editor.
The example below is for a section but blocks work the same way.
Step 1: Add the Setting to the Section Schema
- Open the section file you want to modify (e.g.,
sections/section.liquid
) - Locate the schema
"settings": []
array (between{% schema %}
and{% endschema %}
) - Add the new setting before the presets section:
{
"type": "text",
"id": "custom_css_classes",
"label": "t:settings.custom_css_classes",
"info": "t:info.custom_css_classes"
}
Step 2: Modify the Section Snippet (if needed)
If your section has a {% render 'section', [anything] %}
right in the beginning, you have to open that snippet. If there isn’t, this code is in the same file as the schema (the one above).
- Open the corresponding snippet file (e.g.,
snippets/section.liquid
) - Find the main div element that has the class attribute
- Add the conditional rendering for custom CSS classes:
<div class="{% if section.settings.custom_css_classes != blank %} {{ section.settings.custom_css_classes }}{% endif %}">
If it is a block,
<div class="{% if block.settings.custom_css_classes != blank %} {{ block.settings.custom_css_classes }}{% endif %}">
Step 3: Add Translation Strings
- Open the translation file (e.g.,
locales/en.default.schema.json
) - Add the setting label in the “settings” section:
"custom_css_classes": "Custom CSS classes"
- Add the info text in the “info” section:
"custom_css_classes": "Add custom CSS classes separated by spaces"
Implementation Details
The solution works by:
- Adding a text input setting in the Shopify theme editor
- Conditionally appending the custom classes to the section’s div element only when they’re not blank
- Providing clear labeling and instructions for merchants through translation strings
Best Practices
- Always use translation keys (t:) for labels and info text
- Only add classes when they’re not blank to avoid unnecessary whitespace
- Test thoroughly in the theme editor to ensure the setting appears correctly
Troubleshooting
If the setting doesn’t appear:
- Check that the JSON syntax is correct in the schema
- Verify that the setting is placed in the correct section of the schema
- Ensure there are no trailing commas in the JSON
If classes aren’t rendering:
- Check that the conditional logic in the snippet is correct
- Verify that the setting ID matches between the schema and snippet
- Test with simple class names to rule out special character issues