Key takeaways#
- A HubSpot module is a folder with
module.html,module.css,module.js,fields.jsonandmeta.json. - Design fields for editors first: clear labels, sensible defaults, help text and groups.
- Keep CSS and JavaScript scoped, small and loaded only when the module is used.
- Work locally with the HubSpot CLI and keep your theme in Git.
Modules are the building blocks of every HubSpot CMS website. A good module lets a marketer build a beautiful page in minutes; a bad one leads to support tickets and broken layouts. This guide summarises how we build custom modules at Spot Ninjas – from the folder structure to field design, HubL, performance and accessibility.
Anatomy of a module#
When you create a module with the HubSpot CLI (hs create module), you get a folder ending in .module with these files:
| File | Purpose |
|---|---|
module.html |
HubL + HTML markup |
module.css |
Styles, included once per page when the module is used |
module.js |
JavaScript, included once per page when the module is used |
fields.json |
Field definitions shown to editors |
meta.json |
Label, icon, content types, global or not, etc. |
Set up a local workflow#
- Install Node.js and the HubSpot CLI.
- Authenticate with
hs init/hs authusing a personal access key. - Fetch your theme with
hs fetchor start from the boilerplate. - Run
hs watchto upload changes automatically while you work in a sandbox or developer account. - Commit everything to Git and deploy to production with
hs upload– ideally from a CI pipeline.
Design fields for editors, not for developers#
The fields you define in fields.json are the user interface for marketers. Good field design is the difference between a module people love and one they avoid.
- Use groups (e.g. “Content”, “Style”, “Advanced”) to keep the sidebar tidy.
- Set defaults that look good, so a freshly dropped module never appears empty.
- Add help text where a choice is not obvious.
- Prefer choice fields over free text for things like alignment, theme colour or layout.
- Use repeaters (
occurrence) for lists such as cards, FAQ items or logos – with min and max limits. - Use visibility rules to hide fields that do not apply to the current layout.
- Use the right field types: image, link, CTA, form, rich text, boolean, number, choice, colour and – for advanced cases – HubDB or CRM object fields.
Writing clean HubL#
HubL is HubSpot’s Jinja-based templating language. A few practices keep modules readable and robust:
{# Card grid module: module.html #}
{% set cards = module.cards %}
{% if cards %}
<section class="card-grid card-grid--{{ module.style.columns }}">
{% for card in cards %}
<article class="card-grid__item">
{% if card.image.src %}
{% set loading = loop.index <= 3 ? "eager" : "lazy" %}
<img src="{{ card.image.src }}" alt="{{ card.image.alt }}" loading="{{ loading }}"
width="{{ card.image.width }}" height="{{ card.image.height }}">
{% endif %}
<h3>{{ card.title }}</h3>
{{ card.text }}
</article>
{% endfor %}
</section>
{% endif %}
- Guard against empty fields with
{% if %}. - Always output image
widthandheightto prevent layout shift. - Use
loading="lazy"for images below the fold. - Escape user input where needed with filters such as
|escape. - Avoid deeply nested logic – move complex data to HubDB or serverless functions.
Scope your CSS and JavaScript#
Module CSS and JS are added to every page where the module appears. Keep them small and scoped:
- Use a unique BEM-style class prefix (e.g.
.card-grid__item) so styles never leak. - Use theme variables and CSS custom properties instead of hard-coded colours.
- Write vanilla JavaScript; avoid loading jQuery or large libraries for small interactions.
- Initialise JS per module instance so several copies on one page do not conflict.
- For styles that depend on field values, use the
require_cssblock or inline CSS variables scoped to the module’s unique name.
Accessibility is part of “done”#
- Use semantic elements: headings in the right order,
<button>for actions,<a>for navigation. - Make interactive modules (tabs, accordions, sliders) keyboard-accessible with correct ARIA attributes.
- Require alt text for informative images, and allow decorative images to have empty alt.
- Check colour contrast for every theme colour option you allow.
- Respect
prefers-reduced-motionfor animations.
Performance checklist#
- Responsive images using HubSpot’s resize parameters or
resize_image_url. - No render-blocking third-party scripts inside modules.
- Lazy-load videos with a thumbnail facade instead of embedding iframes directly.
- Test with Lighthouse and real-device Core Web Vitals after release.
Testing and documentation#
Before release, test each module with minimum and maximum content (one item vs. twenty items, short vs. very long headings), in all layout options and at mobile, tablet and desktop widths. Write a short editor guide: what the module is for, which fields matter, and one or two screenshots of good usage.
Global modules, partials and when to use each#
| Use | Best tool |
|---|---|
| Reusable section with different content per page | Regular module |
| Same content everywhere (e.g. a CTA banner) | Global module |
| Header, footer and other site-wide markup | Global partial |
| Listing driven by a data table | Module + HubDB |
Frequently asked questions#
What is the difference between a module and a template in HubSpot?#
A template defines the overall page layout and drag-and-drop areas. A module is a reusable component with its own fields, markup, CSS and JavaScript that editors place inside templates.
Do I need HubSpot CLI to build modules?#
You can build modules in the design manager, but we recommend the HubSpot CLI for local development, version control with Git and safe deployments.
How much does a custom HubSpot module cost?#
We build custom modules of normal complexity from SEK 5,000 per module (excl. VAT), including responsive design, accessibility and editor documentation.
Can modules use data from HubDB or the CRM?#
Yes. Modules can render data from HubDB tables and, with the right field types and subscriptions, from CRM objects – ideal for listings such as locations, products or events.