How to structure a Custom Code section with styles, markup, and script
Every Custom Code section consists of three parts: styles, markup, and script. Together, these give you full control over appearance, content, and behavior.
Basic structure
<!-- 1. STYLES — Section-specific CSS --><style> .my-section-{{ section.id }} { max-width: 1200px; margin: 0 auto; padding: 48px 16px; }</style><!-- 2. MARKUP — HTML with Liquid logic --><section class="my-section-{{ section.id }}"> <h2>My Custom Section</h2> {%- assign collection = collections['all'] -%} {% for product in collection.products limit: 4 %} <div> <h3>{{ product.title }}</h3> <p>{{ product.price | money }}</p> </div> {% endfor %}</section><!-- 3. SCRIPT — JavaScript scoped to the section --><script> (function() { const section = document.querySelector('.my-section-{{ section.id }}'); if (!section) return; // Your JavaScript logic here })();</script>
Styles
Place your CSS inside a <style> tag at the top of the section. Always scope styles to section.id to prevent conflicts when multiple instances of the same section exist on a page.
Always use section.id in your CSS selectors. Without it, styles from one section instance will leak into others.
Markup
The markup section contains HTML mixed with Liquid tags and objects. This is where you define the visual structure and dynamic content of your section.
Place your JavaScript inside a <script> tag. Scope DOM queries to the section root so multiple instances of the same section don’t interfere with each other, and wrap your code in an IIFE to avoid leaking variables into the global scope.
Always scope DOM queries to the section instance. Using document.querySelector('.toggle') without a section root will match elements across every instance of the section on the page.
Best practices
Scope CSS with section.id
Always include section.id in class names to prevent style conflicts: