I would like to add CSS class to body tag at partial design without changing MVC layout in Sitecore 9.1 SXA
.
Kindly someone assist me if this is possible.
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityAs far as I know there is no option to add a CSS class to body tag from partial design.
What you could do is adding your custom processor to decoratePage
pipeline:
public class AddCustomCssClass : PageDecorator
{
public override void Process(PageDecoratorArgs args)
{
args.AddAttribute("class", "custom-css-class);
}
}
It will allow you to add custom CSS class to <body>
tag without changing SXA layout file.
However this method will be executed when <body>
tag is rendered, not when content of a particular page design is processed.
You can add a class in <body>
tag with SXA OOTB using Styling
section at the page level using Body Css Class
field for every page.
Configuration
Output
Hope it helps!
You said that you want to apply the CSS class to body tag in the partial design. That is not possible OOTB. And you also cannot add this in the Page Design that is holding the partial design where you want to add.
So if you want to do this using OOTB approach on all the pages for which you want to set the CSS class for body, you can set it in the __Standard Values
of a particular page template. So you can set it in the Body CSS class
field and when you will create a new page it will be automatically set and you will see the CSS class appear in the body tag.
Using the Template to design mapping
on the Page Design, you can associate this template to the Page Design (that is holding your partial design) and it will work.
I have tested it and it looks like this.
For the pages those are already created, you can run a simple PowerShell scrip to set the value.
Hope this will help you to achieve this scenario.