跪拜 Guibai
← All articles
CSS · Frontend · Design Patterns

Layered CSS Variables and a Ten-Floor Template Keep AI-Generated Product Pages Uniform

By Mh ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

AI-generated front-end code is usually inconsistent and hard to review; this approach shows that a strict variable naming scheme and a template-as-spec can clamp AI output into one style, which directly cuts review time and makes per-section overrides simple.

Summary

Product detail pages at one e-commerce brand were built as one-off custom work: nearly every floor on every page had its own animation, so nothing shared except fonts and theme colors. Rebuilding that per page was expensive. Earlier attempts at a component library failed on customization, and an animation template with 100+ animations just shifted the cost to newcomers hunting for the right snippet.

When AI-assisted code generation arrived, it made pages faster but introduced fresh problems: inconsistent output, heavy review time, and dead code that made fine-tuning an animation nearly impossible. The component library tackles this by constraining AI from the start. Styles live under a single root class name so nothing leaks to other pages. Variables follow a namespace-type-object-modifier naming scheme, split into three layers: base values, semantic variables, and composite variables. Composite variables are defined on every element so a floor-level override of one atomic variable recomputes correctly, instead of being baked in at the root.

The second half is a template: a complete demo page with ten floors, each showing a layout (hero, selling-point cards, image-text, carousel, horizontal scroll, parameter list, packaging). The template code is deliberately standardized: rem units, at most two selectors, only class names, no unused properties, a single active/hide pattern for tabs. AI reads this template before generating, so its output follows the same conventions, cutting review time. Themes and mobile switching override variables only, and interactive behavior stays CSS-first except carousels, which use a single wrapped Swiper call.

After launch, work splits into design, library maintenance, and detail-page development, with one rule: shared things have a single outlet. Any missing variable or component gets reported and added to the shared layer, never as a one-off in a page.

Takeaways
— Styles are scoped under a single root class name to avoid affecting other pages and being polluted by old styles.
— Variable names follow namespace-type-object-modifier so names are derivable; e.g., `--pdp-line-height-superheadline` follows from `--pdp-font-size-superheadline`.
— Variables are split into three layers: base values, semantic variables (one property each), and composite variables that bundle weight, size, line-height, and font family into a `font` shorthand.
— Composite variables are defined on the root and every descendant so an override of one atomic variable takes effect at the point of use.
— The template is a complete demo page with ten floors, each demonstrating a layout: hero, selling points, image-text, multi-tab carousel, horizontal scroll, parameter list, packaging.
— Template code enforces rem units, at most two-level selectors, only class names, no unused properties, and a consistent `active`/`hide` toggle for tabs.
— Theme switching uses `data-theme="dark"` on the root, and responsive breakpoints (max-width 1023px or portrait) override variables only, not typography classes.
— Interactive behavior stays CSS-first; only carousels use Swiper, wrapped in a single shared method.
— Post-launch team structure: design maintains Figma and acceptance criteria; library maintenance handles shared styles, template, and spec docs; detail-page developers only write product-specific floor styles.
— Development workflow: AI reads the spec doc and template first, then selects floors, replaces images/copy, chooses theme, and outputs root div/style/script for `staticContent`.
Conclusions

A variable naming scheme designed for derivability turns AI into a more reliable consumer: it can infer `--pdp-line-height-superheadline` without documentation, which is a cheaper constraint than a long prompt.

Defining composite variables on each element makes per-floor overrides work by recalculating at the point of use; that's a pattern for design systems where sections need isolated tweaks.

Treating the template as the canonical, executable spec means AI gets examples instead of prose; that's a more reliable way to transfer coding conventions to an LLM.

The 'single outlet' rule for shared styles prevents the usual drift where developers add one-off variations, keeping the library coherent as it grows.

Concepts & terms
Composite variable
A CSS custom property that bundles several related values (font weight, size, line height, font family) into a single shorthand, e.g., `--pdp-superheadline`, used in a `font` property.
Template-as-spec
A code template that encodes naming, structure, and style conventions, so AI and developers can follow it directly instead of reading a prose specification.
From the discussion
Featured comments
卷土的土

The inheritance timing of combined variables is worth noting.

Mh

Yes, the detailed explanation about inheritance is in the definition of combined variables.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗