Hi ,
I want to apply different styles on forms when I use the form in different pages.
Like we have options available for components in pages using style system. Or is there any other way to do this.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @varun790,
Applying different styles to AEM forms on different pages is achievable through multiple approaches. The best method depends on your project requirements and the flexibility of your setup. Below, I’ll walk you through the common methods, including using the Style System, CSS overrides, and Client-Side Libraries (ClientLibs).
The Style System in AEM allows you to define multiple styles for components and apply them dynamically through the page editor. This approach is very similar to how styles are applied for other AEM components.
Enable Style System for the Form Component:
Define Styles in the Policy:
Apply Styles to the Component:
CSS Implementation:
.form-style-1 { background-color: #f0f0f0; padding: 20px; } .form-style-2 { border: 2px solid #000; margin: 15px; }
This approach allows page authors to dynamically select styles for each form instance without requiring developer intervention.
If you need form styles to vary based on the page context (e.g., different page templates or specific page paths), you can implement page-specific CSS overrides.
Add a Page-Specific Class to the Body Element:
<body class="${currentPage.name}">
Write Page-Specific CSS:
.home-page .form { background-color: #e0f7fa; } .product-page .form { border: 1px solid #ff5722; }
This approach is useful when styles are tightly coupled with the page’s context. However, it requires developers to maintain the CSS mappings.
ClientLibs allow you to modularize and load CSS/JS files dynamically. You can use this feature to load specific CSS files for forms based on the page.
Create Separate ClientLibs for Form Styles:
Include Specific ClientLibs on Pages:
<sly data-sly-test="${currentPage.getPath().contains('/home')}"> <link rel="stylesheet" href="/etc.clientlibs/myproject/clientlibs/form-style-1.css"> </sly>
This approach is more advanced and allows for fine-grained control over which styles are loaded.
If the form styling needs to be fully dynamic and driven by page-level configurations, you can use Page Properties or Custom Dialog Fields to pass contextual data to the form component.
Add a Configuration Dialog to the Page:
Pass the Configured Style as a Class:
<div class="form ${pageProperties['formStyle']}">
Define the CSS for Each Style:
This method is ideal when you need authors to control form styles from page properties.
If you need more interactivity (e.g., applying styles based on user actions or runtime conditions), you can use JavaScript to dynamically add or remove classes.
document.addEventListener("DOMContentLoaded", function () { const form = document.querySelector(".form"); if (window.location.pathname.includes("home")) { form.classList.add("form-style-1"); } else if (window.location.pathname.includes("product")) { form.classList.add("form-style-2"); } });
Thanks
Pranay
Views
Replies
Total Likes
Hi @varun790,
Applying different styles to AEM forms on different pages is achievable through multiple approaches. The best method depends on your project requirements and the flexibility of your setup. Below, I’ll walk you through the common methods, including using the Style System, CSS overrides, and Client-Side Libraries (ClientLibs).
The Style System in AEM allows you to define multiple styles for components and apply them dynamically through the page editor. This approach is very similar to how styles are applied for other AEM components.
Enable Style System for the Form Component:
Define Styles in the Policy:
Apply Styles to the Component:
CSS Implementation:
.form-style-1 { background-color: #f0f0f0; padding: 20px; } .form-style-2 { border: 2px solid #000; margin: 15px; }
This approach allows page authors to dynamically select styles for each form instance without requiring developer intervention.
If you need form styles to vary based on the page context (e.g., different page templates or specific page paths), you can implement page-specific CSS overrides.
Add a Page-Specific Class to the Body Element:
<body class="${currentPage.name}">
Write Page-Specific CSS:
.home-page .form { background-color: #e0f7fa; } .product-page .form { border: 1px solid #ff5722; }
This approach is useful when styles are tightly coupled with the page’s context. However, it requires developers to maintain the CSS mappings.
ClientLibs allow you to modularize and load CSS/JS files dynamically. You can use this feature to load specific CSS files for forms based on the page.
Create Separate ClientLibs for Form Styles:
Include Specific ClientLibs on Pages:
<sly data-sly-test="${currentPage.getPath().contains('/home')}"> <link rel="stylesheet" href="/etc.clientlibs/myproject/clientlibs/form-style-1.css"> </sly>
This approach is more advanced and allows for fine-grained control over which styles are loaded.
If the form styling needs to be fully dynamic and driven by page-level configurations, you can use Page Properties or Custom Dialog Fields to pass contextual data to the form component.
Add a Configuration Dialog to the Page:
Pass the Configured Style as a Class:
<div class="form ${pageProperties['formStyle']}">
Define the CSS for Each Style:
This method is ideal when you need authors to control form styles from page properties.
If you need more interactivity (e.g., applying styles based on user actions or runtime conditions), you can use JavaScript to dynamically add or remove classes.
document.addEventListener("DOMContentLoaded", function () { const form = document.querySelector(".form"); if (window.location.pathname.includes("home")) { form.classList.add("form-style-1"); } else if (window.location.pathname.includes("product")) { form.classList.add("form-style-2"); } });
Thanks
Pranay
Views
Replies
Total Likes
Hi @varun790,
Any update on this..?
Thanks
Pranay
Views
Replies
Total Likes