Custom CSS Styles
Objective
The goal is to enable users to write and apply custom CSS through a module's settings in the Advanced Tab. The implementation ensures that custom CSS styles override module default and modified settings styles.
This understanding will be put in practice in next tutorials.
Prerequisites
This tutorial assumes that you have read, followed, and completed:
Transition from Divi 4 to Divi 5
In Divi 4, the custom CSS fields are defined in $custom_css_fields variable within the PHP module class. It uses the %%order_class%% selector placeholder to set the order-class position in the selector property.
In Divi 5, the custom CSS fields are defined in customCssFields property of the module.json file, making the process more streamlined and modular. The %%order_class%% selector placeholder maps to {{selector}} for the full order class in context. You can also use {{baseSelector}} for the unprefixed module class and customPostTypeSelector for CPT-specific templates. In the Visual Builder, pass baseOrderClass and isCustomPostType into <CssStyle />. On the frontend (PHP), pass the same values into CssStyle::style() from the $args array your module_styles callback receives from Module::render (covered below).
Custom CSS Styles Explanation
CssStyle Component and Class
<CssStyle> component in VB and CssStyle::style() on the frontend (PHP) render custom CSS for the module. Prop names match between JS and the PHP $args array keys (types: JS boolean / string ↔ PHP bool / string|null).
| Prop | Type | Default |
|---|---|---|
| selector | string | '' |
| selectors | object | {} |
| selectorFunction | Function | undefined |
| attr | object | undefined |
| asStyle | boolean | true |
| cssFields | object | undefined |
| orderClass | string | undefined |
| baseOrderClass | string | undefined |
| isCustomPostType | boolean | undefined |
Explanation of Key Props
selector Defines the base CSS selector. Used to target the primary element for styling. The base target for the generated CSS rules. For example, if set to .my-class, all styles will apply to elements with that class. This property handle {{selector}} interpolation effectively.
selectors Optional. Specifies different selectors for various breakpoints and states (e.g., desktop, tablet, mobile). Provides flexibility to define selectors for specific breakpoints or states (e.g., hover, focus). This is especially useful for creating responsive designs.
selectorFunction Optional. Dynamically modifies the selector based on selector, state, breakpoint, or attribute values. Allows advanced customization by enabling programmatic modification of the selector during runtime. This is helpful in dynamic scenarios where the selector may need to adapt — for example, placing :hover on a nested child instead of the module root. See Hover Selectors.
attr Contains CSS attributes and their values across breakpoints and states. Stores the CSS properties and values for different breakpoints and states. For example, it can define separate color values for desktop and mobile views.
asStyle Determines whether to render the output as a <style> element (true) or return it as a plain string (false). When true, the component renders a <style> tag in the DOM, directly applying the styles. When false, it returns the CSS rules as a string, allowing them to be used elsewhere.
cssFields Maps CSS field names to corresponding selector suffixes or other metadata. Maps custom CSS field names to additional metadata. This includes suffixes (e.g., :before, :after) or template structures for advanced styling requirements.
orderClass A unique class name on VB and module order class name on FE as identifier ensuring custom styles override default ones. Ensures specificity by appending an additional class name. This guarantees that custom styles override any pre-existing ones in the DOM.
baseOrderClass The module order class without builder or layout prefixes (for example the bare .et_pb_* or extension module class fragment). Required when any customCssFields entry uses {{baseSelector}}. In VB the parent Module supplies it on StylesProps; forward it to <CssStyle />. In PHP, Module::render supplies $args['baseOrderClass']; pass it into CssStyle::style() so frontend output matches the builder.
isCustomPostType Whether the current layout/post context is a custom post type. Forward from StylesProps to <CssStyle /> (VB) or from $args['isCustomPostType'] into CssStyle::style() (PHP) when you define customPostTypeSelector; otherwise the Visual Builder may ignore the CPT template while the server still branches on CPT.
Placeholders in customCssFields
{{selector}}— Replaced with theorderClassyou pass into<CssStyle />or intoCssStyle::style()as'orderClass'(Theme Builder / layout context may include prefixes such as.et-db #et-boc .et-lwhen that is howorderClasswas computed).{{baseSelector}}— Replaced withbaseOrderClass. PassbaseOrderClassfromStylesProps(VB) or$args['baseOrderClass'](PHP) whenever metadata uses this placeholder.customPostTypeSelector— Optional per-field template used instead ofselectorwhenisCustomPostTypeis true. PassisCustomPostTypefromStylesProps(VB) or$args['isCustomPostType'](PHP) whenever you define this property.
selectorSuffix is plain text — it does not support placeholders such as {{:hover}}. Putting {{:hover}} in customCssFields.selector is also unsupported for this API. To place :hover on a nested element, use selectorFunction or CssStyle selectors as described in Hover Selectors.
For field-level metadata reference, see Custom CSS Fields.
Explanation of cssFields on module.json
The cssFields prop is an optional mapping object that connects custom CSS field names to their selector suffixes and other metadata.
Let's explain how customCssFields of module.json and cssFields of <CssStyle> component or CssStyle class work together:
customCssFieldsinmodule.json:- Contains custom CSS field definitions for the module.
- Maps custom CSS field names to their corresponding selector suffixes.
- Supports
{{selector}}interpolation for dynamic selector placement.
cssFieldsin<CssStyle>or thecssFieldskey inCssStyle::style():- Pass your module’s custom CSS field map (from
module.json/ block registration), same shape ascustomCssFields. - Enables
{{selector}},{{baseSelector}}, and CPT branching when combined withorderClass,baseOrderClass, andisCustomPostType. - Generates custom CSS rules from the saved
cssattribute and field metadata.
- Pass your module’s custom CSS field map (from
Below is the explanation of each prop of cssFields:
{
"customCssFields": {
"moduleQuickTitle": {
"label": "Module Quick Title",
"subName": "moduleQuickTitle",
"selector": "div{{selector}}",
"selectorSuffix": " .d5_tut_simple_quick_module_title"
}
}
}
| Prop | Type | Value in this Field | Description |
|---|---|---|---|
| label | string | Module Quick Title | A user-friendly name for the field, displayed in the UI to represent this specific CSS customization. |
| subName | string | moduleQuickTitle | Internal key or identifier for the field, used programmatically to reference the field in CSS generation logic. |
| selector | string | div{{selector}} | The base selector template where {{selector}} is a placeholder replaced dynamically with the actual selector (e.g., orderClass). |
| selectorSuffix | string | '.d5_tut_simple_quick_module_title' | A plain-text string appended to the resolved selector (no placeholders). Often used to target specific child elements or classes within the parent selector. For nested :hover placement, see Hover Selectors. |
How It Works
The code example can be found below in our Example Modules Plugin
Example of
CssStyleclass.Example of
customCssFieldsin module.json.Example of
CssStylecomponent.
Update module.json
To add the module custom CSS fields, add customCssFields prop along with the definition. In addition, you can use {{selector}} placeholder in the selector property to rearrange the order class selector. Here’s an example:
{
"name": "d5-tut/simple-quick-module",
"d4Shortcode": "d5_tut_simple_quick_module",
"title": "Simple Quick Module",
"titles": "Simple Quick Modules",
"category": "module",
"attributes": {
"module": {
"type": "object",
"selector": "{{selector}}",
"default": {
"meta": {
"adminLabel": {
"desktop": {
"value": "Simple Quick Module"
}
}
}
}
},
"title": {
"type": "object",
"selector": "{{selector}} .d5_tut_simple_quick_module_title",
"attributes": {
"class": "d5_tut_simple_quick_module_title"
},
"tagName": "h2",
"inlineEditor": "plainText",
"elementType": "heading",
"childrenSanitizer": "et_core_esc_previously"
},
"content": {
"type": "object",
"selector": "{{selector}} .d5_tut_simple_quick_module_content",
"attributes": {
"class": "d5_tut_simple_quick_module_content"
},
"tagName": "div",
"inlineEditor": "richText",
"childrenSanitizer": "et_core_esc_previously",
"allowHtml": true
}
},
"customCssFields": {
"moduleQuickTitle": {
"label": "Module Quick Title",
"subName": "moduleQuickTitle",
"selector": "div{{selector}}",
"selectorSuffix": " .d5_tut_simple_quick_module_title"
}
}
}
Key Updates:
customCssFields: ThecustomCssFieldsobject serves as a configuration map that defines specific parts of a module for applying custom CSS. Each field within the object includes properties such aslabel,subName,selector, andselectorSuffix, which together facilitate CSS generation.selector: Set as"div{{selector}}"in themoduleQuickTitlefield.- Purpose: Allows flexible placement of
orderClasswithin the selector.
Configure CssGroup for Custom CSS
Integrate the CssGroup component in the module's advanced settings to manage custom CSS. It is React function component that renders module settings advanced panel.
// React function component that renders module settings' advanced panel.
advanced: ({groupConfiguration}) => (
<React.Fragment>
<CssGroup
mainSelector=".d5_tut_simple_quick_module"
cssFields={groupConfiguration?.css?.component?.props?.cssFields}
/>
<VisibilitySettingsGroup />
<TransitionGroup />
<PositionSettingsGroup />
<ScrollSettingsGroup />
</React.Fragment>
)
Add <CssStyle /> component in the Visual Builder (VB)
- The
<CssStyle />component should be placed at the very bottom of the<StyleContainer />component to ensure that the custom CSS values take precedence over default styles. - Pass
orderClassso the{{selector}}placeholder resolves in metadata. - Pass
baseOrderClassandisCustomPostTypefrom the sameStylesPropstheModulecomponent gives yourModuleStyleswhenevercustomCssFieldsuse{{baseSelector}}orcustomPostTypeSelector(see placeholders above).
/**
* Simple Quick Module's style components.
*/
const ModuleStyles = ({
attrs,
baseOrderClass,
elements,
isCustomPostType,
mode,
noStyleTag,
orderClass,
settings,
state,
}) => (
<StyleContainer mode={mode} state={state} noStyleTag={noStyleTag}>
{/* Element: Module */}
{elements.style({
attrName: 'module',
styleProps: {
disabledOn: {
disabledModuleVisibility: settings?.disabledModuleVisibility,
},
},
})}
{/* Element: Title */}
{elements.style({
attrName: 'title',
})}
{/* Element: Content */}
{elements.style({
attrName: 'content',
})}
<CssStyle
attr={attrs.css}
baseOrderClass={baseOrderClass}
cssFields={elements?.moduleMetadata?.customCssFields}
isCustomPostType={isCustomPostType}
orderClass={orderClass}
selector={orderClass}
/>
</StyleContainer>
);
Add CssStyle::style Method in Frontend (FE)
- The
CssStyle::style()call should be placed at the very bottom of theStyle::add()method withinmodule_styles()so custom CSS overrides default module styles. - Pass
orderClass(andselectorset to the same full order class string) so{{selector}}resolves. - Pass
baseOrderClassandisCustomPostTypefrom$argswhencustomCssFieldsuse{{baseSelector}}orcustomPostTypeSelector, matching the Visual Builder (see placeholders above).Module::renderincludes these keys on$argsfor your styles callback.
/**
* Render module style.
*/
public static function module_styles( $args ) {
$attrs = $args['attrs'] ?? [];
$elements = $args['elements'];
Style::add(
[
'id' => $args['id'],
'name' => $args['name'],
'orderIndex' => $args['orderIndex'],
'storeInstance' => $args['storeInstance'],
'styles' => [
// Module.
$elements->style(
[
'attrName' => 'module',
'styleProps' => [
'disabledOn' => [
'disabledModuleVisibility' => $args['settings']['disabledModuleVisibility'] ?? null,
],
],
]
),
// Title.
$elements->style(
[
'attrName' => 'title',
]
),
// Content.
$elements->style(
[
'attrName' => 'content',
]
),
// Module - Only for Custom CSS.
CssStyle::style(
[
'selector' => $args['orderClass'],
'attr' => $attrs['css'] ?? [],
'cssFields' => self::custom_css(),
'orderClass' => $args['orderClass'],
'baseOrderClass' => $args['baseOrderClass'] ?? null,
'isCustomPostType' => ! empty( $args['isCustomPostType'] ),
]
);
],
]
);
}
Congratulations! You’ve successfully integrated Custom CSS functionality into your Divi 5 module

Custom CSS functionality in Divi 5 empowers developers to enhance module styling by overriding default styles and enabling targeted, dynamic customization. This feature ensures greater flexibility and user control, making modules more adaptable to diverse design requirements.
Got a question or feedback? Ask away!